javaandroidandroid-build-flavorsandroid-flavorsbuild-variant

How to use same class but different implementation in android multi flavor project


I have 3 flavors.

flavor1

flavor2

flavor3

and MainActivity for all flavors. I want the same implementation for flavor1 and flavor2 and different for flavor3. If i add separate MainActivity in flavor3/java/com/sample/MainActivity it show "Duplicate class found in the file" and the folder structure is following.

main/java/com/sample/MainActivity

flavor1

flavor2

flavor3/java/com/sample/MainActivity

Do I need to add the MainActivity in all flavors?

Like :

main/

flavor1/java/com/sample/MainActivity

flavor2/java/com/sample/MainActivity

flavor3/java/com/sample/MainActivity

But doing this every time I have to update when change in flavor1 and make that change in flavor2. is there any solution?


Solution

  • Do I need to add the MainActivity in all flavors?

    Yes.

    But doing this every time I have to update when a change in flavor1 and make that change in flavor2. is there any solution?

    Have MainActivity be as small as possible, delegating to other classes as needed. So, the flavor1 and flavor2 implementations of MainActivity might delegate to some other class that resides in main, while the flavor3 implementation of MainActivity does something else.