Dhrupal Dhrupal - 4 months ago 41
Android Question

Auto increment version code on Generate signed APK only

I have successfully incremented version code. But I only want to increment it while I do generate signed apk from menu Build -> Generate signed APK. Following is my gradle code.

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
def versionPropsFile = file('version.properties')

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

versionProps.load(new FileInputStream(versionPropsFile))

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

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

defaultConfig {
applicationId "com.test"
multiDexEnabled true
versionCode code
versionName "1.1"
minSdkVersion 18
targetSdkVersion 23
}
}
else {
throw new GradleException("Could not read version.properties!")
}

buildTypes {
release {
//minifyEnabled false
//proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
debug {
debuggable true
}
}
dexOptions {
incremental true
javaMaxHeapSize "4g"
preDexLibraries = false
}}


And my version.properties file contains following info:
Minor=7
Branch=4
Major=10
build.version=73

Answer

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'