Android Studio开发Firebase壁纸应用遇Dex合并失败求助
Hey there, let's work through this dex merge error you're hitting. The Multiple dex files define LiveData$1 message tells us there's a duplicate dependency conflict—even with multidex enabled, duplicate classes will still break your build. Here's how to fix it step by step:
1. Identify the conflicting dependencies
First, we need to find out which libraries are pulling in duplicate copies of the LiveData class. Run this command in your project's root directory:
- On macOS/Linux:
./gradlew app:dependencies - On Windows:
gradlew.bat app:dependencies
Search the output for android.arch.lifecycle:livedata (or androidx.lifecycle:livedata if you're using AndroidX). You'll likely see that Firebase (or one of its associated libraries, like Firebase UI) is introducing a version of LiveData that conflicts with the one you're explicitly adding, or vice versa.
2. Exclude duplicate dependencies
Once you spot the conflicting library, add an exclude rule to your build.gradle (Module level) to remove the duplicate LiveData dependency. For example, if Firebase Auth is pulling in the conflicting LiveData:
implementation 'com.google.firebase:firebase-auth:20.0.4' { exclude group: 'android.arch.lifecycle', module: 'livedata' }
Repeat this for any other libraries that show up with duplicate LiveData entries.
3. Align to AndroidX (if you haven't already)
If you're still using the old Support Library, mixing it with Firebase's modern AndroidX dependencies is a common source of conflicts. Here's how to switch:
- Open your
gradle.propertiesfile and add these lines:
Jetifier automatically converts old Support Library dependencies to AndroidX equivalents.android.useAndroidX=true android.enableJetifier=true - Replace any Support Library lifecycle dependencies with AndroidX versions. For example:
// Replace this (Support Library) // implementation 'com.android.support:multidex:1.0.3' // implementation 'android.arch.lifecycle:livedata:1.1.1' // With this (AndroidX) implementation 'androidx.multidex:multidex:2.0.1' implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
4. Clean deeper than just "Clean Project"
Sometimes the standard Clean/Rebuild isn't enough. Try these steps:
- Delete the
.gradlefolder in your project's root directory - Delete the
buildfolders in both your project root and theappmodule - Re-sync your Gradle files (click the "Sync Project with Gradle Files" button in Android Studio)
- Rebuild your project
5. Check for manual library duplicates
Double-check your app/libs folder to make sure you haven't added a .jar or .aar file for LiveData (or any lifecycle-related library) that's also being pulled in via Gradle dependencies. If you find one, remove it.
After following these steps, your build should resolve the duplicate LiveData class issue and merge dex successfully.
内容的提问来源于stack exchange,提问作者Saif

