Android:Dagger 2是否会干扰不可变对象?

时间:2018-09-03 07:56:40

标签: java android guice dagger-2 immutables-library

遇到一个非常奇怪的问题;希望你们能提供帮助。

很长一段时间以来,我一直在自己的android项目中使用Immutables library,并且没有任何问题。为不可变对象自动生成的文件位于(例如)build/generated/source/apt/debug/com/package/name/ImmutableClass.java中,项目可以编译并正常运行。

我最近决定在同一项目中使用依赖注入,并开始使用Dagger2。我曾在另一个Java项目(非Android)中使用Guice,但之前从未使用过Dagger。突然,我开始收到奇怪的错误,说找不到Immutables类。

错误:error: cannot find symbol class ImmutableClass

我尝试删除我创建的@Component@Module,错误消失了。有趣的是,文件ImmutableClass.java仍位于与以前相同的位置。

请让我知道您的想法。谢谢。

2 个答案:

答案 0 :(得分:2)

Dagger 2与其他代码生成库结合使用时确实会产生奇怪的错误消息。

如果您更深入地研究错误,那么您可能会发现另一个提示“找不到符号类DaggerXXXComponent”(或类似内容)的人。

您最有可能遇到使Dagger预处理器失败的问题。然后,由于Dagger失败,其他代码生成将无法运行,包括Immutables。

您需要调试导致Dagger失败的错误,然后Immutables错误也将消失。

答案 1 :(得分:0)

请考虑使用<annotationÅrocessorPath>。通过编辑引用我当前模块的pom.xml文件,以下方法对我有效:

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.immutables</groupId>
                        <artifactId>value</artifactId>
                        <version>${dependency.version.immutables}</version>
                    </path>
                    <path>
                        <groupId>com.google.dagger</groupId>
                        <artifactId>dagger-compiler</artifactId>
                        <version>2.16</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>

重要的是:路径的排序顺序无关紧要。我使用的是Maven 3.6.0版。

相关问题