Dagger 2 无法访问 Retrofit
Dagger 2 cannot access Retrofit
我正在尝试使用 Dagger 2(带有 Android 模块)向我的存储库提供 Retrofit 实例。购买我遇到错误:
Error:cannot access Retrofit
Picasso 等其他实例注入成功,我只是 Retrofit 有问题。
我的模块
@Module
class NetworkModule {
@Provides
@Singleton
fun providesRetrofit(): Retrofit {
return Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(URL_BASE)
.build()
}
@Provides
@Singleton
fun providesPicasso(context: Context): Picasso {
return Picasso.Builder(context)
.loggingEnabled(true)
.build()
}
@Provides
@Singleton
fun providesRemoteRepository(retrofit: Retrofit): RemoteRepository = RemoteRepository(retrofit)
@Provides
@Singleton
fun providesRepository(remote: RemoteRepository): Repository = RepositoryImp(remote)
}
我的应用程序组件
@Singleton
@Component(modules = arrayOf(
AppModule::class, NetworkModule::class, AndroidInjectionModule::class, ActivityBuilder::class
))
interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun application(app: Application): Builder
fun build(): AppComponent
}
fun inject(application: AppApplication)
}
应用组件
@Module
class AppModule {
@Provides
@Singleton
fun providesApplicationContext(app: Application): Context = app
}
Activity 生成器
@Module
abstract class ActivityBuilder {
@ContributesAndroidInjector(modules = arrayOf(MainActivityModule::class))
abstract fun bindMainActivity(): MainActivity
}
我的存储库'
class RepositoryImp @Inject constructor(val remoteRepository: RemoteRepository) : Repository {
override fun fetchData() {
}
}
我的远程存储库
class RemoteRepository @Inject constructor(retrofit: Retrofit) {
val service: ApiService
init {
service = retrofit.create(ApiService::class.java)
}
}
Gradle日志
e: error: cannot access Retrofit
e:
e: class file for retrofit2.Retrofit not found
e: Consult the following stack trace for details.
e: com.sun.tools.javac.code.Symbol$CompletionFailure: class file for retrofit2.Retrofit not found
w: warning: Supported source version 'RELEASE_7' from annotation processor 'android.arch.lifecycle.LifecycleProcessor' less than -source '1.8'
w:
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:138)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:154)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:58)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:103)
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.invoke(CompileServiceImpl.kt:386)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile.invoke(CompileServiceImpl.kt:96)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda.invoke(CompileServiceImpl.kt:889)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda.invoke(CompileServiceImpl.kt:96)
at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:137)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:916)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.doCompile(CompileServiceImpl.kt:888)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:385)
at sun.reflect.GeneratedMethodAccessor86.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.run(Transport.java:200)
at sun.rmi.transport.Transport.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[=16=](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.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.invoke(KotlinToJVMBytecodeCompiler.kt:377)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze.invoke(KotlinToJVMBytecodeCompiler.kt:68)
at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:96)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:368)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:133)
... 29 more
:app:kaptDebugKotlin FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> Internal compiler error. See log for more details
谢谢!
我发现了问题,在 build.gradle ...
我的改造实例在一个单独的模块中。这个模块是 api:
应用build.gradle
api project(':data')
并且我在改造依赖项中使用了实现。我改成了api,问题就解决了。
api "com.squareup.retrofit2:retrofit:$retrofit_version"
api "com.squareup.retrofit2:converter-gson:$retrofit_version"
接受的答案有效,但使用 api
函数会将 data
模块使用的所有其他库公开给 app
模块。为避免这种情况,只需将改装依赖项添加到您的应用程序模块或您需要依赖项的地方。
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
上面转到 app
模块的 Gradle 文件。如果您的 Retrofit 实例还具有其他依赖项,例如 RxJava2CallAdapterFactory
,请不要忘记也添加这些依赖项。
如果您不想在 appModule 中包含改装依赖项,您应该创建一个新的 class 其中包含改装实例:
@Singleton
class RetrofitContainer {
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
companion object {
private const val BASE_URL = "https://jsonplaceholder.typicode.com"
}
}
然后,在你的 dagger 模块中添加以下依赖项:
@Module
@InstallIn(SingletonComponent::class)
object NetworkingModule {
@Singleton
@Provides
fun provideRetrofit(): RetrofitContainer = RetrofitContainer()
@Singleton
@Provides
fun providePhotoApi(
retrofitContainer: RetrofitContainer
): PhotoApi {
return retrofitContainer.retrofit.create(PhotoApi::class.java)
}
}
我正在尝试使用 Dagger 2(带有 Android 模块)向我的存储库提供 Retrofit 实例。购买我遇到错误:
Error:cannot access Retrofit
Picasso 等其他实例注入成功,我只是 Retrofit 有问题。
我的模块
@Module
class NetworkModule {
@Provides
@Singleton
fun providesRetrofit(): Retrofit {
return Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(URL_BASE)
.build()
}
@Provides
@Singleton
fun providesPicasso(context: Context): Picasso {
return Picasso.Builder(context)
.loggingEnabled(true)
.build()
}
@Provides
@Singleton
fun providesRemoteRepository(retrofit: Retrofit): RemoteRepository = RemoteRepository(retrofit)
@Provides
@Singleton
fun providesRepository(remote: RemoteRepository): Repository = RepositoryImp(remote)
}
我的应用程序组件
@Singleton
@Component(modules = arrayOf(
AppModule::class, NetworkModule::class, AndroidInjectionModule::class, ActivityBuilder::class
))
interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun application(app: Application): Builder
fun build(): AppComponent
}
fun inject(application: AppApplication)
}
应用组件
@Module
class AppModule {
@Provides
@Singleton
fun providesApplicationContext(app: Application): Context = app
}
Activity 生成器
@Module
abstract class ActivityBuilder {
@ContributesAndroidInjector(modules = arrayOf(MainActivityModule::class))
abstract fun bindMainActivity(): MainActivity
}
我的存储库'
class RepositoryImp @Inject constructor(val remoteRepository: RemoteRepository) : Repository {
override fun fetchData() {
}
}
我的远程存储库
class RemoteRepository @Inject constructor(retrofit: Retrofit) {
val service: ApiService
init {
service = retrofit.create(ApiService::class.java)
}
}
Gradle日志
e: error: cannot access Retrofit
e:
e: class file for retrofit2.Retrofit not found
e: Consult the following stack trace for details.
e: com.sun.tools.javac.code.Symbol$CompletionFailure: class file for retrofit2.Retrofit not found
w: warning: Supported source version 'RELEASE_7' from annotation processor 'android.arch.lifecycle.LifecycleProcessor' less than -source '1.8'
w:
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:138)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:154)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:58)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:103)
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.invoke(CompileServiceImpl.kt:386)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile.invoke(CompileServiceImpl.kt:96)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda.invoke(CompileServiceImpl.kt:889)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda.invoke(CompileServiceImpl.kt:96)
at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:137)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:916)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.doCompile(CompileServiceImpl.kt:888)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:385)
at sun.reflect.GeneratedMethodAccessor86.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.run(Transport.java:200)
at sun.rmi.transport.Transport.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[=16=](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.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.invoke(KotlinToJVMBytecodeCompiler.kt:377)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze.invoke(KotlinToJVMBytecodeCompiler.kt:68)
at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:96)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:368)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:133)
... 29 more
:app:kaptDebugKotlin FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> Internal compiler error. See log for more details
谢谢!
我发现了问题,在 build.gradle ... 我的改造实例在一个单独的模块中。这个模块是 api:
应用build.gradle
api project(':data')
并且我在改造依赖项中使用了实现。我改成了api,问题就解决了。
api "com.squareup.retrofit2:retrofit:$retrofit_version"
api "com.squareup.retrofit2:converter-gson:$retrofit_version"
接受的答案有效,但使用 api
函数会将 data
模块使用的所有其他库公开给 app
模块。为避免这种情况,只需将改装依赖项添加到您的应用程序模块或您需要依赖项的地方。
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
上面转到 app
模块的 Gradle 文件。如果您的 Retrofit 实例还具有其他依赖项,例如 RxJava2CallAdapterFactory
,请不要忘记也添加这些依赖项。
如果您不想在 appModule 中包含改装依赖项,您应该创建一个新的 class 其中包含改装实例:
@Singleton
class RetrofitContainer {
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
companion object {
private const val BASE_URL = "https://jsonplaceholder.typicode.com"
}
}
然后,在你的 dagger 模块中添加以下依赖项:
@Module
@InstallIn(SingletonComponent::class)
object NetworkingModule {
@Singleton
@Provides
fun provideRetrofit(): RetrofitContainer = RetrofitContainer()
@Singleton
@Provides
fun providePhotoApi(
retrofitContainer: RetrofitContainer
): PhotoApi {
return retrofitContainer.retrofit.create(PhotoApi::class.java)
}
}