我花了一些时间来解决我的错误,但没有任何积极的结果,所以我也许有人可以提供帮助。我的项目中有 3 个模块:app (android)、data、domain - 都是纯 kotlin。所有 DI 都在 app 模块中。
应用组件和应用模块:
@Singleton
@Component(modules = arrayOf(ApplicationModule::class,
NavigatorModule::class,
RepositoryModule::class,
FirebaseModule::class,
SchedulerModule::class,
UseCaseModule::class))
interface ApplicationComponent {
fun injectApplication(application: Application)
fun loginSubComponent() : LoginSubComponent
}
@Module
class ApplicationModule(private val application: Application) {
@Provides
@Singleton
fun provideApplication() = application
}
@Module
class UseCaseModule {
@Provides
@Singleton
fun provideLoginAccountUseCase(scheduler: ComposeScheduler, repository: AccountRepository) = LoginAccountUseCase(scheduler, repository)
@Provides
@Singleton
fun provideLoginAnonymousUseCase(scheduler: ComposeScheduler, repository: AccountRepository) = LoginAnonymousUseCase(scheduler, repository)
}
登录子组件和登录模块:
@ActivityScope
@Subcomponent(modules = arrayOf(LoginModule::class))
interface LoginSubComponent {
fun inject(activity: LoginActivity)
}
@Module
class LoginModule {
@Provides
@ActivityScope
fun provideLoginPresenter(loginAccountUseCase: LoginAccountUseCase, loginAnonymousUseCase: LoginAnonymousUseCase)
= LoginPresenter(loginAccountUseCase, loginAnonymousUseCase)
}
我在 LoginActivity 中注入它,如下所示:
@Inject
lateinit var navigator: Navigator
@Inject <- This inject causes issue
lateinit var presenter: LoginPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
appComponent.loginSubComponent().inject(this)
initView()
.....
}
此配置导致错误:
/home/user/projects/xxx/app/build/tmp/kapt3/stubs/debug/xxx/presentation/di/component/ApplicationComponent.java:6: error: [xxx.presentation.di.component.LoginSubComponent.inject(xxx.presentation.login.LoginActivity)] xxx.presentation.login.LoginPresenter cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
e:
e: public abstract interface ApplicationComponent {
e: ^
e: xxx.presentation.login.LoginPresenter is injected at
e: xxx.presentation.login.LoginActivity.presenter
e: xxx.presentation.login.LoginActivity is injected at
e: xxx.presentation.di.component.LoginSubComponent.inject(activity)
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:137)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:158)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:61)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:107)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:51)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:92)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$2.invoke(CompileServiceImpl.kt:386)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$2.invoke(CompileServiceImpl.kt:96)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:892)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:96)
at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:137)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:919)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.doCompile(CompileServiceImpl.kt:891)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:385)
at sun.reflect.GeneratedMethodAccessor88.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.jetbrains.kotlin.kapt3.diagnostic.KaptError: Error while annotation processing
at org.jetbrains.kotlin.kapt3.AnnotationProcessingKt.doAnnotationProcessing(annotationProcessing.kt:90)
at org.jetbrains.kotlin.kapt3.AnnotationProcessingKt.doAnnotationProcessing$default(annotationProcessing.kt:42)
at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.runAnnotationProcessing(Kapt3Extension.kt:205)
at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.analysisCompleted(Kapt3Extension.kt:166)
at org.jetbrains.kotlin.kapt3.ClasspathBasedKapt3Extension.analysisCompleted(Kapt3Extension.kt:82)
at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM$analyzeFilesWithJavaIntegration$2.invoke(TopDownAnalyzerFacadeForJVM.kt:96)
at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:106)
at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:83)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:376)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:67)
at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:96)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:367)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:132)
... 29 more
由于这个错误,我现在不能使用 DI,而且我几乎可以肯定 Java 版本在这个配置下应该可以正常工作。你有什么建议吗?