Gradle dynamic flavor

2019-07-10 16:18发布

I would like to create dynamic flavors from the directory tree It works great!

But android-studio use gradle in his tmp file like:

/home/svirch_n/.IntelliJIdea14/system/compile-server

and my script doesn't work anymore because it uses relative paths Like this:

Closure getFlavors = { rootDir, basePackage ->
    def result = [:]
    new File("$rootDir").eachDir() { dir ->
        def name = dir.getName()
            if ("$name" != "main")
                result.put("$name", "$basePackage.$name")
        }
    return result
}

// This is a ugly closure.
// If I can get ride of this, my problem would be solved
Closure getSrcPath = {
    if (System.getProperty("user.dir").split("/").last() == "app") {
        return "src"
    } else {
        return "app/src"
    }
}

android {

    ...

    def myFlavors = getFlavors(getSrcPath(), "com.example.app")

    productFlavors {

        myFlavors.each { flavorName, flavorPackage ->
            "$flavorName" {
                applicationId "$flavorPackage"
            }
        }
    }

}

Do you have any idea to resolve this ? Thanks in advance for your help

P.S: I want dynamic flavors cause my git project has public and private repositories and not everyone can have all the flavors but I want them to compile anyway.

1条回答
混吃等死
2楼-- · 2019-07-10 16:38

Assuming I am in the subproject 'app', I can use:

project(":app").getProjectDir().getPath()
查看更多
登录 后发表回答