android - 在库模块中使用 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
我已经阅读了一些线程,我必须将此插件应用于顶级应用程序模块。但是如何在我的功能/库模块中访问插件提供的这些生成的资源?
解决方案
在您的库模块中,您应该添加 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')
}
到任何其他需要播放服务的模块,如果它们不依赖于应用程序模块。
如果这不起作用,您能否向我简要介绍一下您的用例?
推荐阅读
- r - plot.window(…) 中的错误:需要有限的“xlim”值,但看起来一切都很好
- python - 从 psycopg2 在 Postgres 中插入 dict 列表作为 json 数组
- python - 在 Heroku 中上传烧瓶应用程序并且工作正常但在我的终端中出现错误,(拉取函数中的错误)
- php - 从某个日期到 12 月计算月份,然后从次年 1 月到同年 12 月计算月份,然后从 1 月到结束日期
- git - 每当我们使用 dotnet 格式运行 git commit 时,如何格式化代码?
- javascript - 需要根据javascript中的特定要求修改数组
- rxjs - 如何在 Svelte 中使用 RXJs 主题之类的存储变量
- c++ - 不完整类型'class QNetworkReply'的无效使用
- javascript - 如果使用 Typescript,从 VueJS 中的子组件调用父方法
- javascript - 如何在 javascript 数组上迭代新的类实例?