Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle versionName and versionCode not working after 3.0 update

    flavorDimensions "app"
    productFlavors {
        dev {
            resValue "string", "base_url", "http://mydevurl/"
            dimension "app"
        }
        prod {
            resValue "string", "base_url", "http://myprodurl/"
            dimension "app"
        }
    }
    applicationVariants.all { variant ->
        def flavor = variant.mergedFlavor
        def versionName
        if (variant.buildType.isDebuggable()) {
            versionName = "debug_0.1.1"
        }else{
            versionName = "0.1.1"
        }
        flavor.versionName = versionName
        flavor.versionCode = 50
    }

Above gradle setup was working fine till Gradle 3.0 update. Couldn't find anything related in the website referred. How to manage this dynamic version control based on this new flavorDimension change?

like image 337
Mohammed Atif Avatar asked Oct 28 '17 14:10

Mohammed Atif


1 Answers

You can dynamically set the versionCode and versionName with the 3.0 Android gradle plugin by using VariantOutput.setVersionCodeOverride() and VariantOutput.setVersionNameOverride().

For your project, this would look something like:

applicationVariants.all { variant ->
    def flavor = variant.mergedFlavor
    def versionName
    if (variant.buildType.isDebuggable()) {
        versionName = "debug_0.1.1"
    }else{
        versionName = "0.1.1"
    }
    flavor.versionName = versionName
    flavor.versionCode = 50
    variant.outputs.all { output ->
        output.setVersionNameOverride(versionName)
        output.setVersionCodeOverride(50)
    }       
}

reference: https://issuetracker.google.com/issues/63785806#comment6

like image 170
Huy N. Avatar answered Nov 11 '22 03:11

Huy N.