Dynamically generating product flavors

后端 未结 3 1265
南旧
南旧 2020-12-07 23:16

I\'ve created an Android App which needs to be build in many (30+) flavors.

My idea was to generate the different productFlavors directly from the folde

3条回答
  •  再見小時候
    2020-12-08 00:17

    I know there's already an answer for this, but I kind of combined Chris.Zou's approach with TheHippo. And add my own method for doing this.

    Basically, when dealing with flavors, we normally work with different directories under /app/src/ which contains resources. And since the directory name is equal to the package name, I simply listed the directories under that folder (excluded "main" and "androidTest").

    So here's my complete, working gradle script:

    def map = [:]
    
    new File("./app/src").eachFile() { file->
    
        def fileName = file.getName()
        if( fileName == "main" || fileName == "androidTest") {
            return
        }
    
        map.put(fileName, 'com.mypackagename.' + fileName )
    
    
    }
    productFlavors {
        map.each { flavorName, packagename ->
            "$flavorName" {
                applicationId packagename
            }
        }
    }
    

    Edit:

    • Would also like to add, the com.mypackagename is basically the root path for all flavors.
    • I have a separate script that copy-pastes the a flavor directory to the /app/src/ folder.

提交回复
热议问题