Skip to content

Hilt throws an exception when starting instrumented test if application is already installed #5143

@MilanVidic

Description

@MilanVidic

If application is already installed on the emulator, when triggering any instrumented test through Android Studio play button ▶, Hilt throws an exception:
java.lang.RuntimeException: Unable to start activity ComponentInfo{###/###.MainActivity}: java.lang.IllegalStateException: The component was not created. Check that you have added the HiltAndroidRule.

  • If application is not already installed on the emulator, when triggering any instrumented test, test runs normally.
  • If any instrumented test is run via terminal and gradle command ./gradlew :module:connectedDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=classUnderTest#test, test runs normally independent if app is installed or not.
  • If any instrumented test is run on emulator with Android 13 it runs normally, fails on Android 15. Android 14 not tested. Some other repo which uses same Hilt setup runs normally on Android 15. Emulator runs custom AOSP 13/15.

Usually this exception means something is not correctly set up in the test, but I believe everything is fine there since it runs normally on Android 13, through gradle command and if app is not installed. Issue is somewhere in our setup/Android version/Android studio gradle config.

We use kapt, but when I migrated to ksp, same behavior occurred.

Tested on different versions of Android Studio(latest included, Panda 2 | 2025.3.2), same behavior.

hilt: 2.58
kotlin: 2.2.20
jvmTarget: 17
agp:8.13.0

Unfortunately I do not have minimal reproducible code.

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