[英]How to add a folder to an android project sourceset from a gradle plugin
I'm writing a plugin for gradle, to generate some classes for Android projects. 我正在为gradle编写一个插件,为Android项目生成一些类。
I generate those classes in the projects src/gen/java
folder, as I don't want them to be mixed with real source code. 我在项目src/gen/java
文件夹中生成这些类,因为我不希望它们与真正的源代码混合在一起。
From the project's build.gradle
config, I can add this to make the build tools see the generated classes : 从项目的build.gradle
配置中,我可以添加它以使构建工具看到生成的类:
android {
sourceSets {
main {
java {
srcDir 'src/gen/java'
}
}
}
}
The problem is that I want my plugin to set this automatically. 问题是我希望我的插件自动设置它。 From my plugin I tried the followings : 从我的插件我尝试了以下内容:
public class MyPlugin implements Plugin { 公共类MyPlugin实现插件{
@Override
public void apply(Project project) {
// ...
// TEST 1 : doesnt work
project.android.sourceSets.main.java.srcDirs += "src/gen/java"
// TEST 2 : doesnt work
project.android.sourceSets {
main {
java {
srcDir 'src/gen/java'
}
}
}
}
} }
Each time the plugin works, but the folder is still not seen by the compiler and it can't find the generated classes when compiling. 每次插件工作时,编译器仍然看不到该文件夹,并且在编译时无法找到生成的类。 Does any one know of another way to do this from the plugin ? 有没有人知道从插件中执行此操作的另一种方法?
If you move the files to to the build/generated/sources
dir of the app module the plugin is currently generating code for you then you don't have to add it to the source set. 如果您将文件移动到app模块的build/generated/sources
目录,插件当前正在为您生成代码,那么您不必将其添加到源集。
you could also look at SQLDelight which is also a gradle plugin which does code generation or AutoValue 您还可以查看SQLDelight ,它也是一个执行代码生成或AutoValue的gradle插件
Edit: 编辑:
You also need to call the registerJavaGeneratingTask on the BuildVariant that will generate the sources 您还需要在将生成源的BuildVariant上调用registerJavaGeneratingTask
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.