我们有使用 gradle kotlin dsl 编写的构建逻辑的多模块 android 应用程序。我们使用 buildSrc 来提取常见逻辑,例如依赖项版本。我们有类似的东西:
buildSrc/src/main/kotlin/Dependencies.kt:
object Versions {
const val fooVersion = "1.2.3"
const val barVersion = "4.5.6"
}
object Libraries {
val foo = "com.example.foo:foo:$fooVersion"
val bar = "com.example.bar:bar:$barVersion"
}
object Modules {
const val app = ":app"
const val base = ":base"
const val baz = ":baz"
}
Run Code Online (Sandbox Code Playgroud)
然后我们可以在模块的dependencies块中使用这些来避免硬编码/重复值:
app/build.gradle.kts:
dependencies {
implementation(Libs.foo)
implementation(Libs.bar)
implementation(project(Modules.base))
implementation(project(Modules.baz))
}
Run Code Online (Sandbox Code Playgroud)
我们也在 settings.gradle.kts 中使用它:
settings.gradle.kts:
include(
Modules.app,
Modules.base,
Modules.baz
)
Run Code Online (Sandbox Code Playgroud)
这适用于 gradle …
我正在尝试将 AGP 版本更新为7.0.0-beta03from 4.1.2,但在生成资源的任务中遇到了问题。
目前,使用 android gradle 插件,4.1.2,我们有这样的代码,用于注册从任务生成的资源:
afterEvaluate {
android.libraryVariants.all {
val outputPath = "$buildDir/generated/res/foo/$flavorName/${buildType.name}"
val resourceGeneratingTask = tasks.register(...)
val outputDirectory = project.file(outputPath)
val outputs = project.files(outputDirectory).builtBy(resourceGeneratingTask)
registerGeneratedResFolders(outputs)
}
}
Run Code Online (Sandbox Code Playgroud)
在碰到 后7.0.0-beta03,其中android.libraryVariants似乎已被删除并替换为androidComponents.onVariants {},我无法找到一种方法将我们生成的资源挂接到构建中。我addResValue在 api 中看到,但这似乎仅对values我们生成的资源中的资源有效 是raw或drawable。
我找到了一篇介绍变体 API 的中等文章,以及一个包含 Android gradle 配方的github 存储库,但没有找到任何对我们的案例有帮助的内容。