8

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

Community
  • 1
  • 1
Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
  • Why don't you use actual themes in your apps? What kind of values do you need to pass into your modules? If you use theme attributes like `?colorPrimary` in your modules you can use plain Theming without flavors, you can also create custom values to include into your theme – David Medenjak Feb 28 '16 at 14:01

2 Answers2

1

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.

Eddev
  • 890
  • 1
  • 10
  • 21
  • I don't quite understand what you are saying, can you give an example how I could accomplish that given my examples from the question? – Kevin DiTraglia Feb 28 '16 at 01:56
  • Basically, instead of making a flavor for each library, use an `intent` to define a start of each app. Take a look at this: http://www.raywenderlich.com/103044/android-intents-tutorial OR http://programmerguru.com/android-tutorial/android-intent-example/ – Eddev Feb 28 '16 at 18:41
0

I have not done this myself, but based on my understanding of configuration injection, could you not include all of the flavours in the Lib1 project (with the corresponding correct Theme project dependencies) and then include the configuration flavour in your Lib1 Dependency in App1 and App2?

E.g.,

dependencies {
   compile fileTree(include: ['*.jar'], dir: 'libs')
   compile 'com.android.support:appcompat-v7:22.1.1'
   compile project(path: ':Theme', configuration: 'app1Release')
   compile project(path: ':Lib1', configuration: 'app1Release')
   compile project(':Lib3')
   compile project(':Lib4')
}
  • 1
    This has been my work-around so far, but requires me to have a separate flavor of every library for every app I make, which I'm trying to avoid. – Kevin DiTraglia Feb 22 '16 at 20:41
  • Or you can de-couple your theming from the libraries, no? It seems that if the themes are integral to the libraries, then you are going to have 'flavours' of those libraries for each theme inherently. If the themes are 1-1 with each app you make, then likewise you will have that many flavours in the libraries. – Stephen Edwards Feb 23 '16 at 15:55
  • The themes are just general stuff like 'primary-color' and 'button-style', they will always exist in the theme project. I was hoping for a solution where the libraries just pull in those values without knowing which flavor of the theme project actually built, it just knows all those keys will exist. – Kevin DiTraglia Feb 23 '16 at 19:56
  • Either the libraries need to know the theme flavour - and so have their own flavours for each - or they do not need to know it, and so the Theme project need not even be a dependency for them, no? – Stephen Edwards Feb 23 '16 at 20:15
  • I want a solution where the app dictates the flavor of theme and the libraries just depend on what's there, making a flavor for each library is cumbersome and kind of defeats the purpose of keeping the theme related stuff separate to begin with. – Kevin DiTraglia Feb 24 '16 at 15:05