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

Android应用Multidex异常求助(含多版本报错栈与Gradle配置)

Fixing Multidex & Duplicate Volley Class Errors in Android Studio

Root Cause

All your errors boil down to duplicate Volley library classes cluttering your project. The "Multiple dex files define..." and "Unable to merge dex" issues happen when the same class gets included multiple times—here, Volley is clearly being added more than once, and that's breaking the dex merging process even with Multidex enabled.

Looking at your setup, the main culprits are:

  • You’re using a local volley.jar in your libs folder and had a commented-out remote Volley dependency (even if the remote one is disabled, your :framework module might also be pulling in Volley)
  • Duplicate classes are overriding Multidex’s functionality, leading to merge failures across Android versions

Step-by-Step Fixes

1. Remove Duplicate Volley Dependencies

Pick one consistent way to include Volley—remote Maven dependency is preferred (it’s easier to update and avoids jar file bloat):

  • Delete the volley.jar file from your libs directory
  • Uncomment and update the remote Volley line in your dependencies block (use a stable version compatible with your SDK 27 setup):
    implementation 'com.android.volley:volley:1.2.1'
    
  • Check your :framework module’s build.gradle file. If it also includes Volley (via jar or remote), add an exclusion in your main app’s dependency to avoid duplicates:
    implementation project(':framework') {
        exclude group: 'com.android.volley', module: 'volley'
    }
    

2. Verify Multidex Configuration

You already have multiDexEnabled true in defaultConfig, but make sure your app’s Application class is set up correctly:

  • If using a custom Application class, override attachBaseContext to enable Multidex:
    import android.support.multidex.MultiDex;
    import android.app.Application;
    import android.content.Context;
    
    public class MyApplication extends Application {
        @Override
        protected void attachBaseContext(Context base) {
            super.attachBaseContext(base);
            MultiDex.install(this);
        }
    }
    
  • If you don’t have a custom Application, update your AndroidManifest.xml to use MultiDexApplication:
    <application
        android:name="android.support.multidex.MultiDexApplication"
        ...>
        ...
    </application>
    

3. Clean & Rebuild Your Project

Cached build files often hold onto old dependencies, so do a full clean to wipe them out:

  • In Android Studio, go to Build > Clean Project
  • Then run Build > Rebuild Project
  • Alternatively, use the command line:
    # For macOS/Linux
    ./gradlew clean assembleDebug
    
    # For Windows
    gradlew.bat clean assembleDebug
    

4. Hunt for Hidden Duplicates

If errors persist, use Gradle’s dependency tree to spot other duplicate classes:

  • Run this command in your project root:
    ./gradlew app:dependencies
    
  • Look for any other instances of com.android.volley or duplicate classes mentioned in errors, then exclude them using the same exclude syntax from step 1.

Example Cleaned-Up Dependencies Block

Here’s what your streamlined dependencies should look like:

dependencies {
    implementation "com.android.support:appcompat-v7:$supportLibraryVersion"
    implementation 'com.android.support:multidex:1.0.3' // Updated to latest compatible version
    implementation 'com.android.volley:volley:1.2.1' // Remote dependency replaces local jar
    implementation "com.google.code.gson:gson:$gsonVersion"
    implementation 'com.google.android.gms:play-services-analytics:10.2.4'
    implementation "com.android.support.constraint:constraint-layout:$constraintLayoutVersion"
    implementation project(':framework') {
        exclude group: 'com.android.volley', module: 'volley' // Exclude Volley from framework if needed
    }
    // Keep Crashlytics jars (ensure these aren't duplicated elsewhere!)
    implementation files('libs/io-fabric-sdk-android_fabric-classes.jar')
    implementation files('libs/com-crashlytics-sdk-android_answers-classes.jar')
    implementation files('libs/com-crashlytics-sdk-android_beta-classes.jar')
    implementation files('libs/com-crashlytics-sdk-android_crashlytics-classes.jar')
    
    testImplementation "junit:junit:$junitVersion"
    androidTestImplementation "com.android.support.test:runner:$runnerVersion"
    androidTestImplementation "com.android.support.test.espresso:espresso-core:$espressoVersion"
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:27:04