4

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

我的代码与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

我的 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/,所以也许这与编译器查找源集/生成的代码的位置有关。

有什么建议么?

4

1 回答 1

5

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

如果您使用一些需要 kapt 的库(例如房间、数据绑定),这些库可能会在 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> {
                ^


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

于 2018-12-26T04:53:21.583 回答