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

Android编译报错求助:Multiple dex重复定义及Dex合并失败问题

How to Fix "Multiple dex files define Landroid/arch/lifecycle/LiveData$LifecycleBoundObserver" Error

Hey there, let’s tackle this dex merge error you’re dealing with! The core issue here is simple: your project has duplicate dependencies that include the android.arch.lifecycle.LiveData$LifecycleBoundObserver class, which confuses the dex merging tool during compilation. Here are the most reliable fixes to resolve this:

1. Audit your build.gradle for redundant dependencies

Open your app-level build.gradle file and scan for conflicting lifecycle/LiveData entries. You might be mixing the old standalone Architecture Components dependencies with newer AndroidX versions, or pulling in the same library through multiple paths.

  • Action: Remove duplicate entries and stick to a consistent dependency set (either all AndroidX or all legacy Architecture Components—never mix them).
    Example of clean AndroidX dependencies:
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.6.2"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2"
    

2. Use Gradle’s dependency insight to track down the culprit

If you’re unsure which library is pulling in the duplicate LiveData class, run this command in your project root to get a detailed breakdown:

./gradlew app:dependencyInsight --dependency lifecycle-livedata --configuration debugCompileClasspath

This will show every path where the LiveData library is included. Look for unexpected versions or sources causing the conflict.

  • Action: Exclude the problematic duplicate from the conflicting library. For example, if a third-party SDK is bringing in an outdated LiveData version:
    implementation 'com.example:third-party-sdk:1.0.0' {
        exclude group: 'android.arch.lifecycle', module: 'livedata'
    }
    

3. Enable multidex (only as a last resort)

While this doesn’t fix the root duplicate issue, enabling multidex can work around dex merge failures for larger projects. Always prioritize fixing duplicate dependencies first, though.

  • Action: Add multidex support to your app:
    1. In your app-level build.gradle:
      android {
          defaultConfig {
              multiDexEnabled true
          }
      }
      implementation 'androidx.multidex:multidex:2.0.1'
      
    2. If your app uses a custom Application class, update it to extend MultiDexApplication, or add this override:
      @Override
      protected void attachBaseContext(Context base) {
          super.attachBaseContext(base);
          MultiDex.install(this);
      }
      

4. Clean and rebuild your project

After adjusting dependencies, clear cached build files to ensure changes take effect:

  • In Android Studio: Go to Build > Clean Project, then Build > Rebuild Project.
  • Via command line:
    ./gradlew clean build
    

Pro tip: Keep all your Architecture Components dependencies on the same version. Mixing versions is a common cause of these dex conflicts.

内容的提问来源于stack exchange,提问作者Mahmoud sehsah

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 08:05:16