同时使用Dagger 2.15与自定义AnnotationProcessor时Maven编译报FluentFuture缺失错误
解决Dagger 2.15与自定义AnnotationProcessor共存时的编译错误
这个问题本质上是注解处理器的classpath依赖缺失或版本冲突导致的:Dagger 2.15依赖特定版本的Guava,当自定义处理器加入后,要么Guava版本被低版本依赖覆盖,要么处理器的运行classpath里没有包含Guava,从而找不到FluentFuture类。下面是具体的解决步骤:
1. 检查并统一Guava依赖版本
Dagger 2.15默认依赖Guava 23.0版本,先确认你的项目是否有其他依赖引入了无FluentFuture类的旧版Guava(比如18.0及更早版本):
- 运行Maven命令查看依赖树,定位Guava版本:
mvn dependency:tree | grep guava - 如果发现版本冲突,在
pom.xml的dependencyManagement中锁定Guava版本为23.0,强制所有依赖使用该版本:<dependencyManagement> <dependencies> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>23.0</version> </dependency> </dependencies> </dependencyManagement>
2. 正确配置Maven Compiler插件的注解处理器路径
Maven编译插件处理多个注解处理器时,必须明确将所有处理器及其依赖加入annotationProcessorPaths(不能仅放在普通dependencies中),确保处理器运行时的classpath包含所有必需组件。
修改pom.xml中的maven-compiler-plugin配置:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>1.8</source> <target>1.8</target> <!-- 配置所有注解处理器及其依赖 --> <annotationProcessorPaths> <!-- Dagger 2.15注解处理器 --> <path> <groupId>com.google.dagger</groupId> <artifactId>dagger-compiler</artifactId> <version>2.15</version> </path> <!-- 你的自定义注解处理器 --> <path> <groupId>你的自定义处理器GroupId</groupId> <artifactId>你的自定义处理器ArtifactId</artifactId> <version>你的版本号</version> </path> <!-- 加入Guava依赖,确保处理器能找到FluentFuture --> <path> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>23.0</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>
3. 验证配置并重新编译
完成配置后,清理编译缓存并重新构建项目:
mvn clean compile
如果问题仍存在,可以检查自定义注解处理器的依赖配置:确保处理器没有依赖与Dagger冲突的Guava版本,或者将自定义处理器中的Guava依赖标记为provided,避免重复引入不同版本。
内容的提问来源于stack exchange,提问作者Horbert
相关产品推荐
相关产品推荐

