Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android theme module with flavors

So this is a bit interesting, I'm unsure how exactly to set it up in android studio. I have several modules that have some re-usable components I use in various apps, however it would be nice to inject certain themes into the reusable components using flavors. Rather than create a new flavor for every component for every app I write, I was thinking of having 1 Theme module, that would have a flavor per app I write, that has color schemes...etc. Here is kind of how I want it set up:

App1: dependencies
reusable lib1
reusable lib3
reusable lib4
theme - App1 flavor

App2: dependencies
reusable lib1
reusable lib2
reusable lib4
theme - App2 flavor

Now I would prefer if the reusable libs could simply depend on theme without needing to know which flavor to build, and the main app proj in it's dependency on theme could reference the flavor for that app (using this answer https://stackoverflow.com/a/24316133/1316346). The reason for this is each reusable module can't have a single app in it's build.gradle dependencies or it would break other apps referencing them. It's also tedious to have to make a flavor of each reusable module for every app I write. Is there any way to achieve something like this? Here's what I tried:

App1 build.gradle:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile project(path: ':Theme', configuration: 'app1Release')
    compile project(':Lib1')
    compile project(':Lib2')
    compile project(':Lib4')
}

App2 build.gradle:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile project(path: ':Theme', configuration: 'app2Release')
    compile project(':Lib1')
    compile project(':Lib3')
    compile project(':Lib4')
}

Lib1 build.gradle:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile project(path: ':Theme')
}

The problem with this is as soon as Lib1 tries to access anything in theme, it get's an error. In fact it doesn't even build theme first, it will attempt to build Lib1 before Theme even though Lib1 has a dependency (something weird with the flavors). If I change Lib1 to:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile project(path: ':Theme', configuration: 'app1Release')
}

It will work for app1, but I'd have to either constantly change it prior to building each app, or do lots of flavors for each lib I'd like to avoid. Anybody ever achieve anything like this?

tl;dr Can a module reference a flavor of another module based on the flavor built by the app referencing the same module

like image 297
Kevin DiTraglia Avatar asked Jul 07 '15 19:07

Kevin DiTraglia


People also ask

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 { ...

How do I use a different Google services JSON file with multiple product flavors Android?

Usage: How to use this file for different product flavors. There are only 3 steps first step is so simple just remove the google-service. json file from the root of the app/ module and save it your local system directory for the save side, & then make your product flavors like Production & Staging environment.

What are buildTypes and product Flavours in Gradle?

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.


1 Answers

Take a look at Manifest Merging: http://developer.android.com/tools/building/manifest-merge.html

For having the same flavor of every library for each app you make, denote in the XML files an intent:

Intent can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

That way, you can start up multiple apps with the same one, solving the problem of having to change the dependencies for every app. With intent, you'd just have apps starting up with the same code by itself.

Yes, a module reference a flavor of another module based on the flavor can be built by the app referencing the same module.

like image 157
Eddev Avatar answered Sep 24 '22 21:09

Eddev