Android Studio 3.1中单元测试配置失效问题求助
Hey there, let's work through this Gradle build issue you're hitting after upgrading to Android Studio 3.1. Based on the details you shared, here are targeted steps to resolve the problem:
1. Verify Gradle Version Compatibility
First off, the Android Gradle Plugin (AGP) and Gradle itself have strict version matching rules. For AGP 3.1.0/3.1.1, you need Gradle 4.4 or higher.
Check your project's Gradle wrapper configuration:
- Open
gradle/wrapper/gradle-wrapper.propertiesin your root project - Make sure the
distributionUrlpoints to a compatible Gradle version, e.g.:distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
If you were using an older Gradle version (like 3.x for AGP 2.3.0), this mismatch is likely causing the build failure.
2. Clear Caches and Refresh Dependencies
Cached data from the old setup can often break new builds. Let's wipe those out:
- Go to
File > Invalidate Caches / Restartin Android Studio, confirm the action - Once Studio restarts, open the Terminal tab and run:
# For macOS/Linux ./gradlew clean build --refresh-dependencies # For Windows gradlew clean build --refresh-dependencies
This command cleans build artifacts and forces Gradle to re-download all dependencies from scratch.
3. Update Module Build Configurations
AGP 3.x introduced new dependency configurations and requires consistent SDK settings:
- Open your module's
build.gradlefile - Ensure these settings are correctly set:
android { compileSdkVersion 27 buildToolsVersion "27.0.3" defaultConfig { targetSdkVersion 27 // ... other configs } // Enable Java 8 compatibility (required for AGP 3.x + JDK 8) compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { // Replace old testCompile/androidTestCompile with new configurations testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' // ... other test dependencies }
Note: AGP 3.0+ deprecated testCompile and androidTestCompile in favor of testImplementation and androidTestImplementation.
4. Check "Core tests" Run Configuration
Your custom build configuration might have outdated settings:
- Go to
Run > Edit Configurations... - Select your "Core tests" configuration
- Verify:
- The Module dropdown is set to the correct module you want to test
- The Test kind is set appropriately (e.g., "All in module")
- The Gradle task field points to the correct test task (usually
test<Variant>UnitTest, liketestDebugUnitTestfor debug builds)
- Save the configuration and try running the tests again.
5. Get the Full Error Log
The truncated error message you shared doesn't show the root cause. To get more details:
- In the Android Studio
Buildwindow, click theToggle Viewbutton (looks like a stack trace icon) to expand the full log - Or run your test task directly in the terminal with verbose output:
./gradlew yourCoreTestTaskName --info
Replace yourCoreTestTaskName with the actual task name from your configuration. The full log will reveal specific issues like dependency conflicts, missing tasks, or configuration errors.
内容的提问来源于stack exchange,提问作者Leejjon

