how to manipulate apk variant versioncode

1.3k views Asked by At

The versionCode for the android apk gets set in defaultConfig. I would like to change for each of my build types but it seems like this can only be done by flavours? Is there another way to override the versionCode, maybe similar to the way the outputFileName is updated?

3

There are 3 answers

0
Luca Corradi On BEST ANSWER

I think your case fit perfectly with product flavors.

You set a flavor for prod and a flavor for stage. In both set version code as your expression (e.g. 300 + android.defaultConfig.versionCode for prod, and 200 + android.defaultConfig.versionCode for release)

See the doc for detailed examples: https://developer.android.com/studio/build/build-variants

Then, when you want to run or deploy a version you choose between the 4 combinations in Build Variants that will be: prodRelease, prodDebug, stageRelease and stageDebug

0
Sami Kanafani On

You can create different AndroidManifest.xml for each buildType you have, and then set the versionCode in the manifest, but in my personal opinion you should use flavors.

0
Ramesh Yankati On

In your project root build.gradle you could define version code and version Name some thing like this

  def code = project.hasProperty('versionCode') ? versionCode.toInteger() : 
        (System.getenv("VERSION_CODE") as Integer ?: <default-version-code>)

  def name = project.hasProperty('versionName') ? versionName.toString() : 
  (System.getenv("VERSION_NAME") as String ?: "<default-version-name>")

You could provide version code and name runtime in the build command something like this -P = value

 allprojects {
    ext.baseVersionCode = code
    ext.baseVersionName = name    
}

These version code your could access in you all modules.In the app build.gradle

defaultConfig {
    versionCode baseVersionCode
    versionName baseVersionName
    archivesBaseName = "customzied_apk_name"
}