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

Espresso-Web在Release版本应用中测试失败问题求助

Fixing NoActivityResumedException with Espresso-Web + UIAutomator in Release Build

Here are targeted fixes for your issue—since Debug works but Release fails when launching via adb shell am start:

1. Enable WebView Debugging in Release

Espresso-Web relies on WebView debugging, which is enabled by default in Debug but disabled in Release. Add this code to your app's entry point (e.g., Application class or main Activity):

// Enable WebView debugging for test builds
if (BuildConfig.DEBUG || isTesting()) {
    WebView.setWebContentsDebuggingEnabled(true);
}

// Helper to check if running in instrumentation test mode
private boolean isTesting() {
    try {
        Class.forName("androidx.test.espresso.Espresso");
        return true;
    } catch (ClassNotFoundException e) {
        return false;
    }
}

This ensures WebView debugging is active only when tests are running, without affecting production Release builds.

2. Add R8/ProGuard Rules for Testing Libraries

Release builds often strip or obfuscate classes needed by Espresso. Add these rules to proguard-rules.pro:

# Keep Espresso and related classes
-keep class androidx.test.espresso.** { *; }
-keep class androidx.test.** { *; }
-keep class org.hamcrest.** { *; }

# Espresso-Web dependencies
-keep class androidx.test.espresso.web.** { *; }
-keep class com.google.gson.** { *; }
-keep class org.json.** { *; }

This prevents critical testing components from being removed during obfuscation.

3. Force Espresso to Recognize the Externally Launched Activity

Since you're starting the app via adb, Espresso might not detect the resumed activity automatically. Use UiAutomator to wait for the app to launch, then trigger Espresso's activity tracking:

UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
// Wait for your app's package to be active
device.wait(Until.hasObject(By.pkg("com.your.app.package").depth(0)), 10000);

// Fetch the resumed activity and force Espresso to attach to it
ActivityLifecycleMonitor lifecycleMonitor = ActivityLifecycleMonitorRegistry.getInstance();
Collection<Activity> resumedActivities = lifecycleMonitor.getActivitiesInStage(Stage.RESUMED);
if (!resumedActivities.isEmpty()) {
    Activity currentActivity = resumedActivities.iterator().next();
    // Perform a trivial action to trigger Espresso's context sync
    Espresso.onView(ViewMatchers.isRoot()).perform(ViewActions.click());
}

This makes Espresso aware of the activity started via adb.

4. Verify Test Runner Configuration

Ensure your Release build variant uses the correct test runner. In your module-level build.gradle:

android {
    buildTypes {
        release {
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            // Keep debuggable true only for test variants of Release
            debuggable true
        }
    }
}

Setting debuggable true for the test Release variant allows Espresso to attach properly.

Quick Checks

  • Confirm your Release test variant has the same permissions as Debug.
  • Ensure no release-specific flags (like android:exported) block the test runner from accessing the activity.

内容的提问来源于stack exchange,提问作者FiXiT

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 21:05:23