简体   繁体   English

如何将生成文件夹中的生成代码添加到 Gradle Android 库插件中的路径

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

相关问题 如何从gradle插件向android项目源集添加文件夹 - How to add a folder to an android project sourceset from a gradle plugin 如何在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如何使用gradle在构建过程中从外部文件夹添加可绘制对象 - Android how to add drawables from external folder during build using gradle 如何在 Android Studio 项目中将库添加到 Gradle 构建? - How to add a library to Gradle build in Android Studio project? 将productFlavor添加到实验性Android gradle插件库中 - Add productFlavor to experimental Android gradle plugin library 如何将生成的源文件夹添加到 Gradle 中的源路径? - How can I add a generated Source Folder to my Source Path in Gradle? Android Gradle库构建:在生成的aar内的jar中包含和排除文件 - Android Gradle library build: Include & exclude files from the jar inside the generated aar 如何用Android Studio和gradle构建一个Android库? - How to build an android library with Android Studio and gradle? 如何使用 Maven-Publish Android Gradle Plugin 生成的组件在 Kotlin DSL (build.gradle.kts) 中? - How do you use Maven-Publish Android Gradle Plugin's generated components in Kotlin DSL (build.gradle.kts)? 使用Android-apt从Dagger编译器看不到Gradle插件生成的代码 - Generated Code from Gradle Plugin not visible to Dagger compiler using android-apt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM