I am working on an Android project that uses Flavors
. There are currently 4 flavors in my project but there will soon be 6-7 flavors.
Let's say I have 4 flavors, f1, f2, f3, and f4.
I am very familiar with Android resources in main/ being overridden by the ones in the flavors, but classes are not (cf "duplicate classes" compilation error). So if I have MyClass
in main/ and I want to change this class only in f3, I understand there are a few solutions :
- Remove MyClass from main/ and put it in all the flavors, then do the modifications in f3/MyClass. This would lead to a maintenance hell, I don't want to do that.
- Have in main/ a
AbstractMyClass
which has the same content than MyClass, inherit it with an empty MyClass in f1, f2, f4 and inherit it in f3 and do the modifications. Again I don't like this solution because it makes the class number grow really fast and this destroys the project's readability. Actually the project I currently work with uses this solution and it is a nightmare since the project is big.
I found another solution with this beautiful post: https://stackoverflow.com/a/30548238/8096984.
This post describes that f1, f2, and f4 would have a link to a common additional sourceSet containing MyClass (so MyClass wouldn't be in main/ anymore), and f3 wouldn't have it. So it could be free to define its own implementation of MyClass without the duplicate class error.
This solution seems to be the best of the 3, but for 1 overriding. What if now I have a MyClass2 that I want different only in f2? If I put MyClass2 in the additional sourceSet, then I would have to link f3 to the sourceSet and therefore break the first overriding that I did because of the duplicate class error.
Do I have to create another sourceSet? If I do, then I don't really like this solution because you can imagine the number of sourceSets that I would have to create in a big project that uses 6-7 flavors...
What should I do?