41

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.
Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
whoabackoff
  • 1,603
  • 4
  • 17
  • 32
  • 1
    Are you targeting the Google APIs as your build target? – nhaarman May 16 '13 at 20:59
  • I set up Google Maps in this [demo project](https://github.com/johnjohndoe/TouchableMapDemo). – JJD Sep 16 '13 at 20:42
  • You can see my the answer to a similar question in http://stackoverflow.com/questions/16759364/multi-project-setup-with-gradle-for-android/17657713#17657713. I hope this helps you! – JavierSP1209 Jul 15 '13 at 15:22

7 Answers7

55

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

Pavel Dudka
  • 20,754
  • 7
  • 70
  • 83
  • 3
    Upvoted the answer. But why does everyone uses the dark theme? Yuk! That hurts my eyes! – Thibault D. May 18 '13 at 11:30
  • i'm receiving `Gradle: package com.google.android.gms.maps does not exist` etc. does anyone know how to fix this? i don't want to open a new question just for this. – EsseTi May 18 '13 at 17:19
  • have you added GooglePlayServices as a reference lib as described on step 6? – Pavel Dudka May 18 '13 at 17:40
  • I followed these steps to the letter and am still getting a similar error to that of EsseTi's. – jmgrosen May 18 '13 at 19:49
  • I followed this https://developers.google.com/maps/documentation/android/intro#sample_code and added the jar of `google_play_services.jar` and it compiles. the problem is that it does not find my phone (4.2.2) attached via usb – EsseTi May 19 '13 at 09:21
  • ok, solved this. another thing. how did you set up the key? what's the pakage? is it `com.example.mapdemo`? and api is just `Google Maps API v2`? because it runs on my phone but the map is blank. – EsseTi May 19 '13 at 09:35
  • 2
    Obtaining map api key was out of scope. You can refer to https://developers.google.com/maps/documentation/android/start#the_google_maps_api_key to get more info how to get map api v2 key – Pavel Dudka May 19 '13 at 17:50
  • yes, u are right. my bad. i found here a comprehensive answer: http://stackoverflow.com/a/13719616/1257185 – EsseTi May 20 '13 at 13:34
  • 4
    I'd like to point out that the question was specifically about Gradle setup, and that this answer, while valid for pure IntelliJ projects, will *not* work on Gradle project. The solution is to edit build.gradle. Editing modules and modules dependencies in the UI will not affect the Gradle setup which is used during building. In fact the next version of Studio will disable this UI. If you don't mind, please edit the answer to add a comment at the top that this is not for Gradle-based project. thanks. – Xavier Ducrohet May 23 '13 at 01:19
  • Hm, I thought Android Studio is already using Gradle for building. And modifying dependencies within UI will affect gradle build configuration. Is there going to be any UI for modifying Gradle dependencies then? – Pavel Dudka May 23 '13 at 01:57
  • Studio uses Gradle for building only if you create a new project or import a Gradle project, but you can open regular IntelliJ projects as well. Changing the modules/dependencies in the UI affects Studio and "fixes" issues inside studio like code-completion but this has no effect on Gradle (if used). We will offer a UI to directly do the change to build.gradle (which then will get re-imported into Studio), but it's not available yet. – Xavier Ducrohet May 23 '13 at 03:24
13

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
    }
}
Frohnzie
  • 3,559
  • 1
  • 21
  • 24
  • 2
    This is the REAL answer! Just one correction: compile files('../lib-google-play-services/libs/google-play-services.jar') is not needed ;-) is already indirect added through the project dependency. – Patrick Boos May 24 '13 at 06:02
  • 2
    As well settings.gradle needs to be updated with an include statement for the library project. – Patrick Boos May 24 '13 at 06:03
9

I have tried and failed many a tutorial on this, but finally find a simple solution that seem to work with Android Studio and gradle

I just installed Android Studio 0.2.3 on my mac, and these are the steps that made me view a maps fragment on a fresh hello world project template:

1) Click the SDK manager button in the toolbar in Android Studio.

2) Under 'Extras' locate 'Google play services' and download it.

3) in your build.gradle file in your src directory, add this line to dependencies:

compile 'com.google.android.gms:play-services:3.1.36'

4) order and install your API-key following this tutorial: https://developers.google.com/maps/documentation/android/start#the_google_maps_api_key

5) add the fragment to your layout xml:

<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.MapFragment"/>

6) you should now be able to run your project on your device.

monopoint
  • 719
  • 1
  • 6
  • 12
  • This is the recommended method for the latest version (0.2.6) of Android Studio. – Daniel Aug 30 '13 at 04:28
  • 1
    Please mind that the fragment must be `com.google.android.gms.maps.SupportMapFragment` if you support older versions. – JJD Sep 16 '13 at 20:40
  • This worked for me with AS 0.3.1 though I did have to exit and restart before the generated APKs would run. – Brian White Nov 05 '13 at 03:38
3

Here is a configuration for a project I made that uses Google Maps API V2, in Android Studio 0.2, with gradle 0.5.+. Also other modules like ActiobarSherlock, and a custom NumberPicker are used, and i just leave them in case someone needs them.

Project structure:

enter image description here

1) TOP Directory settings.gradle:

include ':SuperModule', ':libraries:actionbarsherlock', ':libraries:numberPicker'

2) TOP Directory build.gradle

task assemble {}

(some of you may faces the task assemble not found. thats why you put this line!)

3) SuperModule build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

dependencies {
    compile 'com.google.android.gms:play-services:3.1.36'
    compile 'com.android.support:gridlayout-v7:13.0.0'
    compile project(':libraries:actionbarsherlock')
    compile project(':libraries:numberPicker')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 16
    }
}

Notice the dependencies here. Its actiobarSherlock(ABS), and NumberPicker, used as android Libraries. I also use Play Services(thats why the min sdk must be >=8), and the layout lib (for the space element) Support library is NOT included here, since its included in ABS library!

Actionbar Sherlock build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android-library'

dependencies {
    compile 'com.android.support:support-v4:13.0.0'
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        instrumentTest.setRoot('tests')
    }
}

NumberPicker build.gradle isnt shown because it has the same logic with the others..

Paschalis
  • 11,929
  • 9
  • 52
  • 82
2

I wrote an article and stepped through creating a map application using both the Location interface and google map api. This will go through many of the idiocyncracies I ran into while trying to get a map application up and running including, which SDK's to include, what libraries to import, what to set in the android manifest and how to change the gradle file. Hope this helps

http://www.todroid.com/how-to-create-a-google-map-application-using-android-studio/

mike gold
  • 1,551
  • 12
  • 12
1

I had a similar problem, but it was just that it couldn't find the com.google.android.gms.maps package (it kept on saying it did not exist). This was after I imported everything (seemingly) properly by following the migration tutorial on Google's own docs: http://developer.android.com/sdk/installing/migrate.html

Anyway, after importing everything, I still had the issue and wasn't sure how to set the build path (or the non-eclipse equivalent).

I just had to follow half of your tutorial below, remove the google-play-services library which was already listed in the module settings window, then add it again.

I hope this is useful to someone else.

BroFist
  • 11
  • 1
1

I had similar problem too,i solved importing google_play_services.jar like Library instead google play services project Like a Module.

encastellano
  • 435
  • 3
  • 5