【发布时间】:2021-10-25 18:56:00
【问题描述】:
出于某种原因,Dagger 不会为我的组件生成 DaggerApplicationComponent。我尝试过:重建、清理、使缓存失效、重新启动 Android Studio 等。对我来说没有任何效果。 完整代码如下:
模块
@Module
class AppModule {
@Provides
@Singleton
fun provideContext(): Context = provideContext()
}
@Module
class DatabaseModule {
@Provides
@Singleton
open fun provideRoom(context: Context): RoomDatabase =
Room.databaseBuilder(
context,
AppDatabase::class.java,
DATABASE_NAME
).build()
}
@Module
class NetworkModule {
private val json = Json { ignoreUnknownKeys = true }
private val client = OkHttpClient.Builder()
.addInterceptor(TokenInterceptor)
.build()
@Provides
@Singleton
open fun provideRetrofit(): Retrofit =
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(json.asConverterFactory("application/json".toMediaType()))
.client(client)
.build()
}
组件
@Singleton
@Component(modules = [AppModule::class, DatabaseModule::class, NetworkModule::class])
interface ApplicationComponent {
fun inject(defaultRepository: DefaultRepository)
fun inject(myApplication: MyApplication)
}
也在我使用的 build.gradle 文件中
implementation 'com.google.dagger:dagger-android:2.35.1'
implementation 'com.google.dagger:dagger-android-support:2.35.1' // if you use the support libraries
kapt 'com.google.dagger:dagger-android-processor:2.35.1'
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlinx-serialization'
id 'kotlin-kapt'
}
应用
class MyApplication : Application() {
val myApplication = DaggerApplicationComponent.builder().build()
}
【问题讨论】:
-
你能展示你的应用代码吗?
-
添加到原帖
-
我想你忘了在
build.gradle文件中添加kapt 'com.google.dagger:dagger-compiler:2.x
标签: android dependency-injection dagger-2