Kapt、Kotlin、Dagger2 注解处理期间的错误

9

在使用Kotlin开发项目时,添加了com.google.dagger:dagger-android-support依赖后,出现了Gradle错误。

以下是我的build.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);
}

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


你进行了干净的构建吗?Kotlin与注解处理器仍然有一些小问题。 - David Medenjak
@DavidMedenjak 没有解决。 - rafaelasguerra
这有帮助吗?https://dev59.com/WFgR5IYBdhLWcg3whtqk#41220138 - Lovis
2个回答

7

在使用 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);
}

5
你尝试在应用级别的gradle文件中添加kapt了吗?
kapt {
    generateStubs = true
}

这可能是解决方案,请查看我的答案: https://dev59.com/WFgR5IYBdhLWcg3whtqk#41220138 使用kapt3(apply plugin: 'kotlin-kapt')不允许使用generateStubs = true - Lovis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接