许多sourceSets的分组数有完全相同的配置 [英] Grouping few of many sourceSets having exactly same configuration

查看:3048
本文介绍了许多sourceSets的分组数有完全相同的配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有以下 sourceSets

sourceSets {
    flavor1 {
        assets.srcDirs = ['repo-assets/flavor1']
        res.srcDirs = ['repo-res/flavor1']
    }
    flavor2 {
        assets.srcDirs = ['repo-assets/flavor2']
        res.srcDirs = ['repo-res/flavor2']
    }
    flavor3 {
        assets.srcDirs = ['repo-assets/flavor1']
        res.srcDirs = ['repo-res/flavor1']
    }
    flavor4 {
        assets.srcDirs = ['repo-assets/flavor2']
        res.srcDirs = ['repo-res/flavor2']
    }
}

如果您发现 flavor1 flavor3 具有相同的srcDirs也是如此 flavor2 flavor4

If you notice flavor1 and flavor3 have same srcDirs and so does flavor2 and flavor4.

我试图找出是否有使用这样的事情,以避免冗余的方式:

I was trying to figure out if there is a way to avoid the redundancy by using something like this:

sourceSets {
    flavor1, flavor3 {
        assets.srcDirs = ['repo-assets/flavor1']
        res.srcDirs = ['repo-res/flavor1']
    }
    flavor2, flavor4 {
        assets.srcDirs = ['repo-assets/flavor2']
        res.srcDirs = ['repo-res/flavor2']
    }
}

以上不工作(已试过)。寻找类似的东西,这样我就可以提供一组通用的 sourceDirs 的一组风味。任何人试图做类似的东西,可以提供一些指点?

The above does not work (already tried). Looking for something similar so that i can just provide a common set of sourceDirs for a set of flavors. Anyone tried doing something similar and can provide some pointers?

请问的 sourceSets 必须相同味道的名字吗?

Does the name of sourceSets need to be same as that of flavors?

我能说出 sourceSets 分开,然后将它们映射到 productFlavors 这样?

Can i name the sourceSets separately and then map them to productFlavors like this?

productFlavors {
    flavor1 {
      sourceset = "src1"
    }
    flavor2 {
      sourceset = "src2"
    }
    flavor3 {
      sourceset = "src1"
    }
    flavor4 {
      sourceset = "src2"
    }
}

sourceSets {
    src1 {
    }
    src2 {
    }
}


试图可能性#3

能否通过sourcesets任务被动态地分配以某种方式达到同样的东西吗?


Trying Possibility#3

Can the sourcesets be dynamically assigned via tasks somehow to achieve the same stuff?

道格拉斯的回答诸如此类的帮助我非常接近我一直在寻找最终(降低code中的build.gradle)。他用上述可能性#3。感谢道格拉斯!从赏金猎人任何更好的选择仍然是受欢迎的(更接近于可能性#1和#以上2)。如果没有出现的赏金是道格拉斯的已经当周期结束,因为我已经接受了他的答案。但仍然会保持约找到一个更好的选择持乐观态度。

Douglas's answer sort of helped me get very close to what i was looking for eventually (reducing the code in build.gradle). He used Possibility#3 above. Thanks Douglas! Any better alternative from bounty hunters is still welcome (something closer to possibilities #1 and #2 above). If nothing comes up the bounty is Douglas's already when the period ends as I've accepted his answer. But still will remain optimistic about finding a better alternative.

推荐答案

您也接近pretty与第一种可能性:

You were also pretty close with your first possibility:

sourceSets {
    [flavor1, flavor3].each {
        it.assets.srcDirs = ['repo-assets/flavor1']
        it.res.srcDirs = ['repo-res/flavor1']
    }
    [flavor2, flavor4].each {
        it.assets.srcDirs = ['repo-assets/flavor2']
        it.res.srcDirs = ['repo-res/flavor2']
    }
}

以上不IDEA编辑器看起来不错,有很多的警告显示。您可以设置类型,如果你想获得code完成:

The above doesn't look nice in IDEA editor, a lot of warnings are shown. You can set the type if you want to get code completion:

import com.android.build.gradle.api.AndroidSourceSet
android {
    sourceSets {
        [flavor2, flavor4].each { AndroidSourceSet ss ->
            ss.assets.srcDirs = ['repo-assets/flavor2']
            ss.res.srcDirs = ['repo-res/flavor2']
        }
    }
}

另一个窍门:这样的味道的定义是共同位于源集上市

Another trick: this way the definition of the flavor is co-located with the source set listing.

android
    productFlavors {
        flavor1 {
            applicationId "flavor1.app.id"
        }
        flavor2 {
            applicationId "flavor2.app.id"
        }
        [flavor1, flavor2].each {
            sourceSets[it.name].assets.srcDirs = ['repo-assets/flavor1']
            sourceSets[it.name].res.srcDirs = ['repo-assets/flavor1']
        }
    }

你去哪种方式也有关于 srcDirs 一个值得注意的事情,<一个href=\"https://android.googlesource.com/platform/tools/build/+/master/gradle/src/main/groovy/com/android/build/gradle/api/AndroidSourceDirectorySet.java\"相对=nofollow>看源:

Whichever way you go there's also a noteworthy thing about srcDirs, see source:

println assets.srcDirs // say it's [src/flavor/assets]
assets.srcDirs = ['dir1', 'dir2'] // overwrites existing directories: output would be [dir1, dir2]
assets.srcDirs 'dir1', 'dir2' // appends existing directories: output would be [src/flavor/assets, dir1, dir2]
assets.srcDir 'dir1' // appends only one dir, output would be [src/flavor/assets, dir1]

这篇关于许多sourceSets的分组数有完全相同的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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