Exclude specific build variants

后端 未结 9 888
一向
一向 2020-12-07 11:07

I have the two default build types: debug / release and a couple of flavors: prod / dev.

Now I want to exclude the build variant dev-release, but keep all other poss

相关标签:
9条回答
  • 2020-12-07 11:22

    When working with flavor dimensions try this one

    variantFilter { variant ->
        def dim = variant.flavors.collectEntries {
            [(it.productFlavor.dimension): it.productFlavor.name]
        }
    
        if (dim.dimensionOne == 'paid' && dim.dimensionSecond == 'someVal') {
            variant.setIgnore(true);
        }
    }
    
    0 讨论(0)
  • 2020-12-07 11:22

    If you use flavor dimensions do this:

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

    It's easy to read and you can target specific build variants.

    0 讨论(0)
  • 2020-12-07 11:25

    One more simpler way

    android.variantFilter { variant ->
        if (variant.name == "qaDebug" || variant.name == "devRelease") {
            setIgnore(true)
        }
    }
    

    Or if you place this code inside android {} closure, android. can be omitted

    android {
        // Please always specify the reason for such filtering
        variantFilter { variant ->
            if (variant.name == "qaDebug" || variant.name == "devRelease") {
                setIgnore(true)
            }
        }
    }
    

    Please always put a meaningful comment for things like this.

    UPD: For Kotlin Gradle DSL there is another way:

    android {
        variantFilter {
            ignore = listOf("qaDebug", "devRelease").contains(name)
        }
    }
    
    0 讨论(0)
  • 2020-12-07 11:27

    Using variant filters like others I found it was easiest to do this by comparing the variant name against a list of variants that I want to keep.

    So in my app/build.gradle file I have something like:

    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"
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-07 11:27

    The answer of @ade.se didn't work for me. But I've struggled a little, and written this, that works great:

    android {
    compileSdkVersion 22
    buildToolsVersion '20.0.0'
    
    variantFilter { variant ->
        if (variant.buildType.name.equals('debug') || variant.buildType.name.equals('release')) {
            variant.setIgnore(true);
        }
    }
    
    defaultConfig {
        applicationId "com.fewlaps.quitnow"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 35
        versionName "1.35"
    }
    

    The code you have to add is the variantFilter one, but I've pasted a little of the context to make it easy to understand.

    0 讨论(0)
  • 2020-12-07 11:37

    The solutions here didn't work for me - I run into this post and added this to build.gradle in my app and it solved the issue for me

    gradle.taskGraph.whenReady { graph ->
      graph.allTasks.findAll { it.name ==~ /.*MyVariant.*/ }*.enabled = false
    }
    

    This is what it does - waits for gradle to assemble the complete list of tasks to execute and then it marks all the tasks that match the name pattern as disabled

    NOTE The match is exact - the expression above lets you match any task that has "MyVariant" somewhere in it's name and it is case sensitive

    0 讨论(0)
提交回复
热议问题