Gradle SourceSets by productFlavor and buildType

大城市里の小女人 提交于 2019-12-06 08:46:58

I think I understand. I will say first off once again that this is not a great setup and you should really look to move towards a more standard setup.

So for the solution, in order to read flavor sources dynamically, I recommend moving your ext structure to something like this:

ext {
    sources = [
        flavor1Debug : ['src/pathToJavaReleaseFiles']
        flavor1Release : ['src/pathToJavaDebugFiles']
    ]
}

This lets you read the source paths and iterate over keys and values in project.ext.sources.

Then you can find the variant by name and add sources from the array.

applicationVariants.all { variant ->
        def extraSources = project.ext.sources.get(variant.name)
        if (extraSources != null) {
            def extraSourceFiles = extraSources.collect { project.file(it) }
            def sourceSet = variant.sourceSets.find { it.name == variant.name }
            sourceSet.java.srcDir extraSourceFiles
            def dummyTask = project.task("register${variant.name}ExtraSourcesTask")
            variant.registerJavaGeneratingTask(dummyTask, extraSourceFiles)
        }
    }

The annoyance with doing this at the variant level is that you have to register the extra sources since the variant object at this point has already collected the sources from the source set and product flavors. However, I think this is probably the least invasive way to do this dynamically.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!