Skip to content

Instantly share code, notes, and snippets.

@AntonKosov
Last active January 6, 2018 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AntonKosov/24507685b8035bd97ddc to your computer and use it in GitHub Desktop.
Save AntonKosov/24507685b8035bd97ddc to your computer and use it in GitHub Desktop.
android + gradle + idea + git + private keystore
// Tags:
// Version name: ignore leading non-digits and everything after the first underscore
// Version code: take digits after the first underscore
// e.g. tag xxx1.2.3.foo-bar_15_baz will yield versionName 1.2.3.foo-bar and versionCode 15
// Output filename:
// <project name>-<release|debug>-<version (from last tag)|"dev" (doesn't have tag)>[-NOTRELEASE (exist changes after last tag)]-<last short commit hash>[-dirty (exist changes)].apk
// Sample:
// myproject-debug-dev-094cf50.apk
// myproject-debug-0.1.0-094cf50.apk
// myproject-debug-0.1.0-NOTRELEASE-094cf50-dirty.apk
// myproject-release-0.1.0-094cf50.apk
// File keystore.properties:
// storePassword=...
// keyPassword=...
// keyAlias=...
// storeFile=...
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.14.2'
}
}
apply plugin: "com.android.application"
dependencies {
//
}
android {
def gitTag = "git --git-dir=$rootDir/.git describe --tags".execute().text.trim()
def gitHash = "git --git-dir=$rootDir/.git rev-parse --short HEAD".execute().text.trim()
def hasModifiedDeletedOrOtherFiles = !"git --git-dir=$rootDir/.git ls-files -mdo --exclude-standard".execute().text.trim().isEmpty()
def hasStagedFiles = !"git --git-dir=$rootDir/.git diff-index --no-ext-diff --name-only --cached HEAD".execute().text.trim().isEmpty()
def isNotTag = "git --git-dir=$rootDir/.git tag -l --contains HEAD".execute().text.trim().isEmpty()
def dirtyWorkingCopy = hasModifiedDeletedOrOtherFiles || hasStagedFiles
def gitDescription = dirtyWorkingCopy ? "${gitHash}-dirty" : gitHash
def versionNameDescription = isNotTag ? "-NOTRELEASE" : ""
def versionMatcher = gitTag =~ /^[^0-9]*([^_]*)(_([0-9]*))?/
def versionNameFromTag = (versionMatcher[0][1] ?: "dev") + versionNameDescription
def versionCodeFromTag = versionMatcher[0][3]?.toInteger() ?: 1
defaultConfig {
versionName versionNameFromTag
versionCode versionCodeFromTag
minSdkVersion 14
targetSdkVersion 19
}
buildToolsVersion "19.1.0"
compileSdkVersion 19
signingConfigs {
debug {
storeFile file("debug.keystore")
storePassword "android"
keyAlias "androiddebugkey"
keyPassword "android"
}
release {
def propsFile = file('keystore.properties')
def props = new Properties()
props.load(new FileInputStream(propsFile))
storeFile file(props['storeFile'])
storePassword props['storePassword']
keyAlias props['keyAlias']
keyPassword props['keyPassword']
}
}
sourceSets {
main {
manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/java']
aidl.srcDirs = ['src/main/src']
renderscript.srcDirs = ['src/main/java']
res.srcDirs = ['src/main/res']
}
instrumentTest.setRoot('tests')
}
buildTypes {
release {
signingConfig android.signingConfigs.release
minifyEnabled = true
proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
proguardFile 'proguard-project.txt'
}
}
applicationVariants.all { variant ->
def apkSuffix = ""
if (variant.buildType.minifyEnabled) {
// only release version
apkSuffix = "-${versionName}-${gitDescription}"
}
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = outputFile.name.replace('.apk', "${apkSuffix}.apk")
output.outputFile = new File(outputFile.parent, fileName)
// print the APK path after assemble is done
output.assemble.doLast {
println output.outputFile
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment