安卓工作室:如何删除/过滤默认调试和发布buildTypes的构建变体,并只保留那些使用自定义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);
    }
}

我过滤变体的方式有什么问题吗?或者不可能用默认的调试和发布构建类型删除变体。

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

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

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);
    }
}

有了新的变体API,它就变成了:

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

参考文件(字段名称过去是enabled而不是enable,但现在已弃用)

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

android.variantFilter { variant ->
    if(variant.name.equals("qaRelease")|| variant.name.equals('something')) {
        variant.setIgnore(true);
    }
}

如果您想忽略特定的构建变体,下面是详细信息。

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)
    }
}

最新更新