Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add flavors in a module with Android Studio?

I have a suite of projects that use the same module, which contains nearly all the actual code. The project is setup like:

project/   - app/     - build.gradle   - libraries/     - module/       - build.gradle   - build.gradle   - settings.gradle 

The dependencies are all setup correctly, and I can build and run apps great, however I can only add flavors to the project, which is not the ideal solution. settings.gradle contains the following:

include ':app', ':libraries:module' 

In the app directory's build.gradle file, I added the following block:

productFlavors {     alpha     production } 

Using gradle 0.11, this syncs and creates assembleAlphaDebug, assembleAlphaRelease, assembleProductionDebug, assembleProductionRelease tasks. When I attempt to do this in the module instead, I get the error:

No resource found that matches the given name (at 'theme' with value '@style/MyCustomTheme')

in the built app/src/main/AndroidManifest.xml. For some reason, the module is not being built, so the custom theme is not working. What am I doing wrong?

like image 632
Phil Avatar asked Jun 19 '14 13:06

Phil


People also ask

What is product flavors Android?

Product flavours lets you create multiple variants of an android app while using a single codebase. To create product flavours you need to define rules in the build.

Can we import module in Android Studio?

Import a module To import an existing module into your project, proceed as follows: Click File > New > Import Module. In the Source directory box, type or select the directory of the module(s) that you want to import: If you are importing one module, indicate its root directory.

What is Flavour dimension Android?

The flavor dimensions define the cartesian product that will be used to produce variants. Example: flavorDimensions("dimA", "dimB") productFlavors { row1 { ... dimension = "dimA" } row2 { ... dimension = "dimA" } row3 { ... dimension = "dimA" } col1 { ...


1 Answers

In the library module's build.gradle, you need a couple extra lines to tell it to export the flavors and which build variant to use by default if not specified when being included from another module:

android {     defaultPublishConfig "productionRelease"     publishNonDefault true      productFlavors {         alpha {         }         production {         }     } } 

That publishNonDefault bit is only necessary if someone would want to depend on something other than the productionRelease variant. Presumably this is the case if you set up multi-flavors in your library in the first place.

Now if you add a dependency from another module via this in its build.gradle:

dependencies {     compile project(':module') } 

it will depend on the productionRelease variant by default. If you'd like to depend on a non-default variant:

dependencies {     compile project(path: ':module', configuration:'alphaDebug') } 
like image 134
Scott Barta Avatar answered Sep 28 '22 15:09

Scott Barta