dagger-2 - 错误 : cannot find symbol @dagger. 模块(包括 = {InflationInject_ViewModule.class})

标签 dagger-2 generated-code assisted-inject

我试过实现 jack 沃顿的 AssistedInject在我的项目中使用 Dagger 2 ( https://github.com/square/AssistedInject )。

我的代码与 https://github.com/square/AssistedInject/tree/master/inflation-inject-sample/src/main/java/com/example 几乎相同但我得到了错误:

error: cannot find symbol @dagger.Module(includes = {InflationInject_ViewModule.class})



好像是 ViewModule生成的代码不知道在哪里找到 InflationInject_ViewModule :
@InflationModule
@Module(includes = [InflationInject_ViewModule::class])
abstract class ViewModule

我的 build.gradle 的相关位是
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {...}

dependencies {
  implementation "com.google.dagger:dagger-android:2.16"
  implementation "com.google.dagger:dagger-android-support:2.16"
  kapt "com.google.dagger:dagger-compiler:2.16"
  kapt "com.google.dagger:dagger-android-processor:2.16"
  implementation 'com.squareup.inject:inflation-inject:0.3.0'
  kapt 'com.squareup.inject:inflation-inject-processor:0.3.0'
}

我已经检查过 InflationInject_ViewModule确实存在于生成的代码中(在 build/generated/source/kapt/devDebug/com/project/di/ ,所以它可能与编译器查找源集/生成代码的位置有关。

有什么建议么?

最佳答案

我使用 AssistedInject,面临同样的问题。就我而言,结果证明我(或您的)代码没有问题,这是 kapt 问题。

如果您使用一些需要 kapt 的库(例如房间、数据绑定(bind)),这些库可能会在 AssistedInject 之前处理。就像是:
数据绑定(bind) -> 房间 -> AssistedInject
如果房间编译失败,AssistedInject 永远不会为此编译 InflationInject_ViewModule.java 没有生成

示例错误:

e: AssistedInjectModule.java:7: error: cannot find symbol
@dagger.Module(includes = {AssistedInject_AssistedInjectModule.class})
                           ^
  symbol: class AssistedInject_AssistedInjectModule
e: error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
  Tried the following constructors but they failed to match:
  Integer(int) -> [param:value -> matched field:unmatched]
  Integer(java.lang.String) -> [param:arg0 -> matched field:unmatched] - java.lang.Integer
e: error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type). - java.lang.Integer
e: TaskDao.java:53: error: Not sure how to convert a Cursor to this method's return type (java.lang.Integer).
    public abstract java.lang.Object updateComplete(@org.jetbrains.annotations.NotNull()
                                     ^
e: AssistedInjectModule.java:8: error: @AssistedModule's @Module must include AssistedInject_AssistedInjectModule
public final class AssistedInjectModule {
             ^
e: AppComponent.java:8: error: [ComponentProcessor:MiscError] 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 abstract interface AppComponent extends dagger.android.AndroidInjector<com.sample.todo.TodoApplication> {
                ^

结论 :查看项目中需要注释处理器的其他库。确保他们的设置是正确的。

关于dagger-2 - 错误 : cannot find symbol @dagger. 模块(包括 = {InflationInject_ViewModule.class}),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53322693/

相关文章:

android - 错误 : Program type already present: android. support.design.widget.CoordinatorLayout$Behavior

eclipse - 将多个模块 Maven 2 项目导入 Eclipse 工作区时出现的问题

java - Maven 在不同项目中生成源

java - Guice:辅助注入(inject)和提供者

java - 如何在 Guice 中注入(inject)使用辅助注入(inject)创建的对象?

android - 如何将增量构建添加到 Dagger 2

android - 我应该为 Dagger 2 中的每个 Activity 使用一个组件吗?

android - 这是在单元测试中使用 Dagger 2 for Android 应用程序以使用模拟/伪造覆盖依赖项的正确方法吗?

dart - dart 生成的库是如何生成的?

java - Guice AssistedInject 不会注入(inject)工厂