Gradle flavors for android with custom source sets - what should the gradle files look like?

后端 未结 5 1133
慢半拍i
慢半拍i 2020-12-02 10:19

I\'ve got an old eclipse project I\'ve moved into android studio and setup to use flavors. It seemed to be working fine till I started trying to use different java files be

5条回答
  •  情书的邮戳
    2020-12-02 10:58

    I think you'd be better off not defining custom sourceSets but using the default gradle configuration. I used to do custom sourcesets until I realized the conventions are, well, convenient.

    You'll want something like this:

    + src
        + main // this is your common code
            + java 
            + res
        + flavor1
            + java
            + res
        + flavor2
            + java
            + res
    

    Then you can just go ahead and remove the sourcesets closure from your build.gradle

    NOTE: For the gradle configuration, resources are merged or overridden whereas all java code is put on the same class-path. For example, the AndroidManifest.xml files for each flavor need only have the differences from main's manifest file. Having an asset, for example, ic_launcher in a flavor overrides the ic_launcher from main if such file exists. However, having a file HomeActivity.java in both main and the flavor is not possible and will give a duplicate file error.

提交回复
热议问题