Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use different build types of Library Module in Android App Module in Android Studio and Gradle

Tags:

I have a workspace containing several different apps and a common library project that I am looking to convert from Eclipse to Android Studio and from Ant to Gradle. Currently, in our ant scripts we replace text in a few of our classes before compiling depending on if the build is for debug purposes, QA, Beta/Customer Acceptance testing or meant for release to Google Play.

I want to take advantage of the build variant system in Gradle and use value resources (booleans, strings, etc) to keep from having the build script replace text in my classes.

Since duplicating the different values for all of the build types in all of my app products would be a maintenance headache, I want to put them in the build types in my common library. I tried this but no matter which build type I use in my app modules, the app module pulls in the Release build of the common library module.

Is there any way that I can get the different build types of the app modules to use the corresponding build type of the library module?

This was run from Android Studio with the Build Variants tool window showing both modules as using the debug variant. The image should read debug for both app and library.

debug debug

like image 919
cren90 Avatar asked Jan 22 '15 05:01

cren90


People also ask

What is a 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.

What is library module in Android Studio?

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest.

How many types of Gradle build files do we have in Android?

Each module has its own build file, so every Android Studio project contains two kinds of Gradle build files.

What is build Gradle module?

The Gradle build system in Android Studio makes it easy to include external binaries or other library modules to your build as dependencies. The dependencies can be located on your machine or in a remote repository, and any transitive dependencies they declare are automatically included as well.


2 Answers

Update: publishNonDefault is deprecated and has no effect anymore. All variants are now published.

Documentation taken from Library Publication chapter in Gradle Plugin User Guide.

By default a library only publishes its release variant. This variant will be used by all projects referencing the library, no matter which variant they build themselves. This is a temporary limitation due to Gradle limitations that we are working towards removing.

Anyway, there are solutions for this problem.

Publish All - It is possible to publish all build variants of your library project by adding following line to your library project:

android {     publishNonDefault true } 

Then you should modify your dependencies in app project as follows:

dependencies {     releaseCompile project(path: ':yourLibrary', configuration: 'release')     debugCompile project(path: ':yourLibrary', configuration: 'debug')      // This is also possible     customCompile project(path: ':yourLibrary', configuration: 'custom') } 

Change Default - You can change which varaint gets published from your library by adding following line to your library project:

android {     defaultPublishConfig "debug" } 

And in this case you don't have to change app's dependencies because it will always get debug build variant.

like image 71
Aleksandar Ilic Avatar answered Sep 22 '22 07:09

Aleksandar Ilic


As of Android Gradle Plugin v3.0.0, the plugin could choose the build type to compile for the sub-module library based on the build type being compiled for the app. Hence debug would compile debug type of library, and release would compile release type of the library. Furthermore there are even added extensions for resolving non-standard build types or flavors using matchingFallback or missingDimensionStrategy.

More info can be found here: Use variant-aware dependency management

like image 27
ahasbini Avatar answered Sep 24 '22 07:09

ahasbini