You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级AGP 3.0.1及Android Studio 3.0.1后Espresso测试无法运行求助

Fixing Espresso Test Failures After Gradle 3.0.1 & Android Studio 3.0.1 Upgrade

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.2 for runner/rules and 3.0.2 for Espresso core/contrib works well). Update your app-level build.gradle like 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 version
    
  • Disable Instant Run
    Early Android Studio 3.0 builds had rough compatibility between Instant Run and Espresso. Turn it off by going to File → Settings → Build, Execution, Deployment → Instant Run and 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 @RunWith or @Rule can 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 your proguard-rules.pro:

    -keep class android.support.test.** { *; }
    -keep class android.support.test.espresso.** { *; }
    -keep class org.junit.** { *; }
    

    (Swap android.support with androidx if you’ve migrated to AndroidX.)

  • Clean and rebuild your project
    Old build cache often causes unexplained issues. Run Build → Clean Project, followed by Build → Rebuild Project, then try running your tests again.

  • Turn off device animations
    Espresso struggles with system animations. On your test device/emulator, go to Settings → Developer Options and 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:12:22