2

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

David Aleksanyan
  • 2,953
  • 4
  • 29
  • 39

2 Answers2

1

Add the following to your AndroidManifest.xml:

<manifest>
  <application>
    ...
    <activity android:name="androidx.activity.ComponentActivity" />
    ...
  <application>
<manifest>

If you have a specific Manifest file for the debug variant, add this to that file instead, as this change isn't needed for the release variant.

cd1
  • 15,908
  • 12
  • 46
  • 47
  • small progress, looks like it's now giving another error,I'm investigating: java.lang.IllegalStateException: Given component holder class androidx.activity.ComponentActivity does not implement interface dagger.hilt.internal.GeneratedComponent or interface dagger.hilt.internal.GeneratedComponentManager at dagger.hilt.EntryPoints.get(EntryPoints.java:62) – David Aleksanyan Feb 09 '22 at 19:21
1

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().

David Aleksanyan
  • 2,953
  • 4
  • 29
  • 39
  • 2
    Initially this seemed to work for me, but somewhat blocked the MainActivity making it unable to receive the initial intent " Could not launch intent Intent { act=android.intent.action.MAIN" cd1 solution worked like a charm. – Miguel Sesma Mar 10 '22 at 12:00