2

I'm writing an Android application with two build flavor. I'm using class with same name in both flavor and this class launched by common code placed in main/src/.

Now question is, is there way to update import-class reference when choose build flavor!! I have idea about Android-studio setting but this just not work until you remove old-import. Any suggestion !!

CoDe
  • 11,056
  • 14
  • 90
  • 197
  • you can use fully qualified classes, based on build type in if else condition. – Pr38y Aug 16 '16 at 07:57
  • 1
    If I understand you correctly, you have put these classes in different source sets. So when building, the class for the applied flavor should be used automatically. Or did you put the classes in different packages? – tynn Aug 16 '16 at 07:58
  • I have a workaround (there can be better solutions). In flavor you can have buildConfigField with different value and based on this you can create instance you want. By this way you need not to touch imports. I think here you need dependency injection pattern as better solution. – Ramit Aug 16 '16 at 08:00
  • @tynn Thanks for your word. I just kept same package and it's working without any interference. – CoDe Aug 16 '16 at 11:16

1 Answers1

2

Android Studio gradle projects can contain different source sets to build the application from. Especially every build type and every flavor has its own source set defined, which gets merge automatically with the main source set.

You cannot change the import statement for different flavors, but you can use these different source sets to have different implementations for the same class. You only have to make sure, not to put these classes into the main source set, but in every mutually exclusive source set, i.e. in debug and release or in flavor1 and flavor2.

tynn
  • 38,113
  • 8
  • 108
  • 143
  • I agree, In simple all you need to keep same package in flavor and place same name class there. – CoDe Aug 16 '16 at 12:32