gpt4 book ai didi

android - 从 Gradle 插件生成的代码对使用 android-apt 的 Dagger 编译器不可见

转载 作者:行者123 更新时间:2023-12-03 05:56:57 26 4
gpt4 key购买 nike

我正在尝试编写一个自定义插件,它将从我的 android 项目中的一些 xml 文件中生成一些代码(特别是 Dagger 模块)。我已经能够生成要生成的代码,但是当我尝试在 Dagger 组件中使用生成的模块时,Dagger 编译器会失败:

我整理了一个 small demo project这证明了失败。有两个模块:插件和 android 应用程序(仅供引用:我必须在应用程序的 build.gradle 中注释掉与插件相关的代码,直到我在本地安装了插件)。

当我重新构建应用程序时,生成的模块会显示在我期望的位置,并且 dagger 组件会在 IJ 中看到它。但是在 android-apt 插件调用 dagger 编译器时它显然不可用(或者该位置不包含在编译器正在查看的代码中)。

我确保所有的编译任务都依赖于我的“生成”任务,失败的任务是 compileDebugJavaWithJavac因此应该知道我生成的代码。

为了尝试包含源代码,我有:

AndroidSourceSet mainSourceSet = p.android.sourceSets.getByName('main')
LOG.info("Adding directory ${outputDir} to android source set ${mainSourceSet}")
mainSourceSet.java.srcDir(outputDir)

尽管如此,我得到:
Compiling with JDK Java compiler API.
C:\projects\java\android\android_plugin_demo\plugindemoapplication\src\main\java\com\bdl\plugindemoapplication\DaggerComponent.java:12: error: cannot find symbol
@Component(modules = PluginDaggerModule.class)
^
symbol: class PluginDaggerModule
C:\projects\java\android\android_plugin_demo\plugindemoapplication\src\main\java\com\bdl\plugindemoapplication\DaggerComponent.java:13: error: dagger.internal.codegen.ComponentProcessor was unable to process this interface because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public interface DaggerComponent {
^
2 errors

那么,让 Dagger 编译器看到我生成的代码我缺少什么魔法呢?

最佳答案

经过更多的实验,我相信我已经找到了答案。我还不知道为什么这可行,但如果我将输出目录添加到 AndroidSourceSet 的 srcDir 到 p.afterEvaluate 之前关闭,它似乎找到它。

这对我来说似乎很奇怪,因为任务依赖项仅添加在 afterEvaluate block 中,并且看起来新任务是在编译之前执行的,所以即使没有这个更改,我也希望 srcDir 添加也会被执行在编译尝试之前。

但至少我现在可以正常工作了。

关于android - 从 Gradle 插件生成的代码对使用 android-apt 的 Dagger 编译器不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44347826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com