Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build variants in Gradle for a Library Project in Android

Tags:

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!

like image 905
Javier Tarazaga Avatar asked Jul 03 '13 15:07

Javier Tarazaga


People also ask

What are build variants in Android?

Build variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors. Although you do not configure build variants directly, you do configure the build types and product flavors that form them.

What is build type in gradle Android?

A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.

Where is build variants in Android Studio?

To change the build variant Android Studio uses, select Build > Select Build Variant in the menu bar. For projects without native/C++ code, the Build Variants panel has two columns: Module and Active Build Variant.

What is variant in gradle?

1. What Are Build Variants? Build variants are specific builds that you can produce from Gradle, based around shared core source code. While a standard app may have a debug and release build type, you can expand on this by adding flavor dimensions.

What is a Gradle build variant?

Build variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors. Although you do not configure build variants directly, you do configure the build types and product flavors that form them.

What does the Android plugin for Gradle do?

The Android plugin for Gradle provides a useful Gradle task that shows you how to organize your files for each of your build types, product flavors, and build variants. For example, the following sample from the task output describes where Gradle expects to find certain files for the "debug" build type:

How to build a Java library using Gradle?

You now have the project setup to build a Java library. To build the project, run the build task. You can use the regular gradle command, but when a project includes a wrapper script, it is considered good form to use it instead.

Where should I organize my Gradle files?

For example, Gradle expects Java class files that are specific to your "debug" build type to be located in the src/debug/java/ directory. The Android plugin for Gradle provides a useful Gradle task that shows you how to organize your files for each of your build types, product flavors, and build variants.


2 Answers

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

Obsolete 1: Try setting this in the dependency project

android {     publishNonDefault true     ... } 

Obsolete 2: 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

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

Update:

Approach from the previous step still can be used with a custom build configuration:

implementation project(path: ':theotherproject', configuration: 'staging') 
like image 92
Beloo Avatar answered Sep 25 '22 12:09

Beloo


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>" 
like image 41
tbruyelle Avatar answered Sep 21 '22 12:09

tbruyelle