在 Android 库中添加对 Google Play 服务的依赖

Adding dependency to Google Play Services inside Android library

我正在开发一个 Android 依赖于 Google Play 服务的库。

Google 提供的有关如何设置 Google Play 服务的说明仅指定将其添加到应用程序项目而不是库时要遵循的步骤。

我尝试对我的库执行相同的步骤并最终得到以下 build.gradle 文件:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0-alpha3'
        classpath 'com.google.gms:google-services:2.0.0-alpha3'
    }
}

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

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

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:support-annotations:+'
    provided 'com.google.android.gms:play-services-auth:8.4.0'
}

apply plugin: 'com.google.gms.google-services'

这个问题是在构建项目时,出现以下异常:

* What went wrong:
Execution failed for task ':processDebugGoogleServices'.
> File google-services.json is missing from module root folder. The Google Services Plugin cannot function without it.

我理解这一点,因为正如 Google Play 服务文档 (https://developers.google.com/android/guides/setup) 中所述,它依赖于 json 文件进行配置。但是,由于我将其构建为库,因此我希望将 google-services.json 文件添加到将使用我的库的项目中,而不是将其放入库本身。

那我该怎么做呢?我如何编译依赖于 Google Play Services 中定义的 类 的库,但没有在库中真正配置 Google Play Services 并让它由使用我的库的应用程序完成?

谢谢!

您可以使用 this page

上的指令生成并添加 google-services.json

它有一组用于生成文件的分步说明。

比我想象的要简单。

我所要做的就是从库的 build.gradle 文件中删除 classpath 'com.google.gms:google-services:2.0.0-alpha3'apply plugin: 'com.google.gms.google-services' 行,然后仅将它们添加到我的应用程序中的 gradle 文件中依赖于此自定义库的项目。