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

后端 未结 5 1113
慢半拍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.

    0 讨论(0)
  • 2020-12-02 11:04

    You can code like the example below i taking

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            res.srcDirs = ['res']
            java.srcDirs = ['src', '../3party/Alohalytics/src/android/java', 'ttsearch/jni/out']
            assets.srcDirs = ['assets', 'flavors/mwm-ttf-assets']
            jniLibs.srcDirs = ['libs', 'jniLibs', 'ttsearch/libs']
        }
        flavor {
            manifest.srcFile 'flavor'
            assets.srcDirs = ['flavor/assets']
            res.srcDirs = ['flavor/res']
            res.srcDirs = ['flavor/res']
            ....
        }
    }
    
    0 讨论(0)
  • 2020-12-02 11:09

    This way works for me. Enjoy

    sourceSets {
            main {
                manifest.srcFile 'src/AndroidManifest.xml'
                java.srcDirs = ['src/java']
                resources.srcDirs = ['srs/others']
                res.srcDirs = ['src/res']
                assets.srcDirs = ['src/assets']
                jniLibs.srcDirs = ['jniLibs']
            }
            development{
                res.srcDirs += ['development/src/res']
            }
            standford{
                res.srcDirs += ['standford/src/res']
            }
    
            commercial{
                res.srcDirs += ['commercial/src/res']
            }
    
        }
        productFlavors {
            development{
                flavorDimensions "default"
            }
            standford{
                flavorDimensions "default"
            }
            commercial{
                flavorDimensions "default"
            }
        }
    
    0 讨论(0)
  • 2020-12-02 11:14

    You are welcome to use the custom sourceSets and flavours (or buildTypes) if you wish.

    As an example, you can set them in your Gradle file as follows:-

    productFlavors {
        flavor2 {
        }
        flavor1 {
        }
    }
    
    sourceSets{
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src/commonFiles/java']
            resources.srcDirs = ['src/commonFiles/java']
            aidl.srcDirs = ['src/commonFiles/java']
            renderscript.srcDirs = ['src/commonFiles/java']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
      flavor1 {
           java.srcDirs = ['src-flavor1'] 
           res.srcDirs = ['res-flavor1']
           ...
        }
    
    }
    
    0 讨论(0)
  • 2020-12-02 11:15

    Here is what my Gradle looks like :

       productFlavors {
        // Still keeping the productFlavors closure in case we decide to add flavors later
        normal {
            applicationId 'com.akshat'
        }
        qa {
            applicationId 'com.akshat.qa'
        }
    }
    
     sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
            jni.srcDirs = [] // Set empty since we use our own ndkBuild task
            jniLibs.srcDirs = ['libs']
        }
    
        normal {
            java.srcDirs = ['src_normal']
        }
        qa{
            java.srcDirs = ['src_qa']
        }
    

    And here is how my Directory structure is :

    MyApplication
        - res
        - libs
        - jni 
        - src
             -com/akshat/
        - src_qa
             - com/akshat/util/FlavorConst.java
        - src_normal
             - com/akshat/util/FlavorConst.java
    
    0 讨论(0)
提交回复
热议问题