简体   繁体   English

使用flavor Dimensions时是否可以排除特定的构建变体?

[英]Is it possible to exclude specific build variant when using flavor Dimensions?

When using Gradle flavorDimensions, is it possible to exclude specific variants? 使用Gradle flavorDimensions时,是否可以排除特定变体? For example - 例如 -

android {
...

flavorDimensions "abi", "version"

productFlavors {
    freeapp {
        flavorDimension "version"
        ...
    }

    x86 {
        flavorDimension "abi"
        ...
    }
}

the following build variants will be created: 将创建以下构建变体:

x86-freeapp-debug x86的freeapp调试
x86-freeapp-release x86的freeapp释放
arm-freeapp-debug 臂freeapp调试
arm-freeapp-release 臂freeapp释放
mips-freeapp-debug MIPS-freeapp调试
mips-freeapp-release MIPS-freeapp释放
x86-paidapp-debug x86的paidapp调试
x86-paidapp-release x86的paidapp释放
arm-paidapp-debug 臂paidapp调试
arm-paidapp-release 臂paidapp释放
mips-paidapp-debug MIPS-paidapp调试
mips-paidapp-release MIPS-paidapp释放

Can "mips-paidapp-release" be manually removed? 可以手动删除“mips-paidapp-release”吗?

Since Gradle 0.9 you can apply a variant filter and iterate over them: 从Gradle 0.9开始,您可以应用变量过滤器并对其进行迭代:

productFlavors {
    freeapp {
        dimension "version"
    }

    x86 {
        dimension "abi"
    }

    paidapp {
        dimension "mips"
    }
}

// Loop variants
android.variantFilter { variant ->
    // Loop flavors
    variant.getFlavors().each { flavor ->
        println variant.buildType.name + " " + flavor.name + " " + flavor.dimension
        if (variant.buildType.name.equals('release') &&
                flavor.name.equals('paidapp') &&
                flavor.dimension.equals('mips')) {
            variant.setIgnore(true)
        }
    }
}

Note: that I changed flavorDimension to dimension because the latter is now the preferred way specify it. 注意:我将flavorDimension更改为dimension因为后者现在是指定它的首选方式。

Note2: the above note requires you to use the newer gradle version: 注意2:以上注释要求您使用较新的gradle版本:

Project/build.gradle should have the following: Project/build.gradle应具有以下内容:

dependencies {
    classpath 'com.android.tools.build:gradle:1.2.3'
}

while Project/app/build.gradle should have this: Project/app/build.gradle应该有:

android {
    buildToolsVersion "22.0.1"
    ...
}

This is how I do it: 我是这样做的:

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

I like it because it's easy to read and you can target specific build variants. 我喜欢它,因为它易于阅读,您可以针对特定的构建变体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Gradle 使用 API 声明 Variant Build Flavor 依赖项并在 KTS 中排除 - Android Gradle declare a Variant Build Flavor dependency using API and Exclude in KTS Android风格和构建变体特定的appboy.xml文件 - Android flavor and build variant specific appboy.xml file 用于实验性插件的风味尺寸和变体过滤器? - Flavor dimensions and variant filters for experimental plugin? 如何结合构建类型和风格来获得构建变体调试<Flavor> - How combine build type and flavor to get a build variant debug<Flavor> 使用风味维度时设置 Android applicationId 的正确方法是什么? - What is the correct way to set Android applicationId when using flavor dimensions? Vector Drawable 不会被 Build Variant Flavor Dimension 覆盖 - Vector Drawable Not Overwritten by Build Variant Flavor Dimension 在Android应用程序中获取产品风味或构建变体 - Get product flavor or build variant in an android app 我们可以按风味组织存储库并构建变体吗? - Can we organize repositories by flavor and build variant 基于 App Variant 的构建配置(BuildType + Flavor) - Build Configurations based on App Variant (BuildType + Flavor) 是否可以根据Android中的构建变型排除某些kotlin文件? - Is it possible to exclude certain kotlin files based on build variant in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM