首页 > 解决方案 > 在库模块中使用 Google Play Services Gradle 插件

问题描述

我正在将我的单体应用程序重构为多模块应用程序,并且我在使用 Google Play Services gradle 插件时遇到了困难。我使用一些播放服务功能,如地图或谷歌登录,它们需要一些由 GPS gradle 插件方便地生成到 android 资源的 api 密钥或客户端 ID,我可以在我的代码中使用它们。但是,如果我把这条神奇的线

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

base在我的模块的底部,我build.gradle得到了神秘的错误


FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':base'.
> java.lang.NullPointerException

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

CONFIGURE FAILED in 0s
Cause: java.lang.NullPointerException

我已经阅读了一些线程,我必须将此插件应用于顶级应用程序模块。但是如何在我的功能/库模块中访问插件提供的这些生成的资源?

标签: androidgradlemodulegoogle-play-services

解决方案


在您的库模块中,您应该添加 google play 服务依赖项:

   dependencies {
           api 'com.google.android.gms:play-services-auth:16.0.1'
           api 'com.google.android.gms:play-services-games:17.0.0'
        }

发布后,您在 app 模块中添加依赖项:

 dependencies {
       api project(':yourlibrary')
    }

在应用模块底部添加插件

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

确保 settings.gradle 也有你的库:

include ':app', ':yourlibrary'

并在您的项目级 gradle 中添加依赖项

   classpath 'com.google.gms:google-services:4.2.0'

确保您的 google-services.json 位于 app/ 目录中(位于 Android Studio 应用模块的根目录)

我相信这应该有效。通过这种方式,您可以从库模块访问应用程序中的 google play 服务。

另外,添加

dependencies {
   api project(':yourlibrary')
}

到任何其他需要播放服务的模块,如果它们不依赖于应用程序模块。

如果这不起作用,您能否向我简要介绍一下您的用例?


推荐阅读