android - 排除特定的构建变体

标签 android gradle android-gradle-plugin

我有两种默认的构建类型:debug/release 和几个风格:prod/dev。

现在我想排除构建变体 dev-release,但保留所有其他可能的组合。有没有办法做到这一点?

最佳答案

变体过滤器

使用 gradle android 插件的 variantFilter 将某些组合标记为忽略。这是 official documentation 中的一个示例它适用于 flavor 维度并展示了如何使用它:

android {
  ...
  buildTypes {...}

  flavorDimensions "api", "mode"
  productFlavors {
    demo {...}
    full {...}
    minApi24 {...}
    minApi23 {...}
    minApi21 {...}
  }

  variantFilter { variant ->
      def names = variant.flavors*.name
      // To check for a certain build type, use variant.buildType.name == "<buildType>"
      if (names.contains("minApi21") && names.contains("demo")) {
          // Gradle ignores any variants that satisfy the conditions above.
          setIgnore(true)
      }
  }
}

正如评论所说,您也可以像这样检查 buildType:

android {
    variantFilter { variant ->
        def names = variant.flavors*.name
        if(variant.buildType.name == 'release' && names.contains("myforbiddenflavor")) {
            setIgnore(true)
        }
    }
}

关于android - 排除特定的构建变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21221868/

相关文章:

android - 我可以在 onBackPressed() 从返回堆栈中弹出 fragment 之前添加动画吗?

android - 如何清除ListAdapter中的数据

java - Android 中的默认录音

Android Exoplayer - 不同视频质量的不同 URL 和手动更改

android - Gradle 找不到 com.android.databinding :dataBinder:1. 0-rc0

android - Kotlin 多平台库接口(interface)的暴露类型未解析

android - 在Gradle Sync上自动重建Android Studio项目

Android Studio 3.0.1 无法解决依赖错误

java - 如何在Gradle useTestNG中使用testng参数

android - 模块化 Android 测试 - 使用基本模块测试的模块测试