Many of Android developers are struggling with Android Studio and Google Maps Android API v2 (or Google Play Services library in general). Well, there should be no problem when you import the project from Eclipse or from InteliJ IDEA. But try to create a new project – you will see nothing but errors.
I don’t like problems. I don’t like when I can’t progress because of problems. Noone does. That’s why I decided to solve the problem and I succeeded. I will show you how. We need:
– Android Studio
– API 17 installed
– Google Play Services installed
– Android Support Library installed (we will make the solution work with Android 2.3.3 – API 9)
Let’s run Android Studio and create a new project. You can call it as you want – doesn’t matter. I have called my – MapStudio. Minimum SDK set to API 9 (Gingerbread 2.3.3), Target and Compile SDK set to API 17 (if your project requires other values just set them – it should work). This tutorial assumes that you created empty project with blank activity. After you click Finish you should see following project structure:
This is standard structure of Android project using Gradle build system.
To use Maps API v2 we need to include Google Play Services library into the project. To do that with current version of Android Studio (0.1.3) we need to do some magic. Android Studio is not finished project yet. It is quite usable but it misses many features. One of them is UI for Gradle configuration. Any changes you make using F4 shortcut (Module Settings) won’t affect Gradle build system. To properly configure Android Studio project we have to do it manually. Fortunately we don’t have to do all the necessary job. The IDE will help us a little, we just need to know how to make it to help us. And the recipe is following.
Let’s create New Module, this time it should be Library Module. To do this we need to select New Module… item from File menu. On the first dialog please select Android Library from the list and press Next. Now we have to provide some data. Set the Application name and Module name to GooglePlayServices (or anything you want). Next one is very important and has to be set exactly as I say. Set the Package name to com.google.android.gms. I have set SDK values to exactly the same as I set for my MapStudio application. Then I have unchecked Create custom launcher icon and Create activity options – we don’t need them in this library project. Now we can finish. You probably get the error message like below but do not be afraid:
Click OK. Now your project structure should look like following:
Please remove android-support-v4.jar from libs folder in you library project (GooglePlayServices). The file has been added by new module wizard by default but we don’t need it here. Libs folder in GooglePlayServices library project should contain only two files like on the image below:
Opposite to that, libs folder in your application project should contain only android-support-v4.jar file.
Now we have project structure ready. We just need to populate our module with appropriate sources. We have to copy res folder located in <SDK location>/google/google_play_services/libproject/google-play-services_lib into MapStudioProject/GooglePlayServices/src/main. You will find res folder already there. Remove it before you copy the one from google-play-services_lib. Then copy AndroidManifest.xml file from <SDK location>/google/google_play_services/libproject/google-play-services_lib into MapStudioProject/GooglePlayServices/src/main (yes, the same location as before 🙂 ).
Now we have to face another part. We have to modify some source files. First let’s modify AndroidManifest.xml file located in MapStudioProject/MapStudio/src/main folder. All the necessary settings are described here.
When you have all the necessary manifest file settings done, let’s include the Map control into our layout file. Open layout XML definition file in text editor. If you have used default settings when creating project the file will be called activity_main.xml. Replace all the text in the file with following (taken from Android Developer tutorials and modified to handle Android 2.3 – MapFragment replaced with SupportMapFragment):
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
Now there is another very important part – Gradle configuration. To properly configure Gradle we HAVE TO understand how the configuration looks under Eclipse or InteliJ IDEA. In short words we have two projects/modules. One of them is our application which uses Compatibility Library and wants to use Google Play Services to handle Maps API v2. Another project/module is Google Play Services library itself. It contain some dummy source file as it has all the API included in JAR file in libs folder. So we may say that Google Play Services project/module depends on google-play-services.jar library. And that’s all. If we understand that we can do everything using Gradle without a problem! So let’s do it!
First let’s open build.gradle file which exists in our library project (GooglePlayServices). Add following dependency:
compile files('libs/google-play-services.jar')
Your file should look like the one below.
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android-library'
dependencies {
compile files('libs/google-play-services.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 17
}
}
Now let’s open build.gradle file included in our application folder. Modify it using following dependencies:
compile files('libs/android-support-v4.jar')
compile project(':GooglePlayServices')
The file should look like the one below:
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
compile project(':GooglePlayServices')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 17
}
}
Now please check if your settings.gradle file contains following line (if not, make it to have it):
include ':MyMaps', ':GooglePlayServices'
If you did everything correctly the project now should compile and run without problems.
Thanks you very much!! 😀 It’s the only tutorial I’ve seen, it seems that nobody knows well how to make Google Maps work on Android Studio jajaja I have just one issue:
android:name=”com.google.android.gms.maps.SupportMapFragment”
AS keeps complaining because it can’t resolve symbole for “maps” :/ any idea?? I’ve done everything exactly as you said, I checked twice 😉
AS is complaining as Gradle is not fully integrated. To get rid of it you have to put google-play-services.jar into application libs folder. Unfortunatelly putting additional JARs into libs folder may lead to problems with compilation and runtime. You have to live with it for now. AS keeps complaining as it is not able to find the reference (references to libraries are still handled in an old IDEA v12 way, not Gradle/IDEA 13 way) so developing may be a little harder – no code completion. But keep in mind that AS is just work-in-progress preview version. It will improve.
I was hoping you not to say that jajaja actually the problem got solved with the “sync project with gradle files” option and everything went just perfect, but that was yesterday. Today I opened my project and eveything was a mess, AS said it was something related to IntelliJ and R. I copied everything to a new project jajaja but, as you say, it’s a preview so it’s nothing we must’nt be prepared to xD. Still better than Eclipse in my opinion 🙂 and again, thanks for this post and the reply!!
I am having this problem for 3 days now. It’s seems imposible to me to solve this. I’ve try step-by-step this tutorial and it’s not working.
This line:
android:name=”com.google.android.gms.maps.SupportMapFragment”/>
Is on red and the Android Studio throws me this:
… 11 more
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.app.Fragment.instantiate(Fragment.java:592)
at android.app.Fragment.instantiate(Fragment.java:560)
at android.app.Activity.onCreateView(Activity.java:4709)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
… 20 more
Caused by: java.lang.ClassNotFoundException: Didn’t find class “com.google.android.gms.maps.SupportMapFragment” on path: /data/app/com.fourfactory.meetu-1.apk
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.app.Fragment.instantiate(Fragment.java:582)
I really don’t know how to solve this…
Are you sure you have configured everything (especially JAR files in libs folders) exactly as I have suggested? The error is caused by uncorrect reference to Google Play Services library. The google-play-services.jar file should only exist in libs folder of the library module. It must not be in your application libs folder. Application takes all reference from the build.gradle setting: compile project(‘:GooglePlayServices’). The android-support-v4.jar file (if you need it) should be ONLY in libs folder of application module.
I’ve noticed that the google Play Service Gradle sample dos it differently.
http://tools.android.com/tech-docs/new-build-system/gpsdemos.zip?attredirects=0&d=1
Maybe I missed something, but is there a reason not to do it like this?
They seem to work with some kind of repo keeping all libs outside the project. I will have a look at it and post an update to my article hopefuly soon.
Thank’s for your Tutorial!
My Android Studio Version is 0.1.5
I tried to create my Project several times exactly you described here.
But at android:name=”com.google.android.gms.maps.SupportMapFragment”/> the “maps.SupportMapFragment” is marked red.
And I always get the Exception.
06-18 18:23:19.934 1320-1320/com.ballooning.ballooningtools E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ballooning.ballooningtools/com.ballooning.ballooningtools.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
at android.app.Activity.setContentView(Activity.java:1881)
at com.ballooning.ballooningtools.MainActivity.onCreate(MainActivity.java:13)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
… 11 more
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
… 20 more
Caused by: java.lang.ClassNotFoundException: Didn’t find class “com.google.android.gms.maps.SupportMapFragment” on path: /data/app/com.ballooning.ballooningtools-2.apk
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
I’m trying to get it up and running for more than one week now.
But it seems to be impossible. 🙁
Do you have an idea to solve my problems?
THANKS!
See my comment on Sotti’s post. The error appear when you have set dependencies in build.gradle files incorrectly.
Thanks for your answer!
I checked the file build.gradle under the my project:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath ‘com.android.tools.build:gradle:0.4’
}
}
apply plugin: ‘android’
dependencies {
compile files(‘libs/android-support-v4.jar’)
compile project(‘:GooglePlayServices’)
}
android {
compileSdkVersion 17
buildToolsVersion “17.0.0”
defaultConfig {
minSdkVersion 9
targetSdkVersion 17
}
}
This is the build.gradle from GooglePlayServices:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath ‘com.android.tools.build:gradle:0.4’
}
}
apply plugin: ‘android-library’
dependencies {
compile files(‘libs/google-play-services.jar’)
}
android {
compileSdkVersion 17
buildToolsVersion “17.0.0”
defaultConfig {
minSdkVersion 9
targetSdkVersion 17
}
}
And here is my settings.gradle
include ‘:BallooningTools’, ‘:GooglePlayServices’
That files look like yours, doesn’t they?
Did you fix it?
The problem is not with the build files. I had your problem and it was because my MainActivity was not properly configured. I needed a
import android.support.v4.app.FragmentActivity;
for
public class MainActivity extends FragmentActivity {
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.Menu;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends FragmentActivity {
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I had the same problem and solved it adding
to the manifest
Your comment wasn’t finished:
I had the same problem and solved it adding
to the manifest
Adding what to the manifest, I am having the same problem as mentioned in this thread.
I’ve try step-by-step this tutorial 5 times too. I am sure everything is ok.
There is no red line and no problem with IDE. I can import everything under com.google.android.gms.maps but when i work the project get “Could not find class ‘com.google.android.gms.maps.model.LatLng'” error in debug mode. What’s wrong ? Please help
I also answered the question already. Check other comments.
Uploaded screenshot, please can you check my codes and lib files. It looks everything ok. But when i click the button crash with that error.
http://www.gezenesor.com/place_image/googleplay.jpg
Thank you
This is the solution http://stackoverflow.com/questions/16779959/android-studio-importing-external-library-jar/16818936#16818936
you can delete my second question. Thank you
Nice tutorial but still not working. I got so many errors at the end that I feel very frustrated
I followed it twice on different operating systems. It definitely works. Could you please show some screenshots showing structure of your project and build.gradle files? I’m unable to help not seeing it.
I’ve try step-by-step but after create New Module.
I only see the android-support-v4.jar from libs folder in the library project (GooglePlayServices). There’s no two files like on the image!
There’s no google-play-services.jar and google-play-services.jar.properties in the libs folder.
There’s also no build folder in the library project (GooglePlayServices)!
confirmed has installed
– API 17 installed
– Google Play Services installed
– Android Support Library installed
Pls help
OK, here’s what I had to do before I got it working:
PROBLEM: in activity_main.xml
android:name=”com.google.android.gms.maps.SupportMapFragment”
SOLUTION:
class=”com.google.android.gms.maps.SupportMapFragment”
PROBLEM: in MainActivity.java
public class MainActivity extends Activity
SOLUTION:
public class MainActivity extends FragmentActivity
Could you be clearer with these remarks:
PROBLEM: in activity_main.xml
android:name=”com.google.android.gms.maps.SupportMapFragment”
SOLUTION:
class=”com.google.android.gms.maps.SupportMapFragment”
Did you replace the problem line one to one with the solution line?
Sweet! After jostling for 3 good days, finally got it working. Ultimate post! Keep sharing the good stuff!
Cheers!
It works! the only solution that fixed this stupid problem.
Thank you so much
There is no /src/main directory. If you set the package name to com.google.android.gms as suggested, there will be /src/com only. What am I missing?
src/main directory is created when you create new project under Android Studio. For old build system (Eclipse, InteliJ IDEA 12) it will be as you described but then you don’t have gradle.
This is a great tutorial ! I had to do a gradlew clean for it to work for me, but otherwise spot on. Thanks !
Thank you so much for gradlew clean info. I’ve breaking my head into what was wrong!
You have saved so many hours!
Thank you so much. You have restored my faith in android dev.
Please post this link to as many Stack Trace questions about google maps and Android Studio. I hope someone gets hold of your post before they go through 2 days of frustration like I did.
Thanks again!
You have saved a week of my time!!!
Thank you so much. This is Probably the only Blog Post I found that shows me how to integrate library projects into Android Studio.
Thanks Again! 🙂
Thank you so much for the tutorial. Saved me from breaking my phone and laptop.
this is my logcat i m facing this problem as my map tracking activity is not opened. please ssuggest me with solution
10-24 19:48:11.445 15151-15151/com.tmc.breadcrumb.trail E/dalvikvm: Could not find class ‘maps.af.k’, referenced from method maps.ag.an.a
10-24 19:48:12.109 1466-1589/? E/LIBGPS: [assist_gps_request_set_id][line = 1414] : Failed with INVALID SET-ID TYPE
10-24 19:48:12.234 1466-1589/? E/LIBGPS: [assist_gps_request_set_id][line = 1414] : Failed with INVALID SET-ID TYPE
10-24 19:48:12.398 1466-1597/? E/GpsLocationProvider: Error getting cell location info.
10-24 19:48:12.398 1466-1597/? E/LIBGPS: [on_request_connection][line = 1157] : on_request_connection!!!
10-24 19:48:12.398 1466-1597/? E/LIBGPS: assist_gps_data_conn_failed: exit
10-24 19:48:12.406 1466-1597/? E/GpsLocationProvider: Error getting cell location info.
10-24 19:48:12.773 1466-1597/? E/GpsLocationProvider: Error getting cell location info.
10-24 19:48:18.773 1466-1597/? E/GpsLocationProvider: Error getting cell location info.
Tried this after following at least another 3 tutorials (which feels like 10 by now), but wasn’t able to make it work, I get the following error at runtime:
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.zeroum.maptest/br.com.zeroum.maptest.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
Anyone have an idea of what might be causing this?
Either way, kudos on the tutorial, it did give me some greater insight about Gradle and how to properly use it.
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
while importing above files getting error “cannot resolve google symbol”