Here is a modernization of my previous answer, which can be seen below. This one works with Gradle 4.4 and Android Studio 3.1.1 .
What this script does:
- Creates a version.properties file if it doesnβt exist (Paul Cantrellβs voice below, this is where I got the idea from, if you like this answer)
- For each build, debug version, or any click of the start button in Android Studio, the number of VERSION_BUILD increases.
- Each time you build a release, your Android versionCode for the game store increases and the number of your patch increases.
- Bonus: after the build is complete, copy your apk to
projectDir/apk to make it more accessible.
This script will create a version number that looks like v1.3.4 (123) and create an apk file such as AppName-v1.3.4.apk .
Major version β β Build version v1.3.4 (123) Minor version β|β Patch version
Basic version: must be manually modified for large changes.
Minor version: must be manually modified for slightly less significant changes.
Patch version: increases when running gradle assembleRelease
Build Version: Increases Each Build
Version number: The same as the version of the patch, this is for the version code, which the Play Store should increase for each new apk download.
Just change the content in the comments marked 1 - 3 below, and the script should do the rest. :)
android { compileSdkVersion 27 buildToolsVersion '27.0.3' def versionPropsFile = file('version.properties') def value = 0 Properties versionProps = new Properties() if (!versionPropsFile.exists()) { versionProps['VERSION_PATCH'] = "0" versionProps['VERSION_NUMBER'] = "0" versionProps['VERSION_BUILD'] = "-1" // I set it to minus one so the first build is 0 which isn't super important. versionProps.store(versionPropsFile.newWriter(), null) } def runTasks = gradle.startParameter.taskNames if ('assembleRelease' in runTasks) { value = 1 } def mVersionName = "" def mFileName = "" if (versionPropsFile.canRead()) { versionProps.load(new FileInputStream(versionPropsFile)) versionProps['VERSION_PATCH'] = (versionProps['VERSION_PATCH'].toInteger() + value).toString() versionProps['VERSION_NUMBER'] = (versionProps['VERSION_NUMBER'].toInteger() + value).toString() versionProps['VERSION_BUILD'] = (versionProps['VERSION_BUILD'].toInteger() + 1).toString() versionProps.store(versionPropsFile.newWriter(), null) // 1: change major and minor version here mVersionName = "v1.0.${versionProps['VERSION_PATCH']}" // 2: change AppName for your app name mFileName = "AppName-${mVersionName}.apk" defaultConfig { minSdkVersion 21 targetSdkVersion 27 applicationId "com.example.appname" // 3: change to your package name versionCode versionProps['VERSION_NUMBER'].toInteger() versionName "${mVersionName} Build: ${versionProps['VERSION_BUILD']}" } } else { throw new FileNotFoundException("Could not read version.properties!") } if ('assembleRelease' in runTasks) { applicationVariants.all { variant -> variant.outputs.all { output -> if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) { outputFileName = mFileName } } } } task copyApkFiles(type: Copy){ from 'build/outputs/apk/release' into '../apk' include mFileName } afterEvaluate { assembleRelease.doLast { tasks.copyApkFiles.execute() } } signingConfigs { ... } buildTypes { ... } }
=================================================== ==
INITIAL RESPONSE:
I want versionName to increment automatically as well. So this is just a complement to CommonsWare's answer, which worked perfectly for me. This is what works for me
defaultConfig { versionCode code versionName "1.1." + code minSdkVersion 14 targetSdkVersion 18 }
EDIT:
Since I'm a little lazy, I want my versions to work as automatically as possible. I want to have a version of the assembly that increases with each assembly, while the version number and version name only increase when I build the release.
This is what I used over the past year, the basics are taken from CommonsWare's answer and my previous answer, as well as some others. This leads to the following version:
Version name: 1.0.5 (123) β Major.Minor.Patch (Build), Major and Minor are manually changed.
In build.gradle:
... android { compileSdkVersion 23 buildToolsVersion '23.0.1' def versionPropsFile = file('version.properties') if (versionPropsFile.canRead()) { def Properties versionProps = new Properties() versionProps.load(new FileInputStream(versionPropsFile)) def value = 0 def runTasks = gradle.startParameter.taskNames if ('assemble' in runTasks || 'assembleRelease' in runTasks || 'aR' in runTasks) { value = 1; } def versionMajor = 1 def versionMinor = 0 def versionPatch = versionProps['VERSION_PATCH'].toInteger() + value def versionBuild = versionProps['VERSION_BUILD'].toInteger() + 1 def versionNumber = versionProps['VERSION_NUMBER'].toInteger() + value versionProps['VERSION_PATCH'] = versionPatch.toString() versionProps['VERSION_BUILD'] = versionBuild.toString() versionProps['VERSION_NUMBER'] = versionNumber.toString() versionProps.store(versionPropsFile.newWriter(), null) defaultConfig { versionCode versionNumber versionName "${versionMajor}.${versionMinor}.${versionPatch} (${versionBuild}) Release" minSdkVersion 14 targetSdkVersion 23 } applicationVariants.all { variant -> variant.outputs.each { output -> def fileNaming = "apk/RELEASES" variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { output.outputFile = new File(getProject().getRootDir(), "${fileNaming}-${versionMajor}.${versionMinor}.${versionPatch}-${outputFile.name}") } } } } } else { throw new GradleException("Could not read version.properties!") } ... } ...
Patch and versionCode increase if you build your project through the terminal using " assembly", "assemblyRelease " or "aR", which creates a new folder in the root of your project called apk / RELEASE, so you do not need to view build / output / more / more / more to find your apk.
The properties of your version should look like this:
VERSION_NUMBER=1 VERSION_BUILD=645 VERSION_PATCH=1
Obviously start with 0. :)