Skip to content

Dagger Hilt plugin doesn't work with com.android.legacy-kapt plugin #4756

Open
@hungvietnguyen

Description

@hungvietnguyen

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:

  1. 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.

  2. 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.
  1. Try using kotlin-android and kotlin-kapt plugins instead:
    • In <root project>/build.gradle.kts, replace alias(libs.plugins.android.kotlin) apply false with alias(libs.plugins.jetbrains.kotlin.android) apply false.
    • In app/build.gradle.kts, replace alias(libs.plugins.android.kotlin) with alias(libs.plugins.jetbrains.kotlin.android)
    • In app/build.gradle.kts, replace id("com.android.legacy-kapt") with kotlin("kapt").
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions