Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio Maps Api v2

My code works perfectly on eclipse. I have created a new project on Android Studio and added my classes, I have also followed this popular tutorial answer How can I create an Android application in Android Studio that uses the Google Maps Api v2?

Now the code doesn't show any red flags and it detects the imports fine but when I build the project I get these errors: Gradle: package com.google.android.gms.maps does not exist Gradle: package com.google.android.gms.maps.model does not exist

On a side note I have attempted to import the MAPS sample project like the tutorial says and that has worked fine but I do not know why my new project gives me these errors even though I have followed the tutorial to the letter.

Any idea guys?

like image 986
uaeHamed Avatar asked May 22 '13 18:05

uaeHamed


3 Answers

If you created a new project (which uses Gradle) and then followed the tutorial, it's completely wrong.

When using Gradle (which new projects do by default), you cannot use the UI to add dependencies. You need to add them manually in build.gradle.

It's a crappy solution right now and we're working to make this better.

like image 200
Xavier Ducrohet Avatar answered Sep 23 '22 20:09

Xavier Ducrohet


In short words:

  1. Run Android Studio
  2. Create new project
  3. Create new module of Android Library type, remember to set the package name to com.google.android.gms
  4. Copy res, src folders and AndroidManifest.xml file from google-play-services_lib (you can find it in your SDK folder if you installed it using SDK Manager) to the appropriate folder in your library module ([Project]/[Library]/src/main)
  5. Remove android-support-v4.jar from libs folder under your library module, live only google-play-services.jar and google-play-services.jar.properties files there.
  6. Make sure your build.gradle file in library module contains following:

    dependencies {
        compile files('libs/google-play-services.jar')
    }
    
  7. Make sure your build.gradle for application module contains following:

    dependencies {
        compile files('libs/android-support-v4.jar')
        compile project(':[LibraryModuleName]')
    }
    
  8. Make sure your settings.gradle contain following:

    include ':MapStudio', ':[LibraryModuleName]'
    
  9. You can grab layout and manifest settings from Google Maps API v2 tutorial

  10. Keep in mind that android-support-v4.jar is neccessary only when you want to support Android v2

You can find more detailed description on my blog.

like image 22
hrabia Avatar answered Sep 23 '22 20:09

hrabia


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

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 install 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.

like image 24
monopoint Avatar answered Sep 23 '22 20:09

monopoint