Skip to content

Instantly share code, notes, and snippets.

@jordanbeck
Last active December 31, 2015 01:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordanbeck/7915008 to your computer and use it in GitHub Desktop.
Save jordanbeck/7915008 to your computer and use it in GitHub Desktop.
Attempting to update versionName and versionCode for gradle buildTypes [and currently failing]

This currently does not work the way I want, but I want to document it for personal reference.

Scenario:

I have an app that uses a library project being build in parallel. I want to have three different build types: debug, internal, release. I want each of those to have it's own package name, app name, version number, and version code. I want to be able to at least automate a build for internal on a nightly basis and upload to HockeyApp.

Problems:

  • First problem I ran into was that buildTypes in gradle do not allow you to specify versionNumber or versionCode. Just suffixes for those properties. So I moved to flavors...
  • Unfortunately, I'm using BuildConfig and with flavors, BuildConfig.java is not found by Android Studios. Everything does work this way (I can run gradle commands from the command line just fine), but without my IDE, it's kind of hard to work. Also, a downside to this approach is that it produces too many apks. If I have the three desired build types and have to have three flavors, I end up with nine apks. Not a show stopper, but kind of annoying.
  • So I removed the flavors, and tried to manually update the AndroidManifest in the build directory. To do this, I call variant.processManifest.doLast for each application variant (see code) and try to modify the file directly. This would at least allow me to inject the versionName and code. Unfortunately, this procedure does not happen at the correct time and the manifest would lose some other change (i.e.: the adding of a packageNameSuffix would no longer be applied)
  • I tried several other [task].doLast or [task].doFirst, but each resulted in an incorrect manifest.

That's where I'm currently at. All of this could have been prevented if I could just set versionName and versionCode in buildTypes, but that's not supported as of now.

The files below are what I currently have and I will update them as I figure things out.

apply plugin: 'android'
repositories {
mavenCentral()
}
android {
final CLIENT_ID = "[client-id]"
final CLIENT_SECRET = "[client-secret]"
compileSdkVersion rootProject.compileSdkVersion
buildToolsVersion rootProject.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.minSdkVersion
targetSdkVersion rootProject.targetSdkVersion
}
signingConfigs {
internal {
storeFile file("app.keystore")
storePassword System.getenv("KEYSTORE_PASSWORD")
keyAlias "app_internal"
keyPassword System.getenv("KEY_PASSWORD")
}
}
buildTypes {
debug {
packageNameSuffix ".debug"
buildConfig "public static final boolean BETA = false;" + "\n\t" +
"public static final boolean DEPLOYED = false;" + "\n\t" +
"public static final String CLIENT_ID = \"" + CLIENT_ID + "\";" + "\n\t" +
"public static final String CLIENT_SECRET = \"" + CLIENT_SECRET + "\";" + "\n\t" +
"public static final String AUTHORITY = \"com.fake.debug.provider\";"
}
internal {
packageNameSuffix ".internal"
signingConfig signingConfigs.internal
buildConfig "public static final boolean BETA = true;" + "\n\t" +
"public static final boolean DEPLOYED = true;" + "\n\t" +
"public static final String CLIENT_ID = \"" + CLIENT_ID + "\";" + "\n\t" +
"public static final String CLIENT_SECRET = \"" + CLIENT_SECRET + "\";" + "\n\t" +
"public static final String AUTHORITY = \"com.fake.internal.provider\";"
}
release {
buildConfig "public static final boolean BETA = false;" + "\n\t" +
"public static final boolean DEPLOYED = true;" + "\n\t" +
"public static final String CLIENT_ID = \"" + CLIENT_ID + "\";" + "\n\t" +
"public static final String CLIENT_SECRET = \"" + CLIENT_SECRET + "\";" + "\n\t" +
"public static final String AUTHORITY = \"com.fake.provider\";"
}
}
}
android.applicationVariants.all { variant ->
variant.processManifest.doLast {
copy {
def props = new Properties()
props.load(new FileInputStream("${buildDir}/../configs/${variant.dirName}/config.properties"))
/*from "${buildDir}/../src/${variant.dirName}/AndroidManifest.xml"
into "${buildDir}/../src/${variant.dirName}"
from "${buildDir}/manifests/${variant.dirName}/AndroidManifest.xml"
into "${buildDir}/manifests/${variant.dirName}"*/
from("${buildDir}/manifests") {
include "${variant.dirName}/AndroidManifest.xml"
}
into("${buildDir}/filtered_manifests")
if (props.versionCode != null) {
println "Updating versionCode to " + props.versionCode
filter { String line ->
line.replaceAll("android:versionCode=\"1\"", "android:versionCode=\"${props.versionCode}\"")
}
}
if (props.versionName != null) {
println "Updating versionName to " + props.versionName
filter { String line ->
line.replaceAll("android:versionName=\"1.0\"", "android:versionName=\"${props.versionName}\"")
}
}
}
}
/*variant.processResources.manifestFile = file("${buildDir}/manifests/${variant.dirName}/AndroidManifest.xml")*/
variant.processResources.manifestFile = file("build/filtered_manifests/${variant.dirName}/AndroidManifest.xml")
}
dependencies {
compile "com.android.support:support-v4:18.0.+"
compile files('libs/HockeySDK-3.0.1.jar')
compile project(':AppLibrary')
}
# Internal config properties : [module]/configs/internal/config.properties
versionCode=5
versionName=1.0.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment