13

Running tests with createComposeRule and hitting a stack trace like (irrelevant parts omitted):

java.lang.RuntimeException: Could not launch activity
at androidx.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:495)
...
Caused by: java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=my.app.package.name.here/android.app.Activity }
...
Ryan M
  • 18,333
  • 31
  • 67
  • 74

3 Answers3

35

The OP question is about the use of createComposeRule() which doesn't require a custom activity (it uses ComposeActivity under the hood).

In this case you need to include this below in your gradle file:

debugImplementation("androidx.compose.ui:ui-test-manifest:1.0.0-beta05")

If you take a look at the contents of that package, it's simply an AndroidManifest.xml with an <activity/> entry for androidx.activity.ComponentActivity.

Mahozad
  • 18,032
  • 13
  • 118
  • 133
  • 1
    I've added the `debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"`. to solve the problem – notionquest Nov 25 '21 at 15:25
  • I lost 3 days figuring this out. I thought adding the manifest to androidTestImplementation would have the same effect, but looks like you can't do that. So what this basically does it gives you a custom activity (androidx.activity.ComponentActivity) from which you can run your tests, but you have to include it via debugImplementation. – Tooroop Nov 28 '21 at 14:56
  • FWIW, I had to add it as implementation() after moving some androidTest code into a module – kenyee Oct 30 '22 at 20:21
3

You need to add

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

to your manifest.

Andre Thiele
  • 3,202
  • 3
  • 20
  • 43
  • I'm running an Android test using createComposeRule() and it works when "androidx.compose.ui:ui-test-manifest:$compose_version" is added, as above. I haven't added ComponentActivity to the manifest. – Ewan Nov 21 '22 at 12:13
2

You need declare an Activity with name android.app.Activity in your AndroidManifest.xml for the Compose UI tests to use to host the content. Add the following within your <application> tag:

<activity android:name="android.app.Activity" android:theme="@style/your_app_theme_here"/>

substituting your_app_theme_here with a theme that exists in your app.

Ryan M
  • 18,333
  • 31
  • 67
  • 74