Dagger 2.15: Appcomponent - was unable to process this interface

喜夏-厌秋 提交于 2021-01-28 06:19:06

问题


I cannot build my app with the following error:

 Task :app:kaptDebugKotlin
        debug/AppComponent.java:7: error: [ComponentProcessor:MiscError] dagger.internal.codegen.ComponentProcessor was unable to process this interface because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public abstract interface AppComponent {
            ^

I'm using dagger 2.15 and got all of it's dependencies included:

    implementation deps.dagger.runtime
    implementation deps.dagger.android
    implementation deps.dagger.android_support
    kapt deps.dagger.android_support_compiler
    kapt deps.dagger.compiler

Code of AppComponent.kt

@Singleton
@Component(
    modules = [
        AndroidInjectionModule::class,
        AppModule::class,
        MainActivityModule::class]
)
interface AppComponent {
    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(application: Application): Builder
        fun build(): AppComponent
    }

    fun inject(app: App)
}

Code of AppModule.kt:

@Module(includes = [ViewModelModule::class])
class AppModule {

    @Provides
    @Singleton
    fun provideApplication(app: App): Context = app

    @Singleton
    @Provides
    fun provideRetrofitService(): RetrofitService {
        return Retrofit.Builder()
            .baseUrl("https://dog.ceo/api/")
            .client(createClient())
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(LiveDataCallAdapterFactory())
            .build()
            .create(RetrofitService::class.java)
    }


    @Singleton
    @Provides
    fun provideDb(app: Application): CacheDb {
        return Room
            .databaseBuilder(app, CacheDb::class.java, "cache.db")
            .fallbackToDestructiveMigration()
            .build()
    }

    @Singleton
    @Provides
    fun provideUserDao(db: CacheDb): UserDao {
        return db.dogImagesDao()
    }

    private fun createClient(): OkHttpClient {
        val logger = HttpLoggingInterceptor.Logger { message -> Timber.tag("OkHttp").d(message) }

        val interceptor = HttpLoggingInterceptor(logger)
        interceptor.level = HttpLoggingInterceptor.Level.BODY

        return OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .connectTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .build()
    }
}

Code of MainActivityModule.kt

@Suppress("unused")
@Module
abstract class MainActivityModule {
    @ContributesAndroidInjector(modules = [FragmentBuildersModule::class])
    abstract fun contributeMainActivity(): MainActivity
}

Code of FragmentBuildersModule.kt

@Suppress("unused")
@Module
abstract class FragmentBuildersModule {
    @ContributesAndroidInjector
    abstract fun contributeUserFragment(): UserFragment
}

I always run into this limited errors with Dagger. Tried to debug the task but doesn't give anymore information. Hopefully anyone else can hint me at something.

One last thing. I'm also using the gradle wrapper 4.7 and Android Studio 3.1.2

UPDATE:

ViewModelModule.kt

@Suppress("unused")
@Module
abstract class ViewModelModule {
    @Binds
    @IntoMap
    @ViewModelKey(UserViewModel::class)
    abstract fun bindUserViewModel(userViewModel: UserViewModel): ViewModel

    @Binds
    abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory
}

回答1:


I had the exact same code copied from Google sample GitHub and experienced the same problem. The way I solved it, as it turns out, is to clear all other errors in your program, especially those related to the imported modules. So I had problem with my Room database part of the code and had build-time errors. Initially I thought it's unrelated to the AppComponent error. But after I cleared them, everything was fine. It kind of makes sense, an unsuccessfully built module is a dependency that cannot be resolved. And probably the compilation errors in the error message meant errors in general.

Hope this will help you



来源:https://stackoverflow.com/questions/50491086/dagger-2-15-appcomponent-was-unable-to-process-this-interface

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!