【问题标题】:Adding dependency to Google Play Services inside Android library在 Android 库中添加对 Google Play 服务的依赖
【发布时间】:2016-01-08 19:12:38
【问题描述】:

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

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 服务中定义的类但并未真正在库中配置 Google Play 服务并让它由使用我的库的应用程序完成的库?

谢谢!

【问题讨论】:

    标签: android-studio android-gradle-plugin google-play-services android-library


    【解决方案1】:

    这比我想象的要简单。

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

    【讨论】:

      【解决方案2】:

      您可以使用this page上的指令生成和添加google-services.json

      它有一套逐步生成文件的说明。

      【讨论】:

      • 我知道,问题是我希望这个文件由将使用我的库的应用程序生成。如果我根据您发布的说明生成文件,我的库将特定于单个项目。
      • 我不认为您可以在不使用特定应用程序的客户端 ID 的情况下以通用方式使用 OAuth。允许这样做会破坏 OAuth 架构的全部目的。
      • 但是我有没有办法告诉 Gradle 只使用 Google Play 服务中定义的类来编译库,而不是在我的库中包含 Google Play 服务?我尝试使用“提供”,但它没有工作,因为它找不到 Google Play 服务中定义的资源。
      猜你喜欢
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 2018-06-27
      • 2013-11-03
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多