Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2 cannot access Retrofit

Tags:

I'm trying to provide an instance of Retrofit to my Repository using Dagger 2 (with Android module). Buy I'm facing the error:

Error:cannot access Retrofit

Other instances like Picasso was injected with success, I just have problems with Retrofit.

My Module

@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)  } 

My Application component

@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)  } 

App Component

@Module class AppModule {      @Provides     @Singleton     fun providesApplicationContext(app: Application): Context = app  } 

Activity Builder

@Module abstract class ActivityBuilder {      @ContributesAndroidInjector(modules = arrayOf(MainActivityModule::class))     abstract fun bindMainActivity(): MainActivity  } 

My repository'

class RepositoryImp @Inject constructor(val remoteRepository: RemoteRepository) : Repository {      override fun fetchData() {      }  } 

My Remote repository

class RemoteRepository @Inject constructor(retrofit: Retrofit) {      val service: ApiService      init {         service = retrofit.create(ApiService::class.java)     }  } 

Gradle Log

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$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:889)     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: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$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:377)     at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.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 

Thank you!

like image 425
fe_araujo_ Avatar asked Nov 05 '17 18:11

fe_araujo_


2 Answers

I found the problem, was in the build.gradle ... My retrofit instance was in a separate module. This module is a api:

App build.gradle

api project(':data') 

And I was using implementation in the retrofit dependencies. I changed to api and the problem was fixed.

api "com.squareup.retrofit2:retrofit:$retrofit_version" api "com.squareup.retrofit2:converter-gson:$retrofit_version" 
like image 170
fe_araujo_ Avatar answered Oct 07 '22 07:10

fe_araujo_


The accepted answer works but using the api function exposes all the other libraries used by the data module to the app module. To avoid this just add the retrofit dependencies to your app module or where ever you need the dependencies.

implementation "com.squareup.retrofit2:retrofit:$retrofit_version" implementation "com.squareup.retrofit2:converter-gson:$retrofit_version" 

Above goes to the app module's Gradle file. If your Retrofit instance also has other dependencies such as RxJava2CallAdapterFactory do not forget to add these depencies too.

like image 37
Tartar Avatar answered Oct 07 '22 07:10

Tartar