Gradle 基于构建类型和风格的依赖

Gradle dependency based on both build type and flavor

我有几种构建类型:debugrelease。 我也有两个口味 pubdev.

pub 风格的应用程序依赖于 pub 库,dev 风格的应用程序也是如此。 现在,我希望 debug 构建类型应用程序依赖于库的 debug 构建。以下不起作用:

pubReleaseCompile project(path: ':common', configuration: "pubRelease")
devReleaseCompile project(path: ':common', configuration: "devRelease")
pubDebugCompile project(path: ':common', configuration: "pubDebug")
devDebugCompile project(path: ':common', configuration: "devDebug")

注意:库设置为编译所有变体。

有没有办法根据风格和构建类型指定条件项目依赖项?

编辑: 为避免混淆,请遵循我当前使用的项目中的相关 build.gradle 文件。

project/common/build.gradle(图书馆)

apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.hugo' // annotation-based code generated logs only in debug build

android {
  defaultPublishConfig "pubRelease"
  publishNonDefault true // four variants of the library are built

  buildTypes {
    debug {}
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
  }
  productFlavors {
    pub {
      // custom build config fields
    }
    dev {
      // custom build config fields
    }
  }
}

dependencies {
  // ...
}

project/parent/build.gradle(使用该库的应用程序模块之一)

apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.hugo'

android {
  // ...

  signingConfigs {
    release {
      // ...
    }
  }

  buildTypes {
    release {
      signingConfig signingConfigs.release
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
      shrinkResources true
      minifyEnabled true
    }
    debug {
      versionNameSuffix '-debug'
    }
  }
  productFlavors {
    pub {
      // custom res values
    }
    dev {
      // custom res values
    }
  }
}

dependencies {
  // ...
  pubCompile project(path: ':common', configuration: "pubRelease")
  devCompile project(path: ':common', configuration: "devRelease")
}

首先定义各种构建类型:

buildTypes {
    pubRelease {
        //config
    }
    devRelease {
        //config
    }
}

创建一个仅针对特定 buildType 和 flavor 执行的任务:

task pubReleaseTask << {
    //code
}

task devReleaseTask << {
    //code
}

可以动态添加依赖:

tasks.whenTaskAdded { task ->
    if (task.name == 'pubRelease') {
        task.dependsOn pubReleaseTask
    }
    if (task.name == 'devRelease') {
        task.dependsOn devReleaseTask 
    }
}

看看Multi-flavor variants 您不应该为此使用 buildTypes。 但是你可以定义多种口味:

flavorDimensions "first", "second"

productFlavors {
    pub {
        flavorDimension "first"
    }
    dev {
        flavorDimension "first"
    }
    deb {
        flavorDimension "second"
    }
    rel {
        flavorDimension "second"
    }
}

然后你可以像这样将依赖项添加到你的库中

pubRelCompile project(path: ':common', configuration: "pubRel")
devRelCompile project(path: ':common', configuration: "devRel")
pubDebCompile project(path: ':common', configuration: "pubDeb")
devDebCompile project(path: ':common', configuration: "devDeb")

Android Gradle 3.x.x

的插件

构建插件 3.x.x 使用变体感知依赖项解析,因此应用程序模块的 devDebug 变体将自动使用其库模块依赖项的 devDebug 变体。要回答这个问题,这就足够了:

implementation project(':common')

在此处阅读更多内容:https://developer.android.com/studio/build/dependencies.html#variant_aware

原回答

我在这里找到了解决方案:https://github.com/JakeWharton/u2020/blob/master/build.gradle

有关为什么我的原始代码不够用的更多信息,请参见此处:https://code.google.com/p/android/issues/detail?id=162285

工作解决方案:

configurations {
  pubDebugCompile
  devDebugCompile
  pubReleaseCompile
  devReleaseCompile
}

dependencies {
  pubReleaseCompile project(path: ':common', configuration: "pubRelease")
  devReleaseCompile project(path: ':common', configuration: "devRelease")
  pubDebugCompile project(path: ':common', configuration: "pubDebug")
  devDebugCompile project(path: ':common', configuration: "devDebug")
}

跟进@dooplaye的例子,假设你只想编译一种风格的leanback,你可以参考下面的代码片段:

applicationVariants.all { variant ->
    def flavorString = ""
    def flavors = variant.productFlavors
    for (int i = 0; i < flavors.size(); i++) {
        flavorString += flavors[i].name;
    }

    if (flavorString.equalsIgnoreCase("pubdeb")) {
        dependencies {
            compile('com.android.support:leanback-v17:22.2.1')
        }
    }
}