I am trying to configure with Gradle a project which contains some external libraries. With Gradle I can setup different Environmental Configuration (with a class inside a config file) for the main application using the Build Variants so I can execute code according to this variables.
The problem is that how I can do the same for a library project? I created this library for this project and I would like to setup different Build Variants for different scenarios.
As an example: In the Library, when running in debug mode, then print all the logs so I can see them while developing. In release mode dont.
Structure of the files:
src ----- > debug -> java -> config -> PlayerEnvConfig
main -> com.mypackagename -> etc...
release -> java -> config -> PlayerEnvConfig
Code in debug: package config;
/**
* Environment configuration for Release
*/
public final class PlayerEnvConfig {
public static final boolean USE_REPORTING = true;
public static final boolean USE_ANALYTICS = true;
public static final boolean USE_LOGGING = false;
public static final boolean USE_DEBUG_LOGGING = false;
public static final boolean USE_DEBUGING = false;
}
Code in release:
package config;
/**
* Environment configuration for Release
*/
public final class PlayerEnvConfig {
public static final boolean USE_REPORTING = true;
public static final boolean USE_ANALYTICS = true;
public static final boolean USE_LOGGING = false;
public static final boolean USE_DEBUG_LOGGING = false;
public static final boolean USE_DEBUGING = false;
}
The problem is that for the main project I can use this Build types to configure differently the application for different scenarios, but how can I do the same for the Library Project?
Because at the moment from what I read in http://tools.android.com/tech-docs/new-build-system/user-guide the library only will use the debug mode while testing.
Any ideas?
Thanks!
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 theBuildConfig.java
generated class.So you could add do something like that in your
build.gradle
:And so have only one
PlayerEnvConfig
withOr even no more
PlayerEnvConfig
and use directly theBuildConfig
class.EDIT Since an update, the syntax has changed :
It's a @bifmadei answer from google code issue and it helps for me:
Try setting this in the dependency project
and this in the project that uses it
Taken from here: https://code.google.com/p/android/issues/detail?id=66805
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.
UPDATE - since the time of this posting, there have been much progress in the gradle build process, thus this answer might not be the recommended best practice and new changes might even brake it. Use your own discretion.
I believe that there is a bit of confusion over the whole project structure and configuration. Let's say that you have the following build.gradle configuration
Your project folder structure should be as follow
FooBar.java
must not be in theprooject_root/src/main/java/com/example
. It must be indebug
andrelease
folder which is to reside outsidesrc
folder but insidebuild-types
folder. That is configured bysetRoot('build-types/*****')
method. A lot of people get confused from seeing 'debug/java' and 'main/java', where the later is referenced in a manner 'src/main/java' from presentation and end up putting 'debug/java' in src, the wrong folder. I hope this help.For more complex environment involving other libraries, you can check out my answer here https://stackoverflow.com/a/19918834/319058
As Scott points out, this is a known shortcoming of Gradle. As a workaround, you can use this method, which uses reflection to get the field value from the app (not the library):
To get the
DEBUG
field, for example, just call this from yourActivity
:I have also shared this solution on the AOSP Issue Tracker.
This is not possible with library projects as you yourself mentioned.
You could just change the library project to an application project. That seems to work fine (at least in theory, I haven't tested it myself).
The other way would be to override that class in your application project. Your application project class will be chosen when merging takes place and you'll have those values available.