Skip to content

Instantly share code, notes, and snippets.

@meeDamian
Last active May 10, 2017 17:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save meeDamian/2eba95921a6f2f5d25b0 to your computer and use it in GitHub Desktop.
Save meeDamian/2eba95921a6f2f5d25b0 to your computer and use it in GitHub Desktop.
# PROJECT `.gitignore`
# put it in the *root* of your project
# most likely just append this line to the already existing file
/secret.properties
// MODULE `build.gradle`
apply plugin: 'com.android.application'
// read secret variables
// IMPORTANT: must be above `android {}` declaration
File secretPropsFile = file('../secret.properties')
if (secretPropsFile.exists()) {
Properties p = new Properties()
p.load(new FileInputStream(secretPropsFile))
p.each { name, value ->
safeLoad name as String, value
}
}
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.yourPackage.someMore"
minSdkVersion 15 // because #minSDK15
targetSdkVersion 22
versionCode getBuildVersion(1)
versionName "0.0.1"
}
// I usually use that, but you can safely remove that
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
signingConfigs {
release {
storeFile safeGetFile('STORE_FILE'))
storePassword safeGet('STORE_PASSWORD')
keyAlias safeGet('KEY_ALIAS')
keyPassword safeGet('KEY_PASSWORD', safeGet('STORE_PASSWORD')) // notice how STORE_PASSWORD is used as a fallback
}
}
buildTypes {
release {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release as Object
minifyEnabled true
zipAlignEnabled true
File releasesDir = new File(
safeGet('RELEASES_PARENT_DIR', '~/APKs'),
safeGet('FOLDER_NAME', project.group as String)
)
if (!releasesDir.exists())
releasesDir.mkdirs()
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
if (output.name == "release") {
/* base file name in a form of:
[package]-[versionType]-[versionName]-[versionCode]
ex. com.meedamian.testApp-release-1.0.0-1111
*/
String fileName = [
defaultConfig.applicationId,
project.name,
defaultConfig.versionName,
getBuildVersion(android.defaultConfig.versionCode, true)
].join('-')
// rename output APK
output.outputFile = new File(releasesDir, fileName + '.apk')
// copy mappings.txt (JIC)
if (variant.getBuildType().isMinifyEnabled()) {
File mappingDir = new File(releasesDir, 'mappings')
if (!mappingDir.exists())
mappingDir.mkdirs()
assemble << {
copy {
from variant.mappingFile
into mappingDir
rename 'mapping.txt', "mapping-${fileName}.txt"
}
}
}
}
}
}
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// add your dependencies, ex:
compile 'com.google.android.gms:play-services-base:7.0.0'
}
// get variables && prevent crashing if missing
String safeGet(String name, String defaultValue = '') {
hasProperty(name) ? project[name] : defaultValue
}
File safeGetFile(String name) {
String fileName = safeGet(name, null)
fileName != null ? file(fileName) : null
}
// loads variables from a file to `project` so they can be `safeGet`-ed later
def safeLoad(String name, Object value, Boolean override = false) {
if (!hasProperty(name) || override)
project.set name, value
}
Integer getBuildVersion(defaultVersion, Boolean increment = false) {
File verFile = file('../version.properties')
if (!verFile.canRead())
verFile.createNewFile()
Properties props = new Properties()
props.load new FileInputStream(verFile)
String currentCodeVersion = props['VERSION_CODE']
if (currentCodeVersion == null)
currentCodeVersion = defaultVersion ?: android.defaultConfig.versionCode
if (increment) {
Integer bumpedCodeVersion = currentCodeVersion.toInteger() + 1
android.defaultConfig.versionCode = bumpedCodeVersion
props['VERSION_CODE'] = bumpedCodeVersion.toString()
props.store verFile.newWriter(), "Build version updated with each release build"
currentCodeVersion = bumpedCodeVersion
}
currentCodeVersion as Integer
}
# GLOBAL `gradle.properties`
# `~/.gradle/gradle.properties`
# 1. remove 'GLOBAL_' prefix from the name and
# 2. put it into ~/.gradle/ directory.
# for builds
RELEASES_PARENT_DIR=/Users/meeDamian/APKs
# for signing
STORE_FILE=/Users/meeDamian/keys/appSigningKey
STORE_PASSWORD=SuperSecretPasswordMickyGoofieConcretBananas
# PROJECT `gradle.properties`
# put it in the *root* of your project.
# if not provided Project name will be used
FOLDER_NAME=projectName
# you have to provide that one
KEY_ALIAS=keyName
# PROJECT `secret.properties`
# if not provided store password will be used
KEY_PASSWORD=AnotherSuperSecretPassword
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment