Android - Jetpack Compose - ResourceNotFoundException



build.gradle:

testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.4-alpha03")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.0-alpha03")
androidTestImplementation("androidx.compose.ui:ui-test-junit4:1.1.0-alpha06")

我的机器人测试课程:

@get:Rule
val composeTestRule = createAndroidComposeRule(MainActivity::class.java)
@Test
fun testUI() {
composeTestRule.setContent {
AppTheme {
SomeComposeUI(

)
}
}
composeTestRule.activity.getString(R.string.apply)
InstrumentationRegistry.getInstrumentation().context.getString(R.string.apply)

}

我遇到的问题是composeTestRule.activity.getString(R.string.apply)InstrumentationRegistry.getInstrumentation().context.getString(R.string.apply)总是返回Method threw 'android.content.res.Resources$NotFoundException'异常。

我试过最新的alpha版本和最新的稳定版本,但没有成功。我甚至尝试过你可以在android工作室导入的compose项目,但也有同样的错误。

注:

  • 使用SDK 31构建
  • 在SDK 31设备上进行测试(与任何SDK设备的问题相同)
  • AGP 7.0.2-等级7.2

如有任何帮助,将不胜感激

事实证明,对于我的项目和android项目,在大多数情况下,当调试器被附加时,你不能使用composeTestRule.activity.getString(R.string.apply)

在没有调试器的情况下运行不会有同样的问题,所以开发会有点困难,但至少我有一个解决方案。