1

I know we can build different flavor apks with different resources by creating different directory within src, but now I want to make some java code customized, is that possible? Or any other ways to do it?


update

I have a project with 100 *.java files, and I have 10 flavors, but each flavor has only one file is customized and the file in each flavor is not the same file, so I have to put that 10 files in each flavor, and once a file in that 10 files is modified, I will have to modify the same file in other flavors, the project is like:

+ App |- src |- main |- java |- path |- A.java(can't be here) |- B.java(can't be here) |- C.java(can't be here) |- D.java(can't be here) |- E.java(can't be here) |- F.java(can't be here) |- G.java(can't be here) |- Others.java + res |- flavorA |- java |- path |- A.java(customized) |- B.java |- C.java |- D.java |- E.java |- F.java |- G.java + res |- flavorB |- java |- path |- A.java |- B.java(customized) |- C.java |- D.java |- E.java |- F.java |- G.java + res |- flavorC |- java |- path |- A.java |- B.java |- C.java(customized) |- D.java |- E.java |- F.java |- G.java + res |- flavorD |- java |- path |- A.java |- B.java |- C.java |- D.java(customized) |- E.java |- F.java |- G.java + res |- flavorE |- java |- path |- A.java |- B.java |- C.java |- D.java |- E.java(customized) |- F.java |- G.java + res |- flavorF |- java |- path |- A.java |- B.java |- C.java |- D.java |- E.java |- F.java(customized) |- G.java + res |- flavorG |- java |- path |- A.java |- B.java |- C.java |- D.java |- E.java |- F.java |- G.java(customized) + res

jason.yu
  • 59
  • 5

1 Answers1

2

Yes you can use different java class for both different product flavor so your project structurer will be like below image.

enter image description here

It was works for me.

+ App
|- src
    |- main
      |- java
          |- path
             |- A.java
             |- B.java
             |- C.java
             |- D.java
             |- E.java
             |- F.java
             |- G.java
             |- Others.java
      + res
   |- flavorA 
      |- java
          |- path
             |- A.java(customized)
      + res
    |- flavorB 
      |- java
          |- path
             |- B.java(customized)
      + res
    |- flavorC 
      |- java
          |- path
             |- C.java(customized)
      + res
    |- flavorD
      |- java
          |- path
             |- D.java(customized)
      + res
    |- flavorE
      |- java
          |- path
             |- E.java(customized)
      + res
    |- flavorF
      |- java
          |- path
             |- F.java(customized)
      + res
    |- flavorG
      |- java
          |- path
             |- G.java(customized)
      + res

You have to add file structure as above. You have to add all the files on main directory and only you need to add modified files as per flavor. If you will put every files in every flavor than in future when you will change A.java than you have to update every A.java of all flavors.

So its better add all the files on main directory and only you need to add modified files as per flavor.

Rujul Gandhi
  • 1,700
  • 13
  • 28
  • In this way, AdminHelperImpl.java can not be put in 'main' ,and if I have 10 flavors, I will have to maintain 10 AdminHelperImpl.java files – jason.yu Jun 13 '18 at 10:52
  • yes if your all the java files different from each other than you have to manage every file. Just for example if you have 5 files same and other 5 are the different than you have to add Sample.java (same files)in main folder and other five you have to add as per your flavours. – Rujul Gandhi Jun 13 '18 at 11:27
  • @jason.yu Did you get that ? – Rujul Gandhi Jun 13 '18 at 13:33
  • I get it ,but what I mean is that there are more flavors and more different Customization.java files in different flavors ,look what I have updated in my question – jason.yu Jun 14 '18 at 03:00
  • Actually, the file structure in your answer is what I want, but it will build failed, did you successfully run it ? – jason.yu Jun 14 '18 at 06:53
  • Did you solve this issue you are still getting issue ? – Rujul Gandhi Jun 15 '18 at 06:25
  • I use another work around way to do it ,which is to add a common code module to put the same files, and all modules not customed declare "srcDir codeModule" to add those files,only the customed files are put in the customer flavors – jason.yu Jul 01 '18 at 04:00