如何使用Gradle API获取Android Studio主SourceSet? [英] How to get Android Studio main SourceSet with Gradle API?
问题描述
我正在为Android Studio开发gradle插件.这是我的问题如何通过Gradle API获取Android Studio主SourceSet?
I'm working on developing a gradle plugin for Android Studio.Here is my problem How can I get Android Studio main SourceSet with Gradle API?
我想用Java源目录做一些复制工作. 在问这个问题之前,我发现了一些有用但无法解决的问题.
I want to do some copy stuff with java source directory. Before asking this question, I found something useful but not work.
SourceSet main = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME)
我收到此错误消息
Caused by: org.gradle.api.UnknownDomainObjectException: SourceSet with name 'main' not found.
I get this error message
Caused by: org.gradle.api.UnknownDomainObjectException: SourceSet with name 'main' not found.
推荐答案
首先,该代码不起作用,因为它仅适用于带有Java gradle插件的Java项目.所以我搜索了一下,发现有一些解决方法. 这是我的代码.
First,the code doesn't work because it's only for the java project with java gradle plugin. So I googled and found something work around. Here is my code.
project.afterEvaluate {
...
def sets;
if (Utils.is140orAbove()) {
sets = project.android.sourceSets;
} else {
sets = project.android.sourceSetsContainer;
}
sets.all { AndroidSourceSet sourceSet ->
if(sourceSet.name.startsWith("main"))
for(File file:sourceSet.java.getSrcDirs())
if(file.exists()) {
mainSource = file;
break;
}
}
...
}
我的代码不漂亮.正在等待更好的答案.
My code is not beautiful.Waiting for better answer.
这篇关于如何使用Gradle API获取Android Studio主SourceSet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!