Sunday, June 02, 2013

Put Android Maps V2 work on your Emulator

Last time i've been reading a lot about the new features of the Google Android Maps API v2., so after many hours of trying to make work these new features in my emulator, I've found such that solution in the following steps:

1. Need To create a new AVD WITHOUT GoogleApis, for example, the next picture shows my emulator for testing.
2. Next, start the emulator as usual, but this after the starting has been completed, we'll install TWO extra APK files. Those APK files are the following:

  1. The Google Services platform is the component of the OS, which provides of those new features, so the most important thing to remember is to ALWAYS INSTALL the latest version of this file, in my case I downloaded the following file com.google.android.gms-19032013.apk. Be careful to check the LATEST version, at the time of writing this post, this file was the latest one.
  2. I read many in many posts to download and install the following file "com.android.vending-1.apk", but those posts do not mention that this file is simply the Google Playstore APK, neither mention that need the LATEST version, so I downloaded as many files as you can imagine, trying to make the API work. Thus, the file we need is again the LATEST apk file of the Google Playstore, in my case I downloaded the "Playstore v4.0.27.apk" file.
This seems to be so easy when nobody is there to tell you what exactly need to do. I encountered many problems using the wrong file.

If you encounter with the (491 error) when trying to update the Google Play Services, or if you are installing some APK right to the /data/system/, maybe you searched for a solution a lot but I certainly can tell you that the answer is not in that way.

I will omit the steps about how to create and configure the API key for android applications, (but if you are interested, I've added some reference links for this) instead I will focus on the project configuration using the (not my favourite IDE) Eclipse; when the Android Studio gets stable, I will post a new entry (if necessary) about how to configure a project in that IDE.

Ok, let get back to what we want.

1. Create a project.

2. After you created the project, need to import the existing "google_play_services-lib" eclipse project, into the new project, by doing the following:

3. IF is the first tie you are attempting to use the API, then need to import the Google Play Services Lib into the workspace.

File > Import...

Then go to the android-sdk installation path and browse to: /SDK_HOME/extras/google/google_play_services/libproject/google-play-services_lib.


I read in many posts, that is NECESSARY to copy the project into the current workspace, this due to bugs in Eclipse IDE.

4. After this has been done download the source code and replace in your new project (the source code is at the end of the post), you'll notice that many classes couldn't be found.


Indeed, you need to add the new imported project which is marked as a Library into the Android settings of the project.

Right Click on Project > Properties > Android
And in the lower panel, press the "Add" button and choose the recently imported Library. Then press Ok.

5. One last thing to not forget is that Eclipse external libraries need to be copied into the "LIBS" folder of the project to make them available to the Build.

Then copy the "google-play-services.jar" file located in the "LIBS" directory of the "google-play-services_lib" imported project.

Also do not forget to add it to the compilation build path of the project, this for making those classes available for IDE.


6. OK, now configure the AndroidManifest.xml, and use the Google API key generated for your account.


7. Finally a very important step is to add the google-services.jar file into the APK, this to avoid exceptions like the following.

java.lang.NoClassDefFoundError: com.google.android.gms.maps.model.LatLng

So do the next steps:

Right clik project > Properties > Java Build Path.
Go to "Order and Export" tab.
 
Select the CheckBox of the "google-play-service.jar" file, the press OK.

8. Run thh project.

 VoilĂ !!!





The application works perfectly!!!



References:

About my search on web.

http://stackoverflow.com/questions/15894143/java-lang-noclassdeffounderror-com-google-android-gms-rstyleable-in-android
http://stackoverflow.com/questions/14372391/java-lang-noclassdeffounderror-com-google-android-gms-rstyleable
http://www.user.tu-berlin.de/hennroja/tutorial_fixing_java.lang.NoClassDefFoundError%20com.google.android.gms.R$styleable.html
http://stackoverflow.com/questions/13691943/this-app-wont-run-unless-you-update-google-play-services-via-bazaar
http://piotrbuda.eu/2012/05/installing-google-play-on-android-emulator.html

Files needed
Google Play Store, A.K.A (com.android.vending.apk)
Google Play Services, A.K.A. (com.google.android.gms.apk)

Google API

step by step guide 
NOTE, don't forget that you can use your OWN keystore to generate the KEY, but need to configure the Default debug store @Eclipse, Window> Preferences > Android > Build.


Source code

2 comments:

Emma Watson said...

Wow! It is outstanding sharing I was searching about the android maps. This post helps me alot in getting many new things.

Best Offline Maps for Android

Bluestacks said...

MY problem is that I want Bluestacks to display Google Map embedded in any applications. There are applications that use Google Maps. All the applications will have the word Google but the map is blank.... Help...will installing the two apk fix this? I have tried and look through the whole list of Google search on this and sitll can't get it to work