Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Google Play Services library in Android Studio

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());     } } 
like image 269
losttime Avatar asked Jan 12 '14 23:01

losttime


People also ask

How do I import Google Play Services?

To do this, select File > Import, and then select Android > Existing Android Code into Workspace, and browse to the google-play-services_lib project. After google-play-services_lib is successfully imported into your workspace, it needs to be marked as an Android library project.

Where is Google services JSON Android studio?

The google-services. json file is generally placed in the app/ directory (at the root of the Android Studio app module).

Where is my Google Play app library?

You can see all the apps you've ever downloaded on your Android phone by opening the "My apps & games" section in your Google Play Store. The apps you've downloaded are divided into two sections: "Installed" (all the apps currently installed on your phone) and "Library" (all the apps that aren't currently installed).


2 Answers

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)

like image 67
Piyush Agarwal Avatar answered Sep 21 '22 19:09

Piyush Agarwal


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" />
like image 43
Prakash Avatar answered Sep 19 '22 19:09

Prakash