Skip to content

Instantly share code, notes, and snippets.

@almozavr
Created June 26, 2014 08:13
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save almozavr/d59e770d2a6386061fcb to your computer and use it in GitHub Desktop.
Save almozavr/d59e770d2a6386061fcb to your computer and use it in GitHub Desktop.
Workaround to bypass library's BuildConfig.DEBUG (always true, always release build type) via custom variable
// Application
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0 alpha"
rootProject.ext.variantRelease = false //default we are in debug mode, will be overriden on task execution
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
debug {
versionNameSuffix ' dev'
}
}
}
dependencies {
// ui
compile 'com.android.support:support-v4:+'
// local jars
compile fileTree(dir: 'libs', include: ['*.jar'])
}
// Trigger build type (as soon as possible) and make some action via corresponding tasks
project.afterEvaluate {
tasks.all { task ->
if (task.name =~ /check.*Manifest/) {
if (task.name =~ /[dD]ebug/) {
task.dependsOn(onDebugTypeTriggered)
} else {
task.dependsOn(onReleaseTypeTriggered)
}
}
}
}
task onDebugTypeTriggered << {
rootProject.ext.variantRelease = false
}
task onReleaseTypeTriggered << {
rootProject.ext.variantRelease = true
}
// Library
apply plugin: 'android-library'
repositories {
mavenCentral()
}
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
}
}
// Trigger BuildConfig creation
project.afterEvaluate {
tasks.all { task ->
if (task.name =~ /generate.*BuildConfig/) {
task.dependsOn(propagateBuildConfig)
}
}
}
task propagateBuildConfig << {
project.android.buildTypes.all { type ->
type.buildConfigField "boolean", "RELEASE", isVariantRelease().toString()
}
}
def isVariantRelease() {
return rootProject.ext.variantRelease
}
@theojalba
Copy link

This works only if you are satisfied doing one build type at a time /w clean. As soon as you try to compile 2 or more builds / flavors at once Gradle will cache the first "release" compilation of the subproject and use it for every subsequent build type. So if you compile debug and release at once using (gradlew clean assemble), then the debug version of the project will contain the release version of the library. This happens because when you compile the debug and release builds of the main project, the build type of the subproject remains the same (namely "release"), and Gradle feels it doesn't need to recompile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment