Created
June 26, 2014 08:13
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.