Robolectric AndroidX片段NoClassDefFoundError [英] Robolectric AndroidX fragments NoClassDefFoundError
问题描述
在将代码和测试迁移到 AndroidX 之后,一切似乎都运行良好,但是由于 NoClassDefFoundError:androidx/fragment/testing/R $,Robolectric junit测试两个片段失败样式. 堆栈跟踪:
After migration of code and tests to AndroidX, all seems to work pretty well, however Robolectric junit tests for two fragments are failing due to NoClassDefFoundError: androidx/fragment/testing/R$style exception. The stack trace:
java.lang.NoClassDefFoundError:androidx/fragment/testing/R $ style at androidx.fragment.app.testing.FragmentScenario $ EmptyFragmentActivity.onCreate(FragmentScenario.java:79) 在android.app.Activity.performCreate(Activity.java:5933)处 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 在 androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:674) 在 org.robolectric.android.controller.ActivityController.lambda $ create $ 0(ActivityController.java:69) 在 org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:365) 在 org.robolectric.android.controller.ActivityController.create(ActivityController.java:69) 在 org.robolectric.android.controller.ActivityController.create(ActivityController.java:74) 在 org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:39) 在 androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:207) 在 androidx.fragment.app.testing.FragmentScenario.internalLaunch(FragmentScenario.java:283) 在 androidx.fragment.app.testing.FragmentScenario.launchInContainer(FragmentScenario.java:265)
java.lang.NoClassDefFoundError: androidx/fragment/testing/R$style at androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity.onCreate(FragmentScenario.java:79) at android.app.Activity.performCreate(Activity.java:5933) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:674) at org.robolectric.android.controller.ActivityController.lambda$create$0(ActivityController.java:69) at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:365) at org.robolectric.android.controller.ActivityController.create(ActivityController.java:69) at org.robolectric.android.controller.ActivityController.create(ActivityController.java:74) at org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:39) at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:207) at androidx.fragment.app.testing.FragmentScenario.internalLaunch(FragmentScenario.java:283) at androidx.fragment.app.testing.FragmentScenario.launchInContainer(FragmentScenario.java:265)
为了测试片段,我正在使用FragmentScenario,似乎FragmentScenario.EmptyFragmentActivity指向包androidx.fragment.testing.R中缺少的R类:
For testing fragments I'm using FragmentScenario, and it seems the FragmentScenario.EmptyFragmentActivity refers to a missing R class from a package androidx.fragment.testing.R:
setTheme(getIntent().getIntExtra(THEME_EXTRAS_BUNDLE_KEY, R.style.FragmentScenarioEmptyFragmentActivityTheme));
setTheme(getIntent().getIntExtra(THEME_EXTRAS_BUNDLE_KEY, R.style.FragmentScenarioEmptyFragmentActivityTheme));
任何主意可能有什么问题吗?也许我错过了一个对我而言并不那么明显的依赖关系.
Any idea what might be wrong? Maybe I'm missing a dependency that's not that obvious to me.
可在以下项目中复制: https://github.com/marcinbak/androidx-test-error
Reproducible in following project: https://github.com/marcinbak/androidx-test-error
也在Google的问题跟踪器中进行了报告: https://issuetracker.google.com/issues/122321150
Also reported in Google's issue tracker: https://issuetracker.google.com/issues/122321150
推荐答案
根据Google的回答,要求包含 androidx_fragment_test ( androidx.fragment:fragment-testing:1.1.0 -alpha03 )在测试APK中(而不是在工具APK或测试依赖项中).
According to answer from Google it is required to include androidx_fragment_test (androidx.fragment:fragment-testing:1.1.0-alpha03) artifact in the testing APK (not in instrumentation APK or testing dependency).
这意味着它必须作为 debugImplementation androix_fragment_test 添加到您的build.gradle文件中.
What this means it has to be added as debugImplementation androix_fragment_test in your build.gradle file.
如果您还在发布目标上运行测试,则必须添加实施和roix_fragment_test .
If you also run tests on release target then you have to add implementation androix_fragment_test.
您可以在Google的问题跟踪器中查看整个对话: https://issuetracker.google.com/issues/122321150
You can see the whole conversation in Google's issue tracker: https://issuetracker.google.com/issues/122321150
这篇关于Robolectric AndroidX片段NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!