Dagger 2.15:Appcomponent-无法处理此界面 [英] Dagger 2.15: Appcomponent - was unable to process this interface

查看:77
本文介绍了Dagger 2.15:Appcomponent-无法处理此界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法构建我的应用,并出现以下错误:

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 {
            ^

我正在使用匕首2.15,并包括了所有依赖项:

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

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

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

MainActivityModule.kt

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

FragmentBuildersModule.kt的代码

Code of FragmentBuildersModule.kt

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

我总是在Dagger中遇到这种有限的错误.试图调试任务,但不再提供任何信息.希望任何人都可以暗示我一些事情.

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.

最后一件事.我还使用了 gradle wrapper 4.7 Android Studio 3.1.2

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

更新:

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
}

推荐答案

我从Google示例GitHub复制了完全相同的代码,并遇到了相同的问题.事实证明,我解决该问题的方法是清除程序中的所有 other 错误,尤其是与导入模块有关的错误.因此,我的Room数据库部分代码出现了问题,并且出现了构建时错误.最初,我认为它与 AppComponent 错误无关.但是,在我清除它们之后,一切都很好.从某种意义上说,构建失败的模块是无法解析的依赖项.错误消息中的编译错误可能是一般性错误.

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.

希望这对您有帮助

这篇关于Dagger 2.15:Appcomponent-无法处理此界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆