简体   繁体   English

如何从gradle插件向android项目源集添加文件夹

[英]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.

相关问题 Android Gradle 插件无法识别 SourceSet 'robolectric' - The SourceSet 'robolectric' is not recognized by the Android Gradle Plugin Android Gradle插件无法识别SourceSet的“instrumentTest” - The SourceSet 'instrumentTest' is not recognized by the Android Gradle Plugin 如何将生成文件夹中的生成代码添加到 Gradle Android 库插件中的路径 - How to add a generated code from build folder to the path in a Gradle Android Library plugin Android Gradle 如何按产品风格自定义库中的源集? - Android Gradle how to custom sourceSet in library by product flavor? 从android gradle插件生成eclipse项目 - Generating eclipse project from android gradle plugin Gradle:从Test sourceSet中的主要sourceSet覆盖类 - Gradle: Override Class from main sourceSet in Test sourceSet 当我合并分支时,Android Gradle插件无法识别获取SourceSet'instrumentTest' - Getting The SourceSet 'instrumentTest' is not recognized by the Android Gradle Plugin when i merged branch Android Gradle 插件无法识别 SourceSet 'commonTest'。 也许你拼错了什么? - The SourceSet 'commonTest' is not recognized by the Android Gradle Plugin. Perhaps you misspelled something? 如何在Codename One Android项目中为build.gradle添加`apply plugin`以添加本机库? - How to add `apply plugin` to build.gradle in Codename One Android project in order to add native library? 如何在android studio Gradle中将libs依赖项从项目添加到模块 - how to add libs dependency from project to module in android studio Gradle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM