Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an Android application in Android Studio that uses the Google Maps Api v2?

I've been having a lot of trouble in Android Studio trying to create an app with GoogleMap.

I have followed the following guide before with (almost) no issues using Eclipse as my IDE:

https://developers.google.com/maps/documentation/android/start

I have never used Android Studio before and I'm having difficulty with the whole project/module paradigm.

I haven't been able to successfully configure the Google Play Services SDK http://developer.android.com/google/play-services/setup.html

Here is one of the weird errors I'm getting:

 Gradle:  FAILURE: Build failed with an exception.  * What went wrong: Execution failed for task ':MyMapApp:compileDebug'. > Compilation failed; see the compiler error output for details.  * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. 
like image 300
whoabackoff Avatar asked May 16 '13 20:05

whoabackoff


People also ask

Can I use Google Maps in my application?

You can use the Google Maps Platform within your applications as long as your site meets the Google Maps Platform Terms of Service.

Is Google Maps API free for Android?

You won't be charged until your usage exceeds $200 in a month. Note that the Maps Embed API, Maps SDK for Android, and Maps SDK for iOS currently have no usage limits and are at no charge (usage of the API or SDKs is not applied against your $200 monthly credit).


2 Answers

Finally I managed to run GoogleMapsAPIv2 project using Android Studio.

EDIT: As mentioned by Xavier, this method is going to work for non-gradle based projects only. And UI part which was used in this tutorial will be excluded from AndroidStudio. So if you have your own project which uses Gradle build system, you need to manually modify build.gradle configuration file since Android Studio UI doesn't affect it.

EDIT2: With AndroidStudio v0.1.1 release, UI part responsible for modules dependencies has been eliminated, so for now we need to update dependencies manually through build.gradle file. UI for changing gradle dependencies is going to be released in next releases

EDIT3: For those who still tries to use this approach - please note that it is obsolete and doesn't work anymore

Here is what I did:

1) I took maps project from the Google Play Services samples and copied that to the separate directory. That is going to be our MapsApiV2 project we will be trying to run. On my Mac it was located at <sdk_location>/extras/google/google_play_services/samples I placed it to the ~/Work/stack/

2) Copied google-play-services_lib project directory to the same place (~/Work/stack), so my working directory looks like this. Btw, lib project is located at <sdk_location>/extras/google/google_play_services/libproject: enter image description here

3) Now let's open Android Studio. On welcome screen press Import Project and import our maps project from ~/Work/stack/maps. Now we see a lot of complaints about unknown reference to GMS library:

enter image description here

4) Now we need to add Google Play Service as a reference library. Going to View -> Open Module Settings

5) On the Modules tab, click + button and select Import Module and import your GooglePlayServices lib. I didn't change anything in the wizards, so clicked Next all the way to the end:

enter image description here

6) Now you need to reference this imported library. Open this screen again (go to View -> Module Settings). Make sure you have your maps project and Dependency tab selected. Click + to add a dependency and select Library. Choose your imported library there:

enter image description here

7) Now we can see that it is not complaining about GMS library, but still complaining about support library:

enter image description here

8) Let's fix it. I have my support library located at <sdk location>/extras/android/support/v13/android-support-v13.jar. So let's try to add it to our workspace. Go to View -> Open Module Settings and select Libraries tab. Select + -> Java and select support library:

enter image description here

9) Now it is going to ask you which project to add this lib to, so make sure you have selected your maps project:

enter image description here

10) At this point code should compile w/o problems. Just make sure you are targeting the right SDK version in Manifest.

Have fun

like image 90
Pavel Dudka Avatar answered Sep 21 '22 04:09

Pavel Dudka


I was following the same instructions except I was creating a new project. Under the project structure I removed the Android-Gradle facet and was able to build successfully. Optionally one can update the gradle build files and add the Android-Gradle facet to the play services library.

NOTE: I changed the name of Google Play Services directory.

build.gradle for Google Play Services library.

apply plugin: 'android-library'  buildscript {     repositories {         mavenCentral()     }      dependencies {         classpath 'com.android.tools.build:gradle:0.4'     } }  dependencies {     compile files('libs/android-support-v4.jar')     compile files('google-play-services.jar') }  android {     compileSdkVersion 17     buildToolsVersion '17.0.0'      sourceSets {         main {             manifest.srcFile 'AndroidManifest.xml'             java.srcDirs = ['src']             resources.srcDirs = ['src']             aild.srcDirs = ['src']             renderscript.srcDirs = ['src']             res.srcDirs = ['res']             assets.srcDirs = ['assets']         }     } } 

build.gradle for test app.

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(':lib-google-play-services')     compile files('../lib-google-play-services/libs/google-play-services.jar') }  android {     compileSdkVersion 17     buildToolsVersion "17.0.0"      defaultConfig {         minSdkVersion 11         targetSdkVersion 16     } } 
like image 38
Frohnzie Avatar answered Sep 20 '22 04:09

Frohnzie