Dagger 2.15:Appcomponent - 无法处理此界面

时间:2018-05-23 14:32:51

标签: android dagger-2

我无法使用以下错误构建我的应用:

 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并且包含了所有依赖项:

    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

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

我总是遇到Dagger这个有限的错误。试图调试任务但不提供任何更多信息。希望其他任何人都可以暗示我的某些事情。

最后一件事。我还使用gradle wrapper 4.7Android 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
}

1 个答案:

答案 0 :(得分:2)

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

希望这对您有帮助