定义多个变体的依赖关系

Tan*_*.7x 6 android gradle

假设我们有四种构建类型:debug,qa,beta和release.

我们可以为特定变体定义依赖关系,如下所示:

dependencies {
    // These dependencies are only included for debug and qa builds
    debugCompile 'com.example:lib:1.0.0'
    qaCompile 'com.example:lib:1.0.0'
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在不重复工件描述符的情况下为多个变体编译这些依赖项?

例如,我想做这样的事情:

dependencies {
   internalCompile 'com.example:lib:1.0.0'
}
Run Code Online (Sandbox Code Playgroud)

在哪里internalCompile指定包含库debugqa构建的库.

我相信解决方案在于定义一个新的Gradle配置,但如果我创建一个internalCompile配置,我不确定如何确保只为这些依赖项编译qadebug构建.

RaG*_*aGe 6

extendsFrom

此配置扩展的配置的名称.超配置的工件也可在此配置中使用.

configurations {
    // debugCompile and qaCompile are already created by the Android Plugin
    internalCompile
}

debugCompile.extendsFrom(internalCompile)
qaCompile.extendsFrom(internalCompile)

dependencies {
    //this adds lib to both debugCompile and qaCompile
    internalCompile 'com.example:lib:1.0.0'
}
Run Code Online (Sandbox Code Playgroud)

或者:

您可以创建工件描述符集合,并将其与多个配置一起使用.

List internalCompile = ["com.example:lib:1.0.0",
               "commons-cli:commons-cli:1.0@jar",
               "org.apache.ant:ant:1.9.4@jar"]

List somethingElse = ['org.hibernate:hibernate:3.0.5@jar',
                  'somegroup:someorg:1.0@jar']

dependencies {
    debugCompile internalCompile
    qaCompile internalCompile, somethingElse
}
Run Code Online (Sandbox Code Playgroud)