Build variants in Gradle for a Library Project in Android Build variants in Gradle for a Library Project in Android android android

Build variants in Gradle for a Library Project in Android


It's a @bifmadei answer from google code issue and it helps for me:

Obsolete:Try setting this in the dependency project

android {    publishNonDefault true    ...}

Update: Starting from gradle 4.10.1 publishNonDefault is true by default. So just use the recommendation below:

Include this in the project that uses it

dependencies {    releaseCompile project(path: ':theotherproject', configuration: 'release')    debugCompile project(path: ':theotherproject', configuration: 'debug')}

Taken from here: https://code.google.com/p/android/issues/detail?id=66805

New approach:Note that with implementation instruction separated releaseCompile and debugCompile become obsolete: https://stackoverflow.com/a/44364851/3379437

But this approach still can be used with a custom build configuration


Not sure what's wrong with your configuration but about your need, I would do it differently.

In the gradle build file you can use the buildConfig keyword to add a specific line to the BuildConfig.java generated class.

So you could add do something like that in your build.gradle :

    release {        buildConfig "public static final String USE_REPORTING = true;"    }    debug {        buildConfig "public static final String USE_REPORTING = false;"    }

And so have only one PlayerEnvConfig with

public static final boolean USE_REPORTING = BuildConfig.USE_REPORTING;

Or even no more PlayerEnvConfig and use directly the BuildConfig class.


EDIT Since an update, the syntax has changed :

buildConfigField "<type>", "<name>", "<value>"


This is documented in https://code.google.com/p/android/issues/detail?id=52962 . As you've found out, the build type isn't propagated to library projects, and there isn't a good workaround. If you have control over the code of the library project, you could make the debug status a mutable global variable, and set it from your main application on startup. It's a bit of a hack, and it has the disadvantage that the compiler can't optimize the unused code paths away from release builds, but unless something unusual is going on it should work.