12

In my current App I have 2 builds, lets call them build 1 and build 2. I understand that two flavours can share common code/resources but can also have separate code and resources such as the following:

Two Flavours Diargram

What i am trying to do is expand build 2 so that it shares some code with another build (e.g build 3). Either sharing some code such as the following:

Two Flavours Sharing Code

This would be build2 and build 3 sharing some code/resources while also having the ability to have their own unique code sets. Or:

Build 3 Extending Build 2

Here, build 3 is extending build 2 so that all code and resources from build 2 are available in both builds but build 3 can also have its own unique code/resources.

I think that extending build 2 is the best way (if it is possible) but any advice or pointing me in the right direction would be much appreciated. I have spent over 6 hours scouring the internet with no avail.

Community
  • 1
  • 1
Roboute Guilliman
  • 233
  • 1
  • 2
  • 14

1 Answers1

11

There's not good support for this. If you really wanted to do it you could explore some hackery with symlinks to try to make different flavors effectively share folders, but that will make things a little weirder with source control, and I can't be sure it won't cause subtle problems in the build.

I would encourage you to not try to do a lot of trickery in the build system, because in the long run it will make your app harder to write and maintain, especially since at present the IDE doesn't have good facilities for dealing with code across product flavors (e.g. if you refactor code, there's no way to have the refactoring apply across a non-active product flavor; you can only work with one at a time).

A better approach would be to take the stuff that's common to Build 2 and Build 3 and extract it out into an Android Library module that's a dependency for those flavors but not for the Build 1 flavor. Different product flavors can have different dependencies, and that mechanism is supported and works well.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Thanks for this. I am just going to leave it for now. I don't really want t do a hackery way that will over complicate the codebase. Thank you for the quick response and the answer! – Roboute Guilliman Jan 13 '15 at 11:56
  • 1
    you could try using `sourceSets` like [in this question](http://stackoverflow.com/questions/20974524/with-two-res-directories-per-build-variant-gradle-stopped-tracking-changes-in-r) – Laur Ivan Sep 10 '15 at 18:50