未解决的参考:DaggerApplicationComponent
Unresolved reference: DaggerApplicationComponent
出于某种原因,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()
}
由于您使用的是 Dagger Android,看来您将 app 模块注入应用程序的方式是错误的。
请更正如下:
在app组件中,应该继承自AndroidInjector
,具体如下:
app_component.kt
@Singleton
@Component(modules = [AppModule::class, DatabaseModule::class, NetworkModule::class])
interface ApplicationComponent : AndroidInjector<MyApplication> {
@Component.Factory
abstract class Factory : AndroidInjector.Factory<MyApplication>
}
接下来是MyApplication
。当你使用匕首 android 时,你应该从 DaggerApplication()
扩展。就这样:
my_application.kt
class MyApplication : DaggerApplication() {
override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
return DaggerAppComponent.factory().create(this)
}
}
最后,您应该清理并重建您的项目。
还有一件事,确保添加所有 Dagger android 库。
应用build.gradle.kts
plugins {
id("kotlin-kapt")
}
dependencies {
implementation("com.google.dagger:dagger:2.37")
implementation("com.google.dagger:dagger-android:2.37")
implementation("com.google.dagger:dagger-android-support:2.37")
kapt("com.google.dagger:dagger-android-processor:2.37")
kapt("com.google.dagger:dagger-compiler:2.37")
}
出于某种原因,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()
}
由于您使用的是 Dagger Android,看来您将 app 模块注入应用程序的方式是错误的。
请更正如下:
在app组件中,应该继承自AndroidInjector
,具体如下:
app_component.kt
@Singleton
@Component(modules = [AppModule::class, DatabaseModule::class, NetworkModule::class])
interface ApplicationComponent : AndroidInjector<MyApplication> {
@Component.Factory
abstract class Factory : AndroidInjector.Factory<MyApplication>
}
接下来是MyApplication
。当你使用匕首 android 时,你应该从 DaggerApplication()
扩展。就这样:
my_application.kt
class MyApplication : DaggerApplication() {
override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
return DaggerAppComponent.factory().create(this)
}
}
最后,您应该清理并重建您的项目。
还有一件事,确保添加所有 Dagger android 库。
应用build.gradle.kts
plugins {
id("kotlin-kapt")
}
dependencies {
implementation("com.google.dagger:dagger:2.37")
implementation("com.google.dagger:dagger-android:2.37")
implementation("com.google.dagger:dagger-android-support:2.37")
kapt("com.google.dagger:dagger-android-processor:2.37")
kapt("com.google.dagger:dagger-compiler:2.37")
}