在我的 Android 应用程序中,我有 google play billing 实现,我在 build.gradle 中将其定义为:
implementation 'com.android.billingclient:billing:1.2'
另外,我有自己创建的库,用于将一些共享代码集成到我的项目中。在那个库中,我想实现 google play billing 实现代码。为此,我也在库的 build.gradle 文件中定义了计费库。
我正在使用 gradle 版本:
'com.android.tools.build:gradle:3.2.0'
是否有任何选项可以只在一个地方(build.gradle)定义库并在两个地方使用它?
最佳答案
从 Gradle Plugin 版本 3.0.0 开始,有一种更好的方法可以做到这一点。我们可以控制每个依赖项是只对当前模块可用,还是对当前模块和依赖它的任何模块可用。这将使我们能够轻松地在项目中跨模块共享依赖关系。
这是我们用来声明依赖关系的方式:
compile 'com.android.billingclient:billing:1.2'
以下是应该替换编译的新配置:
implementation 'com.android.billingclient:billing:1.2'
此依赖项仅在此 modAPI 中使用
api 'com.android.billingclient:billing:1.2'
此依赖项也将在依赖此模块的任何构建中可用。假设我们有一个名为“library”的模块被“app”模块使用,我们可以使用 api 配置来声明依赖项应该与依赖它的任何模块共享。
库模块build.gradle
dependencies {
// dependencies marked 'implementation' will only be available to the current module implementation 'com.squareup.okhttp:okhttp:2.4.0'
// any dependencies marked 'api' will also be available to app module
api 'com.android.billingclient:billing:1.2'
}
app module build.gradle:
dependencies {
// declare dependency on library module
implementation project(':library')
// only need to declare dependencies unique to app
implementation 'example.dependency:1.0.0'
}
请参阅此以获取更多信息和图表。 https://medium.com/@julesrosser/handling-gradle-dependencies-in-multiple-module-android-projects-9e590c3d20dc
关于android - 如何在 Android 应用程序及其库之间共享 gradle 依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53054624/