我正在将一个旧的代码库从Java转换为Kotlin,但我很难实现Dagger组件。我尝试了多种解决方案和导入,但安卓工作室拒绝识别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类:
@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问题非常有帮助。