Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save gianpaolof/fc62abd365be874b73e0740bb2728dd2 to your computer and use it in GitHub Desktop.

Select an option

Save gianpaolof/fc62abd365be874b73e0740bb2728dd2 to your computer and use it in GitHub Desktop.
Useful tricks for Android gradle files
def VERSION_NAME = "1.0"
def buildTime() {
new Date().format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC")
}
android {
buildTypes {
release {
versionName VERSION_NAME
// and/or
buildConfigField "String", "BUILD_TIME", "\"\""
}
debug {
versionName VERSION_NAME + "-" + buildTime()
// and/or
buildConfigField "String", "BUILD_TIME", "\"${buildTime()}\""
}
}
}
task ci_job {
group = 'ci'
}
if (project.hasProperty('buildVariant') && !project.property('buildVariant').isEmpty()) {
def checkVariant = '[RegularRelease]'
def testVariant = '[MockDebug]'
project.afterEvaluate {
/* Assemble */
def assemble = tasks["assemble${buildVariant}"]
assemble.mustRunAfter clean
/* Unit test */
def unitTests = project.tasks["test${testVariant}UnitTest"]
unitTests.mustRunAfter assemble
unitTests.onlyIf {
!project.hasProperty('skipUnitTest') || project.property('skipUnitTest') != 'true'
}
/* Quality */
task quality
quality.dependsOn project.tasks["lint${checkVariant}"]
quality.dependsOn project.tasks["findbugs${checkVariant}"]
quality.dependsOn project.tasks["checkstyle"]
quality.onlyIf {
!project.hasProperty('skipQuality') || project.property('skipQuality') != 'true'
}
/* Execute all */
ci_job.dependsOn clean
ci_job.dependsOn assemble
ci_job.dependsOn unitTests
ci_job.dependsOn quality
}
} else {
ci_job << {
println "Nothing to do, buildVariant is not wel defined"
}
}
ext.versions = [
dagger : '2.7',
rxAndroid : '1.2.1',
rxJava : '1.1.6',
// etc...
]
ext.deps = [
// Dependency injection
dagger : "com.google.dagger:dagger:${versions.dagger}",
daggerCompiler : "com.google.dagger:dagger-compiler:${versions.dagger}",
// RxJava
rxAndroid : "io.reactivex:rxandroid:${versions.rxAndroid}",
rxJava : "io.reactivex:rxjava:${versions.rxJava}",
rxJavaProguard : "com.artemzin.rxjava:proguard-rules:${versions.rxJava}.0",
]
dependencies {
// Dependency injection
compile deps.dagger
apt deps.daggerCompiler
// RxJava
compile deps.rxAndroid
compile deps.rxJava
compile deps.rxJavaProguard
}
def VERSION_NAME = "1.0"
def gitVersion() {
def cmd = 'git rev-list HEAD --first-parent --count'
cmd.execute().text.trim().toInteger()
}
android {
buildTypes {
release {
versionName VERSION_NAME
// and/or
buildConfigField "String", "GIT_SHA", "\"\""
}
debug {
versionName VERSION_NAME + "-" + gitVersion()
// and/or
buildConfigField "String", "GIT_SHA", "\"${gitVersion()}\""
}
}
}
// Trick 1
allprojects {
ext {
isCIBuild = System.getenv('BUILD_ID')
}
}
// Trick 2
allprojects {
ext {
isIdeBuild = project.properties['android.injected.invoked.from.ide'] == 'true'
}
}
signingConfigs {
release {
storeFile "${System.env.MYAPP_PRIVATE_KEY}"
keyAlias "${System.env.MYAPP_ALIAS}"
storePassword "${System.env.MYAPP_STORE_PASSWORD}"
keyPassword "${System.env.MYAPP_PASSWORD}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment