升级AGP 3.0.1及Android Studio 3.0.1后Espresso测试无法运行求助
Hey there! I’ve dealt with plenty of quirky test failures after Gradle/Studio upgrades, so let’s walk through the most likely fixes for your Espresso issues:
Align Android Test Support Library versions
Gradle 3.0+ enforces stricter dependency version consistency. Make sure all your Espresso-related dependencies use matching versions (for 3.0.1,1.0.2for runner/rules and3.0.2for Espresso core/contrib works well). Update your app-levelbuild.gradlelike this:androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test:rules:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' // Add other Espresso libraries (like contrib, intents) with the same 3.0.2 versionDisable Instant Run
Early Android Studio 3.0 builds had rough compatibility between Instant Run and Espresso. Turn it off by going toFile → Settings → Build, Execution, Deployment → Instant Runand unchecking all enabled options.Verify test class annotations
Double-check your test classes have the required annotations to work with Espresso:@RunWith(AndroidJUnit4.class) public class MainActivityTest { @Rule public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class); @Test public void testSampleAction() { // Your Espresso test logic here onView(withId(R.id.sample_button)).perform(click()); } }Missing
@RunWithor@Rulecan cause silent failures or weird errors.Fix ProGuard/R8 obfuscation
If you’re using obfuscation, ensure Espresso and test classes aren’t stripped. Add these rules to yourproguard-rules.pro:-keep class android.support.test.** { *; } -keep class android.support.test.espresso.** { *; } -keep class org.junit.** { *; }(Swap
android.supportwithandroidxif you’ve migrated to AndroidX.)Clean and rebuild your project
Old build cache often causes unexplained issues. RunBuild → Clean Project, followed byBuild → Rebuild Project, then try running your tests again.Turn off device animations
Espresso struggles with system animations. On your test device/emulator, go toSettings → Developer Optionsand disable:- Window animation scaling
- Transition animation scaling
- Animator duration scaling
If none of these fix the problem, sharing the exact error log would help narrow down the issue further!
内容的提问来源于stack exchange,提问作者mbob

