错误:找不到符号@dagger.Module(包括= {InflationInject_ViewModule.class})

Vil*_*lla 4 generated-code assisted-inject dagger-2

我尝试在我的项目中AssistedInject使用 Dagger 2 ( https://github.com/square/AssistedInject ) 实现 Jake Wharton 的。

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

错误:找不到符号@dagger.Module(包括= {InflationInject_ViewModule.class})

生成的代码似乎ViewModule不知道在哪里可以找到InflationInject_ViewModule

@InflationModule
@Module(includes = [InflationInject_ViewModule::class])
abstract class ViewModule
Run Code Online (Sandbox Code Playgroud)

我的 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'
}
Run Code Online (Sandbox Code Playgroud)

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

有什么建议么?

小智 5

我使用 AssistedInject ,面临同样的问题。就我而言,事实证明我的(或你的)代码没有任何问题,这是 kapt 问题。

如果您使用某些需要 kapt 的库(例如 room、数据绑定),这些库可能会在 AssistedInject 之前进行处理。
类似于: databinding -> room -> AssistedInject
如果 room 编译失败,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> {
                ^
Run Code Online (Sandbox Code Playgroud)


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

  • 谢谢。通过在我的 AppComponent 中注释 AssistedInjectionModule,我能够看到阻止构建的*真正的*错误。 (2认同)