简体   繁体   中英

How to use Google Play Services from an Android Library in Android Studio

I am developping an Android library in Android Studio that needs to use the Google Play Services. It already uses several other jar libraries, and everything works well when my library is used in an android application project.

According to the official Android documentation, I should import the Google Play Services with gradle ( http://developer.android.com/google/play-services/setup.html ). But if I import it that way, when I use my library in an android application (by importing the .aar generated by Android Studio), it just hangs as soon as GooglePlayServicesUtil.isGooglePlayServicesAvailable is called, with absolutely no log shown in Logcat. So my guess was that I need to import the Google Play Services as a jar instead of in the gradle.build file. Here is the gradle.build file I use :

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

repositories {
    mavenCentral()
}

dependencies {
    //compile 'com.google.android.gms:play-services:4.3.23'  //works in my lib, but not my app using the lib
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    compileSdkVersion 19
    buildToolsVersion '19.0.1'

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
    }
    release {
        runProguard false
    }
}

And I have this is my lib's manifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="novom.anyware.anywaresdk"
    android:versionCode="1"
    android:versionName="0.1.0">

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

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:icon="@drawable/ic_launcher">
        <service ... />
        <receiver ... />
        <receiver android:name="novom.anyware.anywaresdk.AWRGeofencesReceiver" />
        <service ... />
        <receiver ... />
        <meta-data android:name="com.google.android.gms.version" android:value="4323000" />
    </application>

</manifest>

The problem here is that I cannot use the Google Play Services as a jar library, because I don't have the resources linked to it :

04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3086 (common_google_play_services_install_title) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x004b
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3082 (common_google_play_services_enable_title) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x0056
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3097 (common_google_play_services_update_title) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x0061
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3094 (common_google_play_services_unsupported_title) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x0073
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3090 (common_google_play_services_network_error_title) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x0085
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3088 (common_google_play_services_invalid_account_title) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x00af
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3094 (common_google_play_services_unsupported_title) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x00ce
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3083 (common_google_play_services_install_button) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x000f
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3080 (common_google_play_services_enable_button) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x0016
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3095 (common_google_play_services_update_button) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x001d
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3091 (common_google_play_services_unknown_issue) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x0007
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3085 (common_google_play_services_install_text_tablet) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x0018
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3084 (common_google_play_services_install_text_phone) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x0038
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3081 (common_google_play_services_enable_text) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x003f
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3096 (common_google_play_services_update_text) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x0046
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3093 (common_google_play_services_unsupported_text) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x0066
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3089 (common_google_play_services_network_error_text) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x006d
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3087 (common_google_play_services_invalid_account_text) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x0074
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3092 (common_google_play_services_unsupported_date_text) in Lcom/google/android/gms/R$string;
04-09 17:43:51.867    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x007b
04-09 17:43:51.877    9715-9828/foo.bar.myoldapp W/dalvikvm﹕ VFY: unable to resolve static field 3091 (common_google_play_services_unknown_issue) in Lcom/google/android/gms/R$string;
04-09 17:43:51.877    9715-9828/foo.bar.myoldapp D/dalvikvm﹕ VFY: replacing opcode 0x60 at 0x000c
04-09 17:43:51.877    9715-9828/foo.bar.myoldapp E/GooglePlayServicesUtil﹕ The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.

I read a lot of post that tell to include the google-play-services_lib as a Module in my Android Library Project but every way I tried failed.

Any suggestion please?

Make sure in your Android SDK Manager, you have downloaded the Android Support Repository, and the Google Repository. These are maven repositories that hold the support libraries and Google Play Services.

Then all you need to do is add the following line to your dependencies in your modules build.gradle:

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

You don't need any jars, or aars.

A simple build.gradle will look like:

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
        versionCode 1
        versionName "1"
    }

    }
dependencies {
        compile 'com.android.support:support-v4:19.0.1'
        compile 'com.android.support:appcompat-v7:19.0.1'
        compile 'com.google.android.gms:play-services:4.2.+'
    }

It seems Google hasn't updated the documentation. There's no need of including the jar anymore, as far as I have tried (analytics & admob already included in there as well). Update your gradle dependency with the latest version (revision 16):

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

or

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

Go to your build.gradle and add this to your dependencies.

dependencies {
    ...
    compile 'com.google.android.gms:play-services:4.2.+'
}

Make sure to update `AndroidManifest.xml with

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

And don't forget to Sync project file with gradle.

在此处输入图片说明

You can also Rebuild Project or Clean Project for good measure.I also recommend restarting Android Studio.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM