Override android gradle versionCode form command

I have a build.gradle file in my Android application with the following settings:

android { ... defaultConfig { applicationId "some.app.id" versionName '1.2' versionCode 3 ... } ... } 

My AndroidManifest.xml does not contain versionCode and does not contain versionName.

Now I want to create this application on Jenkins and pass BUILD_NUMBER as the version for the application, so each assembly has a higher version.

So, in the task, I call:

 ./gradlew -PversionCode=$BUILD_NUMBER clean build 

When I use "versionCode" to rename "app-release.apk", the versionCode value matches the one passed from the command line:

 applicationVariants.all { variant -> variant.outputs.each { output -> output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace("app-release.apk", "MyApp_" + "_v" + versionName + "." + versionCode + ".apk")) } } 

Summary

So, I have a default value of "versionCode" equal to 3, but when creating Jenkins I want to override it from the command line.

Problem

The problem is that in AndroidManifest inside the build.apk app has a versionCode value of 3 instead of the value from BUILD_NUMBER. I checked it with "aap dump badging"

Question

Can this value "versionCode" from android defaultConfig be overridden by a command line parameter?

I know I could use the function as described in: http://robertomurray.co.uk/blog/2013/gradle-android-inject-version-code-from-command-line-parameter/ but I prefer a simpler a way to simply override, but I can't get it to work.

+5
source share
1 answer

You can use properties by defining them in the gradle.properties file, see gradle documentation . But you have to be very careful what names you use for your properties: if you use versionName, then the property will have the correct value in gradle (you can println it), but it will not appear in your AndroidManifest.xml! So I decided to use "versName". (I don’t know enough about gradle to understand why this is so ...)

So, in your gradle.properties project you can define the following properties: versCode=3 versName=1.2

And then change the build.gradle file to:

 android { ... defaultConfig { applicationId "some.app.id" versionName versName versionCode versCode as Integer ... } ... } 

Now you can override them on the command line as follows: ./gradlew -PversCode=4 -PversName=2.1.3 clean build

+9
source

All Articles