未解决的参考:转换为 Kotlin 后的 DaggerAppComponent
Unresolved reference: DaggerAppComponent after converting to Kotlin
我正在将旧代码库从 Java 转换为 Kotlin,但我正在努力实现 Dagger 组件。我尝试了多种解决方案和导入,但是 Android Studio 拒绝识别 DaggerAppComponent。这是我的 AppController
class AppController : Application() {
var appComponent: AppComponent? = null
private set
override fun onCreate() {
super.onCreate()
instance = this
FacebookSdk.sdkInitialize(applicationContext)
AppEventsLogger.activateApp(this)
initRealmConfiguration()
initDaggerComponent()
initNewFonts()
}
private fun initRealmConfiguration() {
Realm.init(this)
val realmConfiguration = RealmConfiguration.Builder()
.name(Realm.DEFAULT_REALM_NAME)
.schemaVersion(1)
.migration(MyMigrations())
.deleteRealmIfMigrationNeeded()
.build()
Realm.setDefaultConfiguration(realmConfiguration)
}
private fun initDaggerComponent() {
appComponent = DaggerAppComponent.builder()
.appModule(AppModule(this))
.netModule(NetModule(BuildConfig.END_POINT))
.build()
}
private fun initNewFonts() {
ViewPump.init(ViewPump.builder()
.addInterceptor(CalligraphyInterceptor(
CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/Rosario-Italic.otf")
.setFontAttrId(R.attr.fontPath)
.build()))
.build())
}
companion object {
@get:Synchronized
var instance: AppController? = null
private set
}
}
这是我的 AppComponent class:
@Singleton
@Module(includes = [AppModule::class, NetModule::class])
interface AppComponent {
fun inject(activity: SplashActivity?)
***
fun inject(dialogFragment: AddToPlannerDialogFragment?)
}
最后是我的导入:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'com.google.dagger:dagger:2.27'
kapt "com.google.dagger:dagger-compiler:2.13"
implementation 'com.google.dagger:dagger-android:2.15'
implementation 'com.google.dagger:dagger-android-support:2.15'
kapt "com.google.dagger:dagger-android-processor:2.13"
问题不是您使用 @Module
注释定义组件吗?
我认为您应该将 AppComponent 定义为 @Component
,如下所示:
@Singleton
@Component(modules = [AppModule::class, NetModule::class])
interface AppComponent {
...
}
此外,如果这不是解决方案,我建议您仔细阅读构建输出,这通常对查找 Dagger 问题很有帮助。
我正在将旧代码库从 Java 转换为 Kotlin,但我正在努力实现 Dagger 组件。我尝试了多种解决方案和导入,但是 Android Studio 拒绝识别 DaggerAppComponent。这是我的 AppController
class AppController : Application() {
var appComponent: AppComponent? = null
private set
override fun onCreate() {
super.onCreate()
instance = this
FacebookSdk.sdkInitialize(applicationContext)
AppEventsLogger.activateApp(this)
initRealmConfiguration()
initDaggerComponent()
initNewFonts()
}
private fun initRealmConfiguration() {
Realm.init(this)
val realmConfiguration = RealmConfiguration.Builder()
.name(Realm.DEFAULT_REALM_NAME)
.schemaVersion(1)
.migration(MyMigrations())
.deleteRealmIfMigrationNeeded()
.build()
Realm.setDefaultConfiguration(realmConfiguration)
}
private fun initDaggerComponent() {
appComponent = DaggerAppComponent.builder()
.appModule(AppModule(this))
.netModule(NetModule(BuildConfig.END_POINT))
.build()
}
private fun initNewFonts() {
ViewPump.init(ViewPump.builder()
.addInterceptor(CalligraphyInterceptor(
CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/Rosario-Italic.otf")
.setFontAttrId(R.attr.fontPath)
.build()))
.build())
}
companion object {
@get:Synchronized
var instance: AppController? = null
private set
}
}
这是我的 AppComponent class:
@Singleton
@Module(includes = [AppModule::class, NetModule::class])
interface AppComponent {
fun inject(activity: SplashActivity?)
***
fun inject(dialogFragment: AddToPlannerDialogFragment?)
}
最后是我的导入:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'com.google.dagger:dagger:2.27'
kapt "com.google.dagger:dagger-compiler:2.13"
implementation 'com.google.dagger:dagger-android:2.15'
implementation 'com.google.dagger:dagger-android-support:2.15'
kapt "com.google.dagger:dagger-android-processor:2.13"
问题不是您使用 @Module
注释定义组件吗?
我认为您应该将 AppComponent 定义为 @Component
,如下所示:
@Singleton
@Component(modules = [AppModule::class, NetModule::class])
interface AppComponent {
...
}
此外,如果这不是解决方案,我建议您仔细阅读构建输出,这通常对查找 Dagger 问题很有帮助。