33

I have an android app with 2 flavors: -flavor1 -flavor2

My directory tree is :

/src/main
/src/flavor1
/src/flavor2

main has the default source set. flavor1, and flavor2 directories have their own source set, that gradle gets automatically. If I add a resource file into a flavour directory it overrides it from main, and works fine.

However, if I add a java class into flavor2 to override another one from "main", (for example an activity that needs to be different in flavor2), it doesn't replace it and it says there's a duplicated class in my project. I only got this to work by deleting the class from "main" directory and adding it to all flavors folders.

Is there a way I can override java classes just like resource files ? why does this happen?

1
  • 2
    "Is there a way I can override java classes just like resource files ?" -- AFAIK, no. "why does this happen?" -- AFAIK, that is standard Gradle sourceset behavior for Java code. Jul 30, 2015 at 21:10

1 Answer 1

44

Is there a way I can override java classes just like resource files ?

No. Xavier Ducrohet from the Android tools team discussed some of this briefly here.

There are ways of accomplishing what you want though- more on that in a second.

why does this happen?

Your Java code needs to compile. Assuming you reference a class Foo from other classes in your main source set, that Foo class has to have the same public API regardless of what flavor is being compiled. If you were able to completely replace the main Foo with a flavor-specific Foo, this might not hold true. This is of course not an impossible problem to solve with the tools, but the current build tools' solution is to simply not let you do that.

Of course with resources such as images, this is completely different- as long as some version of the resource exists, Android can use it just fine.

You can, however, create flavor-specific implementations of a class to use within your main source set. The easiest way is to simply put just a Foo in each flavor, and not provide a Foo in your main source set. For bonus points, create an interface for these implementations within your main source set to ensure that the flavor-specific implementations conform to the same interface and won't cause build errors.

2
  • If I'll create flavor-specific implementation of Foo (In my case it is an activity) without providing it in my main source set, it means that the package name of the Foo class is going to be different for each of the flavors. Does it mean that I have to get the package name of Foo dynamically (hence error prone) in case I'm planing to start Foo from the main source set?
    – idish
    Jul 22, 2020 at 22:02
  • 1
    Answering my own comment above - The key here is to place Foo in the common package name (in each of the flavors) and not in the full package.
    – idish
    Jul 23, 2020 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.