How to make sure that with each Generate Signed APK ... the versionCode is automatically incremented to not do it manually .. simply, sometimes you forget about it and download the version with the old versionCode (which is not acceptable!) On Google Play Console

android:versionCode="X" 

    2 answers 2

    Solution 1

     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 } 

    Solution 2 (use time)

     def date = new Date() def formattedDate = date.format('yyMMddHHmm') def code = formattedDate.toInteger() defaultConfig { minSdkVersion 10 targetSdkVersion 21 versionCode code } 
    • TimurVI! Thank! Well, you're a genius !!! ... Sorry, but where to stick it? (Decision 2, approximately, understood, and the first?) - user_MGU
    • @user: Unfortunately, the decisions are not mine. (there are links in the answer) I am a bit far from a genius. :) - TimurVI
    • in the first script it will always be incremented (run build.gradle), output a separate task, and wrap it in a check for signRelease, well, or just +1 do it only when the Signed APK build starts. The second will be overwritten with each build.gradle. It is strange that the answer sounds like a question. - Shwarz Andrei

    I do differently. The idea is based on the use of the version number from the VCS.

    In the case of git, do this:

     //извлечение порядкового номера версии стартуя от HEAD def getVersionCode = { -> try { def stdout = new ByteArrayOutputStream() exec { standardOutput = stdout commandLine 'git', 'rev-list', '--first-parent', '--count', 'master' } println("Build #"+stdout) return asInteger(stdout.toString("ASCII").trim()) } catch (Exception e) { e.printStackTrace(); return 0; } } //присваиваем версию def char[] patches='abcdefghijklmnopqrstuwxyz'.toCharArray() def patch=1 //иногда бывает нужно определить патч внутри версии def majorVersion=1 def minorVersion=0 def revision=getVersionCode() defaultConfig { //blah-blah versionName = majorVersion + '.' + minorVersion + '.' + revision + patches[patch] versionCode = 10000000*majorVersion+10000*minorVersion + 10*revision+patch } 

    At the output we get something like:

     versionName="1.0.25a" versionCode="10000251" 

    Similar scripts can be written for SVN

    Update

    The incrementing of the version will be at every commit . Separately, you can also write githash in order to restore which version dropped and where by logs. When there are a lot of users, not all upgrade versions in time - an extremely useful thing, I tell you. Several times really rescued.

    • it's a good idea to do something like this just not in the Gradle parsim, only in the signingConfigs in release to push. Well, or a question to change. For Generate Signed APK - Shwarz Andrei
    • I agree, but for some reason I’m closer to the idea of ​​increment at each commit - Barmaley