The import com.google.android.gms cannot be resolved
Asked Answered
G

13

81

I am new to Android development. By following this tutorial I got errors The import com.google.android.gms cannot be resolved when I wrote the code into the MainActivity.java:

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
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;

By searching for various fixes I made sure to try these out:

  • I have downloaded Google API's for a particular API level through Android SDK manager.
  • I checked off Google API as project build target.
  • I have added <uses-library android:name="com.google.android.maps" /> line into the AndroidManifest.xml as a child of <application> tag.
  • I cleaned the project by selecting Project->Clean... and built it from scratch.
  • I included .jar file for maps by right-clicking on my project, went to build path and added external archive locating it in my SDK: android-sdk-windows\add-ons\addon_google_apis_google_inc_8\libs\maps

Unfortunately, none of the above have helped.

Graviton answered 13/4, 2013 at 16:27 Comment(0)
C
41

I checked off Google API as project build target.

That is irrelevant, as that is for Maps V1, and you are trying to use Maps V2.

I included .jar file for maps by right-clicking on my project, went to build path and added external archive locating it in my SDK: android-sdk-windows\add-ons\addon_google_apis_google_inc_8\libs\maps

This is doubly wrong.

First, never manually modify the build path in an Android project. If you are doing that, at best, you will crash at runtime, because the JAR you think you put in your project (via the manual build path change) is not in your APK. For an ordinary third-party JAR, put it in the libs/ directory of your project, which will add it to your build path automatically and add its contents to your APK file.

However, Maps V2 is not a JAR. It is an Android library project that contains a JAR. You need the whole library project.

You need to import the android-sdk-windows\add-ons\addon_google_apis_google_inc_8 project into Eclipse, then add it to your app as a reference to an Android library project.

Cardsharp answered 13/4, 2013 at 16:32 Comment(5)
You were right. I imported google-play-services-lib (which also contains the maps) then added it as a reference and those errors are now gone. Still I am not sure what to do about the .jar file already imported. Should I be concerned about deleting it somehow?Graviton
@ArnoldasBendoraitis: Undo the manual change you made to your build path.Cardsharp
Clarification: Import the project into Eclipse using File → Import → Android → Existing Android Code into Workspace. And, of course, the directory you're looking for won't be called 'android-sdk-windows' if you're not using Windows. ;-)Illuminative
@CommonsWare, Somehow if I add the Jar directly it works, but when I try to reference the project instead, it doesn't work. The reference shows the green tick when I do "Add". However, when I click "OK" and reopen Project > Properties, it turns to a red cross again. Do you know why this may be happening?Johannesburg
Maybe it's an update. But currently the path to import is <sdk>\extras\google\google_play_services\libproject\google-play-services_libBiannulate
W
55

Another way is to let Eclipse do the import work for you. Hover your mouse over the com.google.android.gms import that can not be resolved and towards the bottom of the popup menu, select the Fix project setup... option as below. Then it'll prompt to import the google play services library. Select that and you should be good to go.

enter image description here

Withindoors answered 8/4, 2014 at 8:25 Comment(1)
When I try this proposed solution, I receive this message: "No proposals have been found to fix the unresolvable reference to 'com.google.android.gms.internal.fn'. Click here to manually configure the build path or project 'CouponClub'." I already imported "google-play-services_lib" successfully, so I thought that would be enough to have access to com.google.android.gms.internal.fn.Jaborandi
C
41

I checked off Google API as project build target.

That is irrelevant, as that is for Maps V1, and you are trying to use Maps V2.

I included .jar file for maps by right-clicking on my project, went to build path and added external archive locating it in my SDK: android-sdk-windows\add-ons\addon_google_apis_google_inc_8\libs\maps

This is doubly wrong.

First, never manually modify the build path in an Android project. If you are doing that, at best, you will crash at runtime, because the JAR you think you put in your project (via the manual build path change) is not in your APK. For an ordinary third-party JAR, put it in the libs/ directory of your project, which will add it to your build path automatically and add its contents to your APK file.

However, Maps V2 is not a JAR. It is an Android library project that contains a JAR. You need the whole library project.

You need to import the android-sdk-windows\add-ons\addon_google_apis_google_inc_8 project into Eclipse, then add it to your app as a reference to an Android library project.

Cardsharp answered 13/4, 2013 at 16:32 Comment(5)
You were right. I imported google-play-services-lib (which also contains the maps) then added it as a reference and those errors are now gone. Still I am not sure what to do about the .jar file already imported. Should I be concerned about deleting it somehow?Graviton
@ArnoldasBendoraitis: Undo the manual change you made to your build path.Cardsharp
Clarification: Import the project into Eclipse using File → Import → Android → Existing Android Code into Workspace. And, of course, the directory you're looking for won't be called 'android-sdk-windows' if you're not using Windows. ;-)Illuminative
@CommonsWare, Somehow if I add the Jar directly it works, but when I try to reference the project instead, it doesn't work. The reference shows the green tick when I do "Add". However, when I click "OK" and reopen Project > Properties, it turns to a red cross again. Do you know why this may be happening?Johannesburg
Maybe it's an update. But currently the path to import is <sdk>\extras\google\google_play_services\libproject\google-play-services_libBiannulate
R
19

From my experience (Eclipse):

  1. Added google-play-services_lib as a project and referenced it from my app.
  2. Removed all jars added manually
  3. Added google-play-services.jar in the "libs" folder of my project.
  4. I had some big issues because I messed up with the Order and Export tab, so the working solution is (in this order): src, gen, Google APIs, Android Dependencies, Android Private Libraries (only this one checked to be exported).
Riehl answered 2/7, 2013 at 15:55 Comment(2)
Why add the Play Services jar manually, if Gradle will take care of it for you in the build?Munos
Because I didn't have time to convert my projects into Android Studio format using Gradle. This is a solution for Eclipse IDE.Riehl
T
15

In my case or all using android studio

you can import google play service

place in your build.gradle

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

or latest version of play services depend in time you watch this answer

More Specific import

please review this individual gradle imports

https://developers.google.com/android/guides/setup

Google Maps

com.google.android.gms:play-services-maps:7.8.0

error may occurred

if you face error while you sync project with gradle files

enter image description here

make sure you install latest update

1- Android Support Repository.

2- Android Support Library.

3- Google Repository.

4-Google Play Services.

enter image description here

You may need restart your android studio to response

Translucent answered 6/9, 2015 at 13:25 Comment(0)
P
7

Note that once you have imported the google-play-services_lib project into your IDE, you will also need to add google-play-services.jar to: Project=>Properties=>Java build path=>Libraries=>Add JARs

Pundit answered 4/6, 2013 at 4:36 Comment(2)
The most simple one line answer. I was not able to figure out what is happening even after importing Play Services. Then I tired changing it to Google_apis. Finally, a single line solution worked for me.Ailin
Careful, this will cause problems: #15894643Incalculable
D
1

Above solutions should solve your problem. If these do not, make sure you update your Android sdk using SDK manager and install the latest lib project and then repeat the above steps again.

Datura answered 20/9, 2013 at 8:31 Comment(0)
P
1

once again Make sure these 2 things happen correctly nothing more than that. (FOR ECLIPSE USERS)

1) copy the jar file from --> C:\Users(your-username)\android-sdks\extras\google\google_play_services\libproject\google-play-services_lib\libs\ google-play-services.jar

2) Right Click Project--> Build Path -> Add External Archive-> google-play-services.jar

Performative answered 10/6, 2015 at 11:25 Comment(0)
K
1

In Android Studio goto: File -> Project Structure... -> Notifications (last entry) -> check Google Cloud Messaging

Wait a few seconds and you're done :) import com.google.android.gms.gcm.GcmListenerService should be resolved properly

Kosciusko answered 9/10, 2015 at 20:19 Comment(0)
E
1

In my case just add the below code to the build.gradle(:app)

implementation 'com.google.android.gms:play-services-location:16.0.0'
Eliason answered 15/6, 2022 at 6:7 Comment(0)
C
0

I too had the same issue. Got it resolved by compiling with the latest sdk tool versions.(Play services,build tools etc). Sample build.gradle is shown below for reference.

    apply plugin: 'com.android.application'


android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.abc.bcd"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services:8.4.0'
    compile 'com.android.support:appcompat-v7:23.0.1'
}
Ceolaceorl answered 4/2, 2016 at 12:29 Comment(0)
C
0

Supposing that you are using ECLIPSE:

Right click PROJECT PROPERTIES ANDROID

If you have a version of ANDROID checked, you must change it to a GOOGLE API. Choose a version of GOOGLE APIS compatible with your project's target version.

Commonwealth answered 18/2, 2016 at 19:23 Comment(4)
Please explain in details.Antetype
@DmitryIsakov what step would like me to clarify?Commonwealth
Looks like i did it... It was not an easy task to move from Eclipse to AStudioAntetype
@DmitryIsakov - i tried and had to stop because i couldnt afford to take the time off from development and dedicate it to migration. could you please tell me some of the basic steps you took?Commonwealth
B
-1

In my case only after I added gcm.jar to lib folder, it started to work. It was here: C:\adt-bundle-windows-x86_64-20131030\sdk\extras\google\gcm\gcm-client\dist

So the google-play-services.jar didn't work...

Bock answered 19/12, 2013 at 14:51 Comment(1)
This question is not about Google Map v1. It's about Google Map v2. gcm.jar is useless in this case.Pasty
V
-3

I had the same problem so that the dumb API I decided as follows changing the import line

import com.google.android.gms.maps.model.LatLng;
Verified answered 8/5, 2016 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.