Android应用Multidex异常求助(含多版本报错栈与Gradle配置)
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.jarin yourlibsfolder and had a commented-out remote Volley dependency (even if the remote one is disabled, your:frameworkmodule 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.jarfile from yourlibsdirectory - Uncomment and update the remote Volley line in your
dependenciesblock (use a stable version compatible with your SDK 27 setup):implementation 'com.android.volley:volley:1.2.1' - Check your
:frameworkmodule’sbuild.gradlefile. 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
attachBaseContextto 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.xmlto useMultiDexApplication:<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.volleyor duplicate classes mentioned in errors, then exclude them using the sameexcludesyntax 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

