Skip to content

Instantly share code, notes, and snippets.

@jokester
Last active March 20, 2018 06:15
Show Gist options
  • Save jokester/48043f35acdf1dafe090b899cfa7b85e to your computer and use it in GitHub Desktop.
Save jokester/48043f35acdf1dafe090b899cfa7b85e to your computer and use it in GitHub Desktop.
Android build settings that works in both IDE and CI server

What is this for

  • Auto-generated version number for release builds
  • No need to include keystore / password in repository
  • The code should still build with no extra configuration

How it works

  • Version code / number are generated from the commit itself
  • Version / sign settings are injected as environment variables
  • When these settings are not present, failback to default ones silently
// read env variable as string or integer
def env = { System.getenv it }
def envInt = { Integer.parseInt(env(it)) }
android {
defaultConfig {
if (env("ANDROID_VERSION_CODE")) {
versionCode envInt("ANDROID_VERSION_CODE")
versionName env("ANDROID_VERSION_NAME")
} else {
// failback versions, so we don't have to set env-varables in IDE.
versionCode 1
versionName "1.0"
}
}
}
if (env("ANDROID_SIGN_APK"))
// only apply when setted
android {
signingConfigs {
releaseSigning {
storeFile file(env("ANDROID_KEYSTORE_PATH"))
storePassword env("ANDROID_KEYSTORE_PASS")
keyAlias env("ANDROID_KEY_ALIAS")
keyPassword env("ANDROID_KEY_PASS")
}
}
buildTypes {
release {
signingConfig signingConfigs.releaseSigning
}
}
}
#!/bin/bash
# sets (local-specific settings / sign settings / computed attributes) on a CI server (I'm using jenkins)
# DO NOT commit this file and sign keys into repository
# JDK & SDK
export ANDROID_HOME=/somewhere/android-sdk-linux
export JAVA_HOME=/somewhere/jdk1.8.0_65
# SIGN
export ANDROID_KEYSTORE_PATH=/somewhere/keys.jks
export ANDROID_SIGN_APK=yes
export ANDROID_KEYSTORE_PASS=secret
export ANDROID_KEY_ALIAS=secret
export ANDROID_KEY_PASS=secret
# versionCode: number of commits that are reachable from here
# *supposedly* monotonically increasing for each release build
export ANDROID_VERSION_CODE="$(git rev-list HEAD --count)"
# versionName: first 8 chars in commit SHA1
export ANDROID_VERSION_NAME="${GIT_COMMIT:0:8}"
exec sh gradlew clean aR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment