android - Kapt,Kotlin,Dagger2 注释处理时出错

标签 android kotlin dagger-2 dagger

在使用 kotlin 的项目中添加 com.google.dagger:dagger-android-support 依赖项后,我遇到了这个 gradle 错误。

这是我构建 gradle 的一部分 build.gradle

    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'realm-android'
    apply plugin: 'me.tatarka.retrolambda'
    apply plugin: 'kotlin-kapt'

    android {
      ...
       kapt {
         generateStubs = true
       }
    }
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    ...
       kapt "com.google.dagger:dagger-compiler:${daggerVersion}"
       kapt "com.google.dagger:dagger-android-processor:${daggerVersion}"
       compile "com.google.dagger:dagger:${daggerVersion}"
       compile "com.google.dagger:dagger-android-support:${daggerVersion}"

       ...
    }

这里是 gradle 错误 我得到了

ApplicationComponent.java:21: error: [dagger.android.AndroidInjector.inject(T)] java.util.Map<java.lang.Class<? extends android.support.v4.app.Fragment>,javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends android.support.v4.app.Fragment>>> cannot be provided without an @Provides-annotated method.
e: 
public interface ApplicationComponent {
    e:        ^
    e: java.lang.IllegalStateException: failed to analyze: org.jetbrains.kotlin.kapt3.diagnostic.KaptError: Error while annotation processing
        at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:57)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:144)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:167)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:55)
        at org.jetbrains.kotlin.cli.common.CLICompiler.exec(CLICompiler.java:182)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl.execCompiler(CompileServiceImpl.kt:397)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl.access$execCompiler(CompileServiceImpl.kt:99)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$2.invoke(CompileServiceImpl.kt:365)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$2.invoke(CompileServiceImpl.kt:99)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$2$$special$$inlined$withValidClientOrSessionProxy$lambda$1.invoke(CompileServiceImpl.kt:798)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$2$$special$$inlined$withValidClientOrSessionProxy$lambda$1.invoke(CompileServiceImpl.kt:99)
        at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:137)

应用组件

@Singleton
@Component(modules = {AndroidInjectionModule.class, ApplicationModule.class, RetrofitModule.class, ActivityBuilderModule.class})
public interface ApplicationComponent {

    @Component.Builder
    interface Builder {

        @BindsInstance
        Builder application(Application application);

        ApplicationComponent build();
    }

    void inject(TaskApplication application);
}

有人遇到过同样的问题吗?

最佳答案

在我使用 android.support.v4.app.Fragment 后使用 AndroidSupportInjectionModule 解决。

@Singleton
@Component(modules = {AndroidSupportInjectionModule.class, ApplicationModule.class, RetrofitModule.class, ActivityBuilderModule.class})
public interface ApplicationComponent {

    @Component.Builder
    interface Builder {

        @BindsInstance
        Builder application(Application application);

        ApplicationComponent build();
    }

    void inject(TaskApplication application);
}

关于android - Kapt,Kotlin,Dagger2 注释处理时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45593146/

相关文章:

android - 使用 ViewPager2 获取当前 fragment

android - 无法将自定义 listSelector 应用于 ListView

android - GLES20.glUniform4fv和fragshader

Kotlin 协程 : concurrent execution throttling

android - <identifier> 预期的 android Hilt

android - Dagger 2 的依赖注入(inject)

java - Android Kotlin Dagger 2 组件未生成

android - 保存并读取图像,但是打开文件夹时找不到

android - 如何获取谷歌地图API key

android - 如何在订阅 liveData 后仅针对新更新观察 liveData