使用 createComposeRule 运行 Jetpack Compose UI 测试时出现“RuntimeException:无法启动活动...无法解析 Intent 的活动”

Rya*_*ley 17 testing android android-jetpack-compose

使用堆栈跟踪运行测试createComposeRule并命中堆栈跟踪(省略不相关部分):

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 }
...
Run Code Online (Sandbox Code Playgroud)

小智 39

OP问题是关于createComposeRule()它的使用不需要自定义活动(它ComposeActivity在幕后使用)。

在这种情况下,您需要将以下内容包含在您的 gradle 文件中:

debugImplementation("androidx.compose.ui:ui-test-manifest:1.0.0-beta05")
Run Code Online (Sandbox Code Playgroud)

如果您查看该包的内容,您会发现它只是一个AndroidManifest.xml带有.<activity/>androidx.activity.ComponentActivity


Rya*_*ley 2

您需要在您的Compose UI 测试中声明一个Activity名称,以用于托管内容。在您的标签中添加以下内容:android.app.ActivityAndroidManifest.xml<application>

<activity android:name="android.app.Activity" android:theme="@style/your_app_theme_here"/>
Run Code Online (Sandbox Code Playgroud)

your_app_theme_here用应用程序中存在的主题替换。