51

I have an Android project that has been developed entirely within Android Studio (currently version 4.2, gradle version 1.9-all). I want to add functionality from Google Play Services.

The project is unable to resolve GooglePlayServicesUtil, and when I enter the import manually (shown below), I get Cannot resolve symbol 'common'.

import com.google.android.gms.common.GooglePlayServicesUtil;

Any idea what I need to do to get GooglePlayServicesUtil to resolve?

What I've Tried

From the Google Play Services Setup it looks like I just have to add the com.google.android.gms:play-services:4.+ dependency to my build.gradle file (and resync project files with gradle) to make the SDK available to my project. I do get an "exploded bundle" in ProjectName/module/build/exploded-bundles, but that doesn't seem like it does the trick.

I have Google Play Services, Android Support Repository and Google Repository installed from the SDK Manager already. I've also deleted and reinstalled them multiple times :)

Edit:

Might I need to manually add google_play_services as a Project/Global Library? I've attempted with no success.

I'm trying to verify that I'm developing against the Platform API with Google Services (if that's even possible), but I'm not sure that's the case. Nothing I change seems to do anything.

The External Libraries of my project shows:

  • < Android API 19 Platform >
  • < 1.7 >
  • joda-time-2.3
  • support-v4-13.0.0

Source Code

This is my ProjectName/module/build.gradle file:

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

apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion '19.0.1'
    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    signingConfigs {
    }
    productFlavors {
    }
}

dependencies {
    compile 'com.google.android.gms:play-services:4.+'
    compile 'joda-time:joda-time:2.3@jar'
}

The com.google.android.gms.version number resolves fine in my manifest. Here is my ProjectName/module/src/main/AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android" >

    <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="19"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.android.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity"/>

        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <provider
            android:name=".DataProvider"
            android:authorities="com.example.android.provider" >
        </provider>

        <receiver android:name=".WidgetProvider" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                   android:resource="@xml/widget_info" />
        </receiver>

        <service android:name=".DatabaseService" />
        <service android:name=".WidgetProvider$UpdateService" />
    </application>

</manifest>

Here is my MainActivity, where I'm trying to check whether GooglePlayServices is Available:

package com.example.android;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.GooglePlayServicesUtil;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onResume() {
        Log.i(TAG, "onResume");
        GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
    }
}
Community
  • 1
  • 1
losttime
  • 1,110
  • 1
  • 13
  • 19
  • Are you still having problems? If so, does it help to do a clean build? We're looking at bug https://code.google.com/p/android/issues/detail?id=64508 which you may be hitting. – Scott Barta Jan 13 '14 at 17:44
  • I've got it working now (thanks to @pyus13), but the bug you refer to sounds in line with what my issues was. I can still recreate it on new projects, so I'll keep my eye out when Android Studio 4.3 gets released. Thanks for the heads up. – losttime Jan 13 '14 at 18:09
  • 1
    If anyone else has come across this question looking for help, try http://stackoverflow.com/questions/21100688/android-studio-0-4-2-suddenly-cannot-resolve-symbols and see if that works for you. – Scott Barta Jan 13 '14 at 20:53
  • I faced same problem, but solved using this: http://stackoverflow.com/a/42063112/4531507 – Rahul Sharma Feb 06 '17 at 08:44

7 Answers7

34

Try this once and make sure you are not getting any error in project Structure saying that "ComGoogleAndroidGmsPlay not added"

Open File > Project Structure and check for below all. If error is shown click on Red bulb marked and click on "Add to dependency".

GMS dependency

This is a bug in Android Studio and fixed for the next release(0.4.3)

Piyush Agarwal
  • 25,608
  • 8
  • 98
  • 111
  • I do get errors at the bottom of my Project Structure windows, but mine says `Library 'Gradle:ComGoogleAndroidGmsPlayServices4030.aar' is not used`. Regardless, when I click the red lightbulb and click "Add to dependency", then select the module I'm working in, then click Apply, it works! – losttime Jan 13 '14 at 14:46
  • 1
    It doesn't look like my `build.gradle` changed at all. Any ideas why this works, and what I can do in the future to prevent having to go through this extra step again? – losttime Jan 13 '14 at 14:53
  • Oh, those errors are pretty much completely bogus. I have a bug open to suppress them. – Scott Barta Jan 13 '14 at 17:43
  • @ScottBarta Without those errors I don't know how I would get to the screen shown in the screenshot (Project libraries settings?). What's the right way to get there? – losttime Jan 13 '14 at 18:13
  • There is no right way to get there; we don't intend for users to use that particular UI at all because it only updates IntelliJ's internal representation of things and doesn't modify the Gradle build files, which are the ultimate source of truth. The frustration for users is, though, that it's IntelliJ's internal representation that's out of whack even when the Gradle build files are correct. This is due to bugs that we're trying to stamp out. – Scott Barta Jan 13 '14 at 18:15
  • @ScottBarta It is bug in Android Studio that it iswriting the .iml file of module again and again. I have already reported and seems It is fixed now. losttime You can try this close Android Studio, delete your .iml file of module not project, Open your studio again. It will give error for file and will start syncing project with gradle. Wait for sync to complete, your file will be back with correct dependencies. – Piyush Agarwal Jan 13 '14 at 19:19
  • "Project Structure" is no longer in the File menu 0.4.4, FWIW. You can find it by clicking the button next to the sync with gradle button (to the left of it). –  Feb 08 '14 at 17:50
  • @dpk its a small bug in AS 0.4.4, actually button is there above "Other Settings" but not clearly visible, the text is missing. Bug is reported here https://code.google.com/p/android/issues/detail?id=65641 and the happy news is it is already fixed for next release https://android-review.googlesource.com/#/c/81400/ – Piyush Agarwal Feb 08 '14 at 18:14
20

I had similar issue Cannot resolve com.google.android.gms.common.

I followed setup guide http://developer.android.com/google/play-services/setup.html and it works!

Summary:

  1. Installed/Updated Google Play Services, and Google Repository from SDK Manager
  2. Added dependency in build.gradle: compile 'com.google.android.gms:play-services:4.0.30'
  3. Updated AndroidManifest.xml with <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
Prakash
  • 4,479
  • 30
  • 42
7

I solved the problem by installing the google play services package in sdk manager.

After it, create a new application & in the build.gradle add this

compile 'com.google.android.gms:play-services:4.3.+'

Like this

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.google.android.gms:play-services:4.3.+'
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Cristiana Chavez
  • 11,349
  • 5
  • 55
  • 54
  • 2
    I don't think that entire **play-services** library is needed for `PlayServicesUtil`. Something like **play-services-gcm** should be sufficient. – IgorGanapolsky Aug 17 '16 at 18:17
4

After hours of having the same problem, notice that if your jar is on the libs folder will cause problem once you set it upon the "Dependencies ", so i just comment the file tree dependencies and keep the one using

dependencies

//compile fileTree(dir: 'libs', include: ['*.jar'])  <-------- commented one 
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.android.support:appcompat-v7:22.2.1'

and the problem was solved.

Mistalis
  • 17,793
  • 13
  • 73
  • 97
3

I just tried out your build.gradle and it worked fine for me to import GMS, so that's not the issue.

This was with Google Play services (rev 13) and Google Repository (rev 4). Check out those are installed one more time :)

quinnjn
  • 623
  • 7
  • 10
  • 1
    Those are the versions I have (just double-checked). What changes when play-services is properly downloaded as a dependency? Does something get added to the projects "External Libraries" section? Does it show up elsewhere? – losttime Jan 13 '14 at 05:12
1

I got the same problem. I just tried to rebuild, clean and restart but no luck. Then I just remove

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

from build.gradle and resync. After that I put it again and resync. Next to that, I clean the project and the problem is gone!!

I hope it will help any of you facing the same.

Asad
  • 1,260
  • 13
  • 19
-3

//gradle.properties

systemProp.http.proxyHost=www.somehost.org

systemProp.http.proxyPort=8080

systemProp.http.proxyUser=userid

systemProp.http.proxyPassword=password

systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost

Viswa
  • 21
  • 2