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
- Incorrect test directory structure: Instrumentation/Espresso tests must live in
src/androidTest/java/[your-package](notsrc/test/java/, which is for local unit tests). Android Studio might auto-detect misplaced tests, but Gradle enforces this strictly. - Missing or misconfigured Gradle dependencies: You might be missing core testing libraries or haven’t specified your custom test runner.
- Test class/annotation mismatches: Tests might not follow the naming convention (e.g., no
Testsuffix) or use the wrong@Testimport (ensure it’sorg.junit.Test, not a different package).
Fixes
- Verify test placement: Double-check that all your Instrumentation/Espresso tests are in
src/androidTest/java/under your app’s package. - 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' } - 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@Testannotation.
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
- Missing instrumentation registration: Gradle should auto-register your runner if
testInstrumentationRunneris set, but manual registration might be missing if you’ve customized the AndroidManifest. - Incorrect command syntax: You’re using the wrong package name or runner class path in the
am instrumentcommand. - Uninstalled test APK: The test APK (not just the app APK) isn’t installed on the device.
Fixes
- Ensure Gradle registers your runner: Confirm
testInstrumentationRunner "my.package.CustomTestRunner"is set in yourdefaultConfig(this eliminates the need for manual AndroidManifest edits). - Fix the
adbcommand: Use the correct format, replacingmy.packagewith your actual app package:
Note: If you’re using Orchestrator, use the Orchestrator runner instead:# Correct command for your custom runner adb shell am instrument -w my.package/my.package.CustomTestRunneradb shell am instrument -w my.package.test/androidx.test.orchestrator.AndroidTestOrchestrator - 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
- Truncated CustomTestRunner code: Looking at your provided code, the
setSystemAnimationsScalemethod 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. - Orchestrator configuration gaps: You might be missing the orchestrator dependency or haven’t enabled it in
testOptions.
Fixes
- Fix your CustomTestRunner code: Complete the truncated
setSystemAnimationsScalemethod 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 } } - Validate Orchestrator setup: Double-check the
testOptionsandandroidTestUtildependency in yourbuild.gradle(as shown in Issue 1’s fix). - Run with verbose logging: Use
./gradlew connectedAndroidTest --infoto get detailed logs—this will show exactly why tests aren’t being picked up by Orchestrator.
内容的提问来源于stack exchange,提问作者Lars
相关产品推荐
相关产品推荐

