Friday, March 25, 2016

Android Gradle: How to rename apk depending on build type

1. Add the code below to your project app/build.gradle:
android {
    signingConfigs {
        release {
           .....
        }
    }
    def jenkinsBuild = System.getenv("BUILD_NUMBER") ?: "10"   
    defaultConfig {
        .....
        versionCode jenkinsBuild.toInteger()
        versionName "2.0.0."+ versionCode    }
    buildTypes {
        release {
           ...
        }
        qa {
            ..
        }
        debug {
           ...
        }
    }
    applicationVariants.all { variant ->
        if (variant.buildType.name == 'release') {
            variant.mergedFlavor.versionName = android.defaultConfig.versionName;
        }
        if (variant.buildType.name == 'qa') {
            variant.mergedFlavor.versionName = android.defaultConfig.versionName;
        }
        if (variant.buildType.name == 'debug') {
            variant.mergedFlavor.versionName = android.defaultConfig.versionName;
        }
    }
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.outputFile = new File( output.outputFile.parent,output.outputFile.name.replace(".apk", "-${variant.versionName}.apk"))
        }
    }
   ..
}


2. $gradlew build
It will generate all  the apk's for build types (shown below).


Now when you push apps from Jenkins to HockeyApp the 'apk name' will help you identify the build of the apk :)

No comments:

Post a Comment