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

Android Studio 3.1中单元测试配置失效问题求助

Fixing Gradle Build Error for "Core tests" After Android Studio 3.1 Upgrade

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.properties in your root project
  • Make sure the distributionUrl points 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 / Restart in 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.gradle file
  • 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, like testDebugUnitTest for 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 Build window, click the Toggle View button (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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:54:27