[英]How to add a generated code from build folder to the path in a Gradle Android Library plugin
I've got an Android Library module, and in the build.gradle.kts
script I've made a plugin class, inside the apply method of the plugin I am doing:我有一个 Android 库模块,在
build.gradle.kts
脚本中,我制作了一个插件 class,在我正在做的插件的 apply 方法中:
import com.android.build.gradle.LibraryExtension
val libExtension = project.extensions.findByType(LibraryExtension::class.java)!!
libExtension.libraryVariants.all {
val variantName = this.name
val sourceDir = "${project.buildDir}/generated/my-plugin/$variantName"
libExtension.sourceSets[variantName].kotlin.srcDir(sourceDir)
}
(where variantName is “debug” or “release”) (variantName 是“debug”或“release”)
I thought this code would add the folder as a source folder, but it doesn't (and therefore the kotlin class I have in the folder is not available).我认为这段代码会将文件夹添加为源文件夹,但它没有(因此我在文件夹中的 kotlin class 不可用)。
Under the build folder of my android library module:在我的 android 库模块的 build 文件夹下:
My folder (not working / not blue):我的文件夹(不工作/不蓝色):
A correctly working folder in the build directory, created by another plugin:构建目录中正确工作的文件夹,由另一个插件创建:
Ok so the code in the question works, I'm not so crazy ><.好的,所以问题中的代码有效,我没那么疯狂><。
When I split this out into the demo it showed it working.当我将其拆分为演示时,它表明它可以正常工作。
Here is the code that does not work:这是不起作用的代码:
import com.android.build.gradle.LibraryExtension
val libExtension = project.extensions.findByType(LibraryExtension::class.java)!!
libExtension.libraryVariants.all {
val variantName = this.name
val sourceDir = "${project.buildDir}/generated/my-plugin/$variantName"
// This task generates some code and puts it in the sourceDir
val tp = project.tasks.register<MyTask>("${variantName}MyTask")
tp.get().doLast {
libExtension.sourceSets[variantName].kotlin.srcDir(sourceDir)
}
}
I am assuming the "doLast" is what's stopping it working in the IDE.我假设“doLast”是阻止它在 IDE 中工作的原因。 My guess is that if I generate the files first and then set the folder as a sourceset the IDE isn't triggered into action.
我的猜测是,如果我先生成文件,然后将文件夹设置为源集,则不会触发 IDE 动作。 But if I set it as a sourceset first, and then generate the files, it kinda kicks in?
但是,如果我先将其设置为源集,然后生成文件,它会起作用吗?
Leaving the question here anyway, as I found it hard to even find anyone talking about this.无论如何都把问题留在这里,因为我发现很难找到任何人谈论这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.