How to Autoincrement Versioncode in Android Gradle

Autoincrement VersionCode with gradle extra properties

I would like to read the versionCode from an external file

I am sure that there are any number of possible solutions; here is one:

android {
compileSdkVersion 18
buildToolsVersion "18.1.0"

def versionPropsFile = file('version.properties')

if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties()

versionProps.load(new FileInputStream(versionPropsFile))

def code = versionProps['VERSION_CODE'].toInteger() + 1

versionProps['VERSION_CODE']=code.toString()
versionProps.store(versionPropsFile.newWriter(), null)

defaultConfig {
versionCode code
versionName "1.1"
minSdkVersion 14
targetSdkVersion 18
}
}
else {
throw new GradleException("Could not read version.properties!")
}

// rest of android block goes here
}

This code expects an existing version.properties file, which you would create by hand before the first build to have VERSION_CODE=8.

This code simply bumps the version code on each build -- you would need to extend the technique to handle your per-flavor version code.

You can see the Versioning sample project that demonstrates this code.

How to autoincrement versionCode in Android Gradle

I have decided for second option - to parse AndroidManifest.xml. Here is working snippet.

task('increaseVersionCode') << {
def manifestFile = file("AndroidManifest.xml")
def pattern = Pattern.compile("versionCode=\"(\\d+)\"")
def manifestText = manifestFile.getText()
def matcher = pattern.matcher(manifestText)
matcher.find()
def versionCode = Integer.parseInt(matcher.group(1))
def manifestContent = matcher.replaceAll("versionCode=\"" + ++versionCode + "\"")
manifestFile.write(manifestContent)
}

tasks.whenTaskAdded { task ->
if (task.name == 'generateReleaseBuildConfig') {
task.dependsOn 'increaseVersionCode'
}
}

versionCode is released for release builds in this case. To increase it for debug builds change task.name equation in task.whenTaskAdded callback.

How do I automatically increment the build id and version number of an Android App using Azure Pipelines

You may check extension Mobile Versioning, which provide a task to update Android Version Gradle:

- task: UpdateAndroidVersionGradle@1
inputs:
buildGradlePath: 'your_project/app/build.gradle'
versionName: '$(MAJOR).$(MINOR).$(PATCH)' # Optional. Default is: $(MAJOR).$(MINOR).$(PATCH)
versionCode: '$(NUMBER_OF_COMMITS)' # Optional. Default is: $(NUMBER_OF_COMMITS)

Detailed documentation you can check the following link:

https://damienaicheh.github.io/azure/devops/2019/12/19/manage-your-application-version-automatically-using-git-and-azure-devops-en

Android how to auto increment version_code when next apk is generated?

never thought about such feature, may be useful... but note that there are some cases when you need to produce release-signed apk just for testing e.g. check some signing or proguard config works well. but still versionCode may be incremented, why not...

note that this feature will edit your code (should change versionCode permanently, for next +1), so for this purpose (if possible) may exist some plugin or script for Android Studio, but gradle itself won't edit own currently-executing build file. you may override this value (versionCode) for release flavor, but this will be fixed still, as every build command will set versionCode=versionCode+1 temporary without knowledge how many builds were made previously

Auto increment version code in Android app

So, I see it like this:

Depending on article that you present, use ant for this tasks (targets?).

  1. parse Manifest (parse XML)
  2. get old version form manifest and increase it/get version from repo
  3. store new version in manifest
  4. build android app.

But im my case I usually fill this field by value based on Tag's revision when I deploy or distribute application.

Auto increment versioncode only on releases

You could try something like that

import java.util.regex.Pattern    
task('increaseVersionCode') << {

... // You could code in your increment system, for eg

// Using build.gradle (recommended)
def buildFile = file("build.gradle")
def pattern = Pattern.compile("versionCode\\s+(\\d+)")
def manifestText = buildFile.getText()
def matcher = pattern.matcher(manifestText)
matcher.find()
def versionCode = Integer.parseInt(matcher.group(1))
def manifestContent = matcher.replaceAll("versionCode " + ++versionCode)
buildFile.write(manifestContent)

// Using manifest
def manifestFile = file('AndroidManifest.xml')
def matcher = Pattern.compile('versionCode=\"(\\d+)\"')
.matcher(manifestFile.getText())
matcher.find()
def manifestContent = matcher.replaceAll('versionCode=\"' +
++Integer.parseInt(matcher.group(1)) + '\"')
manifestFile.write(manifestContent)
}

tasks.whenTaskAdded { task ->
if (task.name == 'assembleQaRelease') {
task.dependsOn 'increaseVersionCode'
}
}

You can adapt the 'assembleQaRelease' to increment version code to the wanted task.

Auto increment version code on Generate signed APK only

I am able to do it by hooking up my code on assembleRelease gradle task. Got help from Tim's blog. Following is the code-

apply plugin: 'com.android.application' 
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

def Properties versionProps = loadVersionFile()
defaultConfig {
applicationId "com.test"
multiDexEnabled true
versionCode getCode(versionProps)
versionName getName(versionProps)
minSdkVersion 18
targetSdkVersion 23
}
signingConfigs {
release {
storeFile file(".../keys.jks")
storePassword "..."
keyAlias "..."
keyPassword "..."
}
}

buildTypes {
release {
signingConfig signingConfigs.release

//minifyEnabled false
//proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
debug {
debuggable true
}
}
dexOptions {
...
}
}

def loadVersionFile() {
def versionPropsFile = file('version.properties')
def Properties versionProps
if (versionPropsFile.canRead()) {

versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))

} else {
throw new GradleException("Could not read version.properties!")
}
return versionProps}

def getCode(Properties versionProps) {
return versionProps['build.version'].toInteger()}

def getName(Properties versionProps) {
return versionProps['product.version']}

assembleRelease << {
def versionPropsFile = file('version.properties')
def code
def Properties versionProps
if (versionPropsFile.canRead()) {
versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
code = versionProps['build.version'].toInteger() + 1
} else {
throw new GradleException("Could not read version.properties!")
}

versionProps['build.version'] = code.toString()
versionProps.store(versionPropsFile.newWriter(), null)
project.android.defaultConfig.versionCode code}

allprojects {
repositories {
jcenter()
mavenCentral()
flatDir {
dirs 'libs'
}

maven {
...
}
}}

dependencies {...}

apply plugin: 'com.google.gms.google-services'


Related Topics



Leave a reply



Submit