Android - change flavor version name based on buil

2019-02-03 02:07发布

问题:

I want to change version name for app flavors but only if it's a debug build.

(e.g. debug builds will have versions like 1.0.1 D (DEBUG) 555 or 1.0.1 P (DEBUG) 555, however I want the release builds only to have version like 1.0.1) How can I achieve this?

Basically I have these build types:

buildTypes {


    debug {
        versionNameSuffix " (DEBUG) " + mBuild

    }
    release {
        runProguard true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), file('proguard-project.txt')
        signingConfig signingConfigs.release
    }
}

and these flavors (for different api environment):

productFlavors {
    dev {
        versionName = android.defaultConfig.versionName + " D"
    }
    prod {
        versionName = android.defaultConfig.versionName + " P"
    }
}

Is there any way how to make release builds without product flavors version name change?

I've tried to set the version name dynamically via

    buildTypes.each { buildType ->
    if(buildType.name == 'debug') {
        productFlavors.dev.versionName = android.defaultConfig.versionName + " D"
    }
}

but this results in

Could not find property 'dev' on GroupableProductFlavorDsl container.

回答1:

After hours of searching I've managed to remove all the changes made to the version name for the release build using this code

applicationVariants.all { variant ->
    if (variant.buildType.name == 'release') {
        variant.mergedFlavor.versionName = android.defaultConfig.versionName;
    }
}


回答2:

Build Type should override the Product Flavor. Try next one.

buildTypes {

    release {
        versionName = android.defaultConfig.versionName
    }
}