Alex Petev
Alex Petev

Reputation: 489

Unresolved reference: DaggerAppComponent after converting to Kotlin

I am converting an old codebase from Java to Kotlin, but I'm struggling with implementing the Dagger components. I have tried multiple solutions and imports, however Android Studio refuses to recognize DaggerAppComponent. This is my 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
}

}

This is my AppComponent class:

@Singleton
@Module(includes = [AppModule::class, NetModule::class])
interface AppComponent {
    fun inject(activity: SplashActivity?)
    ***
    fun inject(dialogFragment: AddToPlannerDialogFragment?)
}

And finally my imports:

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"

Upvotes: 1

Views: 2621

Answers (1)

Hylke
Hylke

Reputation: 705

Isn't the issue that you define your component with the @Module annotation?

I think you should define your AppComponent as being a @Component like so:

@Singleton
@Component(modules = [AppModule::class, NetModule::class])
interface AppComponent {
  ...
}

Also, if this is not the solution, I would advise to read the build output carefully, often thats very helpful in finding dagger issues.

Upvotes: 1

Related Questions