如何获取当前buildType在Android的摇篮配置摇篮、buildType、Android

2023-09-05 00:51:48 作者:不爱又何必纠缠

我想要动态地在基于当前buildType机器人摇篮项目添加的依赖性。我知道我可以specify在依赖的buildType:

I want to dynamically add a dependency in an Android Gradle project based on the current buildType. I know I can specify the buildType in the dependency:

compile project(path: ':lib1', configuration: 'debug')

但我怎么可以使用当前buildType指定我要导入库的变体,使调试或发布版本自动导入调试或释放库的变种?我想是这样的(其中currentBuildType是包含目前使用buildType名称的变量):

But how can I use the current buildType to specify which variant of the library I want to import, so that a debug or release build automatically imports the debug or release variant of the library? What I want is something like this (where currentBuildType is a variable containing the name of the currently used buildType):

compile project(path: ':lib1', configuration: currentBuildType)

该库的项目,我想导入已成立 publishNonDefault真正,因此所有buildTypes发布。

The library project I want to import has set publishNonDefault true, so all buildTypes are published.

推荐答案

我找不到一个干净的方式来获得在摇篮的配置相的电流生成类型。相反,我分别定义的依赖性为每个制作类型这样的:

I could not find a clean way to get the current build type during the configuration phase of Gradle. Instead I define the dependency for each build type separately like that:

debugCompile project(path: ':lib1', configuration: 'debug')
releaseCompile project(path: ':lib1', configuration: 'release')

如果您有多个版本的类型,很多项目依赖本可以得到非常详细的,但它有可能增加一个功能,使依赖一个班轮。你需要把它添加到你的主摇篮构建文件:

If you have many build types and many project dependencies this can get very verbose, but it is possible to add a function to make the dependency a one-liner. You need to add this to your main Gradle build file:

subprojects {
    android {
        dependencies.metaClass.allCompile { dependency ->
            buildTypes.each { buildType ->
                "${buildType.name}Compile" project(path: ":${dependency.name}", configuration: buildType.name)
            }
        }
    }
}

然后你就可以在你的摇篮模块添加项目依赖关系是这样的:

Then you can add project dependencies in your Gradle modules like this:

allCompile project(':lib1')

如果您还使用构建口味,你将不得不去适应的解决方案。看到此链接为特征的文档: 的http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Library-Publication

If you also use build flavors you would have to adapt the solution. See this link for a documentation of the feature: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Library-Publication

请注意,Android团队正在努力改进这种行为: 的https://$c$c.google.com/p /安卓/问题/详细信息?ID = 52962

Please note that the Android team is working on an improvement for this behaviour: https://code.google.com/p/android/issues/detail?id=52962