Gradle SourceSets by productFlavor和buildType [英] Gradle SourceSets by productFlavor and buildType

查看:170
本文介绍了Gradle SourceSets by productFlavor和buildType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



<$ p $

p> sourceSets.whenObjectAdded {
sourceSet - >
def sourceData = rootProject.ext [sourceSet.name]
sourceSet.java.srcDirs = sourceData.javaDirRelease
}

rootProject.ext是一个文件,其中所有productFlavor特定配置都是这样定义的:

  ext {
flavor1 = [
javaDirRelease:['src / pathToJavaReleaseFiles']
javaDirDebug:['src / pathToJavaDebugFiles']
]
}

在主build.gradle中,我也是这样做的: apply from:'variants。 gradle'其中包含上面的ext {}对象。



sourceSets的定义如下:

  sourceSets {
flavor1 {}
}

这个工作正常,但我想添加一个sourceSet特有的productFlavor和buildType像这样:

  sourceSet.debug.java.srcDirs ='src / pathToJavaDebugFiles'

d是针对每种产品口味和每种构建类型定义的,但是当我尝试动态添加它时,这是行不通的。



对我而言,这是有效的(感谢这一点回答如何指定每个flavor的buildType sourceSets?):

  sourceSets {
flavor1 {
def flavorData = rootProject.ext ['flavor1']
版本{
java.srcDirs = flavorData.javaDirRelease
}
debug {
java.srcDirs = flavorData.javaDirDebug
}
}
}

然而,我真的很喜欢这个动态添加,所以我仍然可以保留我的配置文件。我的构建配置相当复杂,并不像这里所描述的那么简单,因此我不需要建议将源文件放到src / flavor1Debug文件夹中,因为这些资源也是从其他productFlavors使用的,所以这不起作用。

解决方案

我想我明白了。我会再次说第一次,这不是一个很好的设置,你应该看看移动到更标准的设置。

所以对于解决方案,为了动态地阅读风味源,我建议将你的ext结构移动到这样的东西上:

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

这可以让您阅读源路径并遍历 project.ext.sources 中的键和值。



然后您可以按名称查找变体并添加数组中的源代码。

  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)
}
}

在变体级别执行此操作的烦恼是您必须注册额外的源,因为此时变体对象已经从源集合和产品风味收集了源。但是,我认为这可能是动态做到这一点的侵入性最小的方式。


EDIT flavors and paths:

Currently I have:

sourceSets.whenObjectAdded { 
    sourceSet -> 
    def sourceData = rootProject.ext[sourceSet.name]
    sourceSet.java.srcDirs = sourceData.javaDirRelease
}

The rootProject.ext is a file where all the productFlavor specific configuration is defined like this:

ext{
    flavor1 = [
        javaDirRelease : ['src/pathToJavaReleaseFiles']
        javaDirDebug : ['src/pathToJavaDebugFiles']
    ]
}

In the main build.gradle I also do: apply from: 'variants.gradle' which contains the above ext{} object.

The sourceSets are defined as such:

sourceSets {
   flavor1{}
}

This works but I want to do add a sourceSet specific to productFlavor and the buildType like this:

sourceSet.debug.java.srcDirs = 'src/pathToJavaDebugFiles'

Which could be defined for each product flavor and per buildType, but this doesn't work when I try to add it dynamically.

What works for me is this (thanks to this answer How can I specify per flavor buildType sourceSets?):

sourceSets {
    flavor1{
        def flavorData = rootProject.ext['flavor1']
        release {
            java.srcDirs = flavorData.javaDirRelease
        }
        debug {
            java.srcDirs = flavorData.javaDirDebug
        }
    }
}

However I would really like this to be added dynamically, so I can still preserve my configuration file intact. My build configuration is quite complex and not as simple as described here, therefore I don't need a suggestion to put the source files into a folder src/flavor1Debug because this resources are used from other productFlavors also, so this won't work.

解决方案

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.

这篇关于Gradle SourceSets by productFlavor和buildType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆