Description
Context
Built-in Kotlin is a new feature in the Android Gradle plugin which allows Android developers to use Kotlin without having to apply the kotlin-android
plugin (https://issuetracker.google.com/259523353).
In AGP 9.0 (canaries coming soon), built-in Kotlin support will be enabled by default.
With built-in Kotlin support, users will no longer be able to use the kotlin-kapt
plugin (the build will fail). Instead, to continue using Kapt, users will need to apply the new com.android.legacy-kapt
plugin (introduced in this commit, available in AGP 8.10+).
Problem
com.google.dagger.hilt.android
plugin currently works with the kotlin-kapt
plugin, but doesn't work with AGP's built-in Kotlin and the com.android.legacy-kapt
plugin.
To reproduce this issue:
-
Open the attached project: kapt-android-sample.zip (same project used in scenario 3 of https://issuetracker.google.com/421403256). This project uses AGP's built-in Kotlin +
com.android.legacy-kapt
plugin. -
Run
:app:kaptDebugKotlin
. The build will fail with:
> Task :app:kaptDebugKotlin
kapt-android-sample/app/build/intermediates/built_in_kapt_stubs/debug/kaptGenerateStubsDebugKotlin/com/example/dependency_injection_with_hilt/views/FirstFragment.java:20: error: [Hilt] Expected @AndroidEntryPoint to have a value. Did you forget to apply the Gradle Plugin? (com.google.dagger.hilt.android)
public final class FirstFragment extends androidx.fragment.app.Fragment implements com.example.dependency_injection_with_hilt.adapters.BaseRecyclerClickListener<com.example.dependency_injection_with_hilt.models.ExampleData> {
^
See https://dagger.dev/hilt/gradle-setup.html
[Hilt] Processing did not complete. See error above for details.
- Try using
kotlin-android
andkotlin-kapt
plugins instead:- In
<root project>/build.gradle.kts
, replacealias(libs.plugins.android.kotlin) apply false
withalias(libs.plugins.jetbrains.kotlin.android) apply false
. - In
app/build.gradle.kts
, replacealias(libs.plugins.android.kotlin)
withalias(libs.plugins.jetbrains.kotlin.android)
- In
app/build.gradle.kts
, replaceid("com.android.legacy-kapt")
withkotlin("kapt")
.
- In
4. Run `:app:kaptDebugKotlin` again. The build will succeed.
Potential Root Cause
Hilt currently has a logic to run if the kotlin-kapt
plugin is applied (this code).
To fix this issue, Hilt should probably expand that check to cover com.android.legacy-kapt
plugin as well.