androidandroid-jetpack-composedagger-hiltinstrumented-test

ComponentActivity ClassNotFoundException when trying to setup Instrumented tests, with Android Compose and Hilt


I'm setting up my first instrumented unit-test and getting an obscure crash in logcat.

The emulator starts and immediately crashes when the app tries to open and the unit test doesn't even run because the application wasn't in the right state.

2022-02-09 19:30:37.116 25764-25764/com.anotherday.day17.test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.anotherday.day17.test, PID: 25764
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.anotherday.day17.test/androidx.activity.ComponentActivity}: java.lang.ClassNotFoundException: Didn't find class "androidx.activity.ComponentActivity" on path: DexPathList[[zip file "/data/app/~~NodQZs7v97-vYTPte3T7UQ==/com.anotherday.day17.test-25XuwB3vTwyNbSX-nlETDQ==/base.apk"],

It seems to be looking for androidx.activity.ComponentActivity which is defined in the following gradle dependency:

implementation 'androidx.activity:activity-compose:1.3.1'

Not sure where else to look, here's my first test and my project in git: https://github.com/davida5/ComposeNotepad/blob/main/app/src/androidTest/java/com/anotherday/day17/navigation/NavigatorTest.kt


Solution

  • I finally figured out the problem, needed to instantiate the compose rule such as this:

    val composeRule = createAndroidComposeRule<MainActivity>()
    

    instead of

    val composeRule = createComposeRule()
    

    The first (correct) call allows passing in of my Hilt annotated (@AndroidEntryPoint) Activity instead of the default ComponentActivity which is not annotated since it belongs to the framework. That's why it kept complaining about the ComponentActivity, if you look inside the createComposeRule(), that's all it does, call createAndroidComposeRule().