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

Android Studio测试可运行,命令行执行失败求助

Let’s tackle each of your command-line test issues one by one—since they work in Android Studio, the problem almost always boils down to configuration mismatches or missing setup that the IDE handles automatically.

Issue 1: connectedAndroidTest reports "No tests found"

Even though you’ve added @Test annotations, Gradle isn’t picking up your tests via the command line. Here’s what to check:

Possible Causes

  1. Incorrect test directory structure: Instrumentation/Espresso tests must live in src/androidTest/java/[your-package] (not src/test/java/, which is for local unit tests). Android Studio might auto-detect misplaced tests, but Gradle enforces this strictly.
  2. Missing or misconfigured Gradle dependencies: You might be missing core testing libraries or haven’t specified your custom test runner.
  3. Test class/annotation mismatches: Tests might not follow the naming convention (e.g., no Test suffix) or use the wrong @Test import (ensure it’s org.junit.Test, not a different package).

Fixes

  1. Verify test placement: Double-check that all your Instrumentation/Espresso tests are in src/androidTest/java/ under your app’s package.
  2. Update your app-level build.gradle: Ensure it includes these critical configurations:
    android {
        defaultConfig {
            // Point to your custom test runner
            testInstrumentationRunner "my.package.CustomTestRunner"
            // Enable clear app state for Orchestrator
            testInstrumentationRunnerArguments clearPackageData: 'true'
        }
    
        testOptions {
            // Enable Android Test Orchestrator
            execution 'ANDROIDX_TEST_ORCHESTRATOR'
        }
    }
    
    dependencies {
        // Core testing dependencies
        androidTestImplementation 'androidx.test.ext:junit:1.1.5'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
        androidTestImplementation 'androidx.test:runner:1.5.2'
        androidTestImplementation 'androidx.test:rules:1.5.0'
        // Orchestrator dependency (required for isolated test runs)
        androidTestUtil 'androidx.test:orchestrator:1.4.2'
    }
    
  3. Validate test classes: Ensure your test classes are public, use @RunWith(AndroidJUnit4.class) (optional if using your custom runner, but adds safety), and every test method has the correct @Test annotation.

Issue 2: adb shell am instrument fails with "Unable to find instrumentation info"

Your custom runner isn’t being recognized by adb, which usually stems from misconfiguration or incorrect command syntax.

Possible Causes

  1. Missing instrumentation registration: Gradle should auto-register your runner if testInstrumentationRunner is set, but manual registration might be missing if you’ve customized the AndroidManifest.
  2. Incorrect command syntax: You’re using the wrong package name or runner class path in the am instrument command.
  3. Uninstalled test APK: The test APK (not just the app APK) isn’t installed on the device.

Fixes

  1. Ensure Gradle registers your runner: Confirm testInstrumentationRunner "my.package.CustomTestRunner" is set in your defaultConfig (this eliminates the need for manual AndroidManifest edits).
  2. Fix the adb command: Use the correct format, replacing my.package with your actual app package:
    # Correct command for your custom runner
    adb shell am instrument -w my.package/my.package.CustomTestRunner
    
    Note: If you’re using Orchestrator, use the Orchestrator runner instead:
    adb shell am instrument -w my.package.test/androidx.test.orchestrator.AndroidTestOrchestrator
    
  3. Reinstall both APKs: Ensure both the app and test APK are installed:
    adb install -r app-debug.apk
    adb install -r app-debug-androidTest.apk
    

Issue 3: Orchestrator runs show "OK (0 tests)"

This is likely due to a broken custom runner or incomplete Orchestrator setup.

Possible Causes

  1. Truncated CustomTestRunner code: Looking at your provided code, the setSystemAnimationsScale method is incomplete (the for loop cuts off mid-line). This would cause a compilation error, resulting in a broken test APK with no executable tests.
  2. Orchestrator configuration gaps: You might be missing the orchestrator dependency or haven’t enabled it in testOptions.

Fixes

  1. Fix your CustomTestRunner code: Complete the truncated setSystemAnimationsScale method to ensure it compiles. Here’s the missing part:
    private void setSystemAnimationsScale(float animationScale) {
        try {
            Class<?> windowManagerStubClazz = Class.forName("android.view.IWindowManager$Stub");
            Method asInterface = windowManagerStubClazz.getDeclaredMethod("asInterface", IBinder.class);
            Class<?> serviceManagerClazz = Class.forName("android.os.ServiceManager");
            Method getService = serviceManagerClazz.getDeclaredMethod("getService", String.class);
            Class<?> windowManagerClazz = Class.forName("android.view.IWindowManager");
            Method setAnimationScales = windowManagerClazz.getDeclaredMethod("setAnimationScales", float[].class);
            Method getAnimationScales = windowManagerClazz.getDeclaredMethod("getAnimationScales");
            IBinder windowManagerBinder = (IBinder) getService.invoke(null, "window");
            Object windowManagerObj = asInterface.invoke(null, windowManagerBinder);
            float[] currentScales = (float[]) getAnimationScales.invoke(windowManagerObj);
            for (int i = 0; i < currentScales.length; i++) {
                currentScales[i] = animationScale; // Missing line
            }
            setAnimationScales.invoke(windowManagerObj, (Object) currentScales); // Missing line
        } catch (Exception e) {
            Log.e(TAG, "Failed to adjust animation scales", e); // Add error logging
        }
    }
    
  2. Validate Orchestrator setup: Double-check the testOptions and androidTestUtil dependency in your build.gradle (as shown in Issue 1’s fix).
  3. Run with verbose logging: Use ./gradlew connectedAndroidTest --info to get detailed logs—this will show exactly why tests aren’t being picked up by Orchestrator.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 09:09:04