简体   繁体   中英

How to build different flavor apks with different Java code?

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

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

在此处输入图片说明

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM