Search code examples
androidkotlindagger-2

Kotlin 1.5.0 does not work with Dagger 2?


I have a very simple Dagger code as below

class MainActivity : AppCompatActivity() {
    @Inject
    lateinit var info : Info
    init {
        DaggerMagicBox.create().poke(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

class Info @Inject constructor() {
    val text = "Hello Dagger 2"
}

@Component
interface MagicBox {
    fun poke(mainClass: MainActivity)
}

With plugin as

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

and dependencies as below

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.google.dagger:dagger:2.33'
    kapt 'com.google.dagger:dagger-compiler:2.33'

Where kotlin_version is 1.4.32. It compiles fine.

However when I migrate to kotlin_version = "1.5.0", it just errors out stating

Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
   > java.lang.reflect.InvocationTargetException (no error message)

without any further detail.

What causes this issue? and how can it be resolved?


Solution

  • You can get more detail by invoking gradle with --stacktrace.

    This seems to be an issue that was fixed in dagger 2.34. The newest release is 2.35.1.