31

我创建了自定义 buildTypes,如下所示:

 buildTypes {
        releasefree.initWith(buildTypes.release)
        releasefree {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        releasepro.initWith(buildTypes.release)
        releasepro {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationIdSuffix ".pro"
        }
        debugfree.initWith(buildTypes.debug)
        debugfree {
            shrinkResources true
            applicationIdSuffix ".debug"
            debuggable true
        }
        debugpro.initWith(buildTypes.debug)
        debugpro {
            shrinkResources true
            applicationIdSuffix ".pro.debug"
            debuggable true
        }
    }

我永远不会使用默认的调试和发布构建类型,并希望将它们从构建变体列表中删除。我有不止几种口味,而且变种的列表太大了。删除具有默认调试和发布类型的变体将有所帮助,因为我永远不会使用它们。

我尝试如下使用变体过滤器,但它不起作用

android.variantFilter { variant ->
    if(variant.buildType.name.endsWith('Release') || variant.buildType.name.endsWith('Debug')) {
        variant.setIgnore(true);
    }
}

我过滤变体的方式是否有问题,或者是否无法使用默认调试和发布构建类型删除变体。

4

4 回答 4

36

弄清楚了。这对我来说是一个非常愚蠢的错误。上述变体过滤器确实有效。名称都是小写的,而我与之比较的字符串中的大写是罪魁祸首。

更改为以下内容(使比较字符串小写)使其按预期工作:

android.variantFilter { variant ->
    if(variant.buildType.name.endsWith('release') || variant.buildType.name.endsWith('debug')) {
        variant.setIgnore(true);
    }
}

或这个

android.variantFilter { variant ->
    if(variant.buildType.name.equals('release') || variant.buildType.name.equals('debug')) {
        variant.setIgnore(true);
    }
}
于 2015-12-17T14:41:11.647 回答
2

如果你想按名称排除使用类似这样的东西

android.variantFilter { variant ->
    if(variant.name.equals("qaRelease")|| variant.name.equals('something')) {
        variant.setIgnore(true);
    }
}
于 2018-07-09T20:32:20.690 回答
0

如果您想忽略特定的构建变体,这里有详细信息以供理解。

flavorDimensions "client", "server"
productFlavors {
    client1 {
        manifestPlaceholders variant : 'Client 1'
        dimension "client"
        applicationId "com.edupointbd.bb"
    }
    client2 {
        manifestPlaceholders variant : 'Client 2'
        dimension "client"
        applicationId "com.edupointbd.bb"
    }
    dev {
        dimension "server"
    }
    staging {
        dimension "server"
    }
    production {
        dimension "server"
    }
}

variantFilter { variant ->
    def names = variant.flavors*.name
    // To check for a certain build type, use variant.buildType.name == "<buildType>"
    if (names.contains("client1") && names.contains("production")) {
        // Gradle ignores any variants that satisfy the conditions above.
        setIgnore(true)
    }
}
于 2021-01-26T13:43:08.647 回答
0

使用 New Variant API,它变成:

androidComponents {
    beforeVariants(selector().withBuildType("release")) { variantBuilder ->
        variantBuilder.enabled = false
    }
}

参考文档

于 2021-12-29T14:04:50.360 回答