排除特定的构建变体

Kun*_*uno 52 android gradle android-gradle-plugin

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

现在我想排除构建变体dev-release,但保留所有其他可能的组合.有没有办法实现这个目标?

ade*_*.se 105

变体滤镜

使用gradle android插件的variantFilter将某些组合标记为忽略.以下是适用于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)
      }
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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


Ark*_*ior 14

使用风味尺寸时尝试这个

variantFilter { variant ->
    def dim = variant.flavors.collectEntries {
        [(it.productFlavor.dimension): it.productFlavor.name]
    }

    if (dim.dimensionOne == 'paid' && dim.dimensionSecond == 'someVal') {
        variant.setIgnore(true);
    }
}
Run Code Online (Sandbox Code Playgroud)


Alb*_*lvo 11

如果使用风味尺寸,请执行以下操作

flavorDimensions "device", "server"

productFlavors {
    emulator {
        dimension = "device"
    }
    phone {
        dimension = "device"
    }
    staging {
        dimension = "server"
    }
    production {
        dimension = "server"
    }
}

android.variantFilter { variant ->
    def device = variant.getFlavors().get(0).name
    def server = variant.getFlavors().get(1).name
    def isRelease = variant.buildType.name.equals('release')
    def isDebug = variant.buildType.name.equals('debug')

    // Disable emulatorProductionRelease build variant
    if (device.equals('emulator') && server.equals('production') && isRelease) {
        variant.setIgnore(true)
    }
}
Run Code Online (Sandbox Code Playgroud)

它易于阅读,您可以针对特定的构建变体.


are*_*lek 9

与其他变量过滤器一样,我发现通过将变量名称与要保留的变量列表进行比较,最容易做到这一点。

所以在我的app/build.gradle文件中,我有类似以下内容:

android {
    variantFilter { variant ->
        def needed = variant.name in [
                'stagingQuickDebug',       // for development
                'stagingFullDebug',        // for debugging all configurations
                'stagingFullCandidate',    // for local builds before beta release
                'stagingFullRelease',      // for beta releases
                'productionFullCandidate', // for local builds before going public
                'productionFullRelease'    // for public releases
        ]
        variant.setIgnore(!needed)
    }
    buildTypes {
        debug {
        }
        release {
        }
        candidate.initWith(release)
    }
    flavorDimensions "server", "build"
    productFlavors {
        staging {
            dimension "server"
            buildConfigField "String", "API_URL", '"https://example-preprod.com/"'
        }
        production {
            dimension "server"
            buildConfigField "String", "API_URL", '"https://example.com/"'
        }
        quick {
            dimension "build"
            minSdkVersion 21
            resConfigs("en", "xxhdpi")
        }
        full {
            dimension "build"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Noa*_*ach 5

这里的解决方案对我不起作用 - 我遇到了这篇文章并将其添加到我的应用程序的 build.gradle 中,它为我解决了这个问题

gradle.taskGraph.whenReady { graph ->
  graph.allTasks.findAll { it.name ==~ /.*MyVariant.*/ }*.enabled = false
}
Run Code Online (Sandbox Code Playgroud)

这就是它的作用 - 等待gradle组装要执行的完整任务列表,然后将所有与名称模式匹配的任务标记为禁用

注意 匹配是精确的 - 上面的表达式允许您匹配名称中某处带有“MyVariant”并且区分大小写的任何任务