Dagger @ContributesAndroidInjector ComponentProcessor 无法处理这个接口

Dagger @ContributesAndroidInjector ComponentProcessor was unable to process this interface

我正在测试 dagger 的新功能:Android 模块。而且我在使用 @ContributesAndroidInjector 时无法编译代码 我总是收到以下错误:

错误:(12, 8) 错误:dagger.internal.codegen.ComponentProcessor 无法处理此接口,因为它的所有依赖项都无法解析。检查编译错误或生成代码的循环依赖。

我尝试像 here 一样实现我的组件,但我仍然遇到错误。

这是最小的例子:

@PerApplication
@Component(modules = {AndroidInjectionModule.class, LoginBindingModule.class})
public interface ApplicationComponent {
    void inject(ExampleApplication application);
}

@Module
public abstract class LoginBindingModule {
    @ContributesAndroidInjector
    abstract LoginActivity contributeYourActivityInjector();
}

public class LoginActivity extends Activity {

    @Inject
    LoginPresenter loginPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        AndroidInjection.inject(this);
        super.onCreate(savedInstanceState);
    }
}

public class LoginPresenter {

    @Inject
    public LoginPresenter() {

    }
}

如果我从 ApplicationComponent 中删除 LoginBindingModule,应用程序将被构建,但会因运行时异常而失败:

java.lang.IllegalArgumentException:Class

没有喷油器工厂

项目设置:

gradle 3.3
buildToolsVersion "25.0.2"
dagger 2.11

annotationProcessor "com.google.dagger:dagger-android-processor:2.11" 添加到您的 gradle 文件将解决您的问题。

在我的例子中 SomeModule class 包含不必要的行:

@ContributesAndroidInjector
internal abstract fun fragmentInjector(): SomeFragment

对于 Kotlin,而不是

annotationProcessor com.google.dagger:dagger-android-processor:2.11

使用

kapt com.google.dagger:dagger-android-processor:2.11

如果 none 的建议解决方案有效,只需检查您是否忘记向任何依赖项添加 @Provides 注释,这就是我的问题

我遇到了同样的错误,但问题出在我声明 Dagger 模块的模块(项目)上。 确保添加 kotlin-kapt 插件,否则 Dagger 将无法生成任何 class.

// declare it at the top of your build.gradle file
apply plugin: 'kotlin-kapt'

我在将模块文件转换为 Kotlin 时遇到了一个非常奇怪的错误。这可能很少见,但也许其他人偶然发现了同样的问题:

我的 Dagger 模块是 Gradle 模块的一部分。它使用仅具有 api Gradle 配置的依赖项。 Dagger 为涉及的每个 Kotlin class 生成存根 (Java) 文件。没有这些潜艇一切正常。对于那些存根,它给出了上述错误。将所有缺少的依赖项添加到 Gradle 模块是我的解决方案。

我遇到了同样的问题,接受的答案对我不起作用。经过大量分析,我发现问题指向其他库,在我的例子中是 butterknife。我的 Dagger 中启用了一个布局变量 activity,如下所示称为 editLenearLayout。

@BindView(R.id.ll_edit11)
    LinearLayout editLenearLayout;

当我删除这两行代码时,令人惊讶的是它起作用了:) 问题是 Butterknife 无法在 布局中绑定 id。但退出因素是 Studio 显示以下错误。

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.

我的线性布局已进入 布局。 pointing to this problem.

可能会拯救某人的一天。

检查您的所有文件是否都指定了包 -> “包 com.something.blahblah...”

我的问题是 duplicate packages(ViewModel 2) 这样的文件。 只需删除它并 clean, rebuild project.