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

导入项目库后遇Android类重复定义及classes.dex错误,求解决方案

Fixing Duplicate Gson Class & Dex Path Errors in Android Studio

Hey Jordy, let's work through these two issues step by step—they're super common when dealing with dependency conflicts and build artifacts, so we'll get you sorted out quickly.

1. Resolving "Type com.google.gson.ExclusionStrategy is defined multiple times"

This error pops up when your project pulls in multiple versions or copies of the Gson library from different dependencies. Here's how to fix it:

  • Identify duplicate Gson dependencies
    Run this command in your project root to generate a dependency tree and spot where Gson is being repeated:

    # Windows
    gradlew.bat app:dependencies
    
    # macOS/Linux
    ./gradlew app:dependencies
    

    Search the output for com.google.code.gson to see which modules are bringing in Gson as a transitive dependency.

  • Exclude redundant Gson imports
    For each dependency that's pulling in an extra Gson copy, add an exclude rule in your app-level build.gradle (Module: app) file. For example:

    implementation('com.some.thirdparty:library:2.0.0') {
        exclude group: 'com.google.code.gson', module: 'gson'
    }
    
  • Force a single Gson version
    To ensure all dependencies use the same stable Gson version, add this to the android block in your app-level build.gradle:

    configurations.all {
        resolutionStrategy.force 'com.google.code.gson:gson:2.10.1' // Use an up-to-date, stable version
    }
    
  • Clear cache and rebuild
    Go to File > Invalidate Caches... > Invalidate and Restart in Android Studio, then run Build > Rebuild Project to apply all changes.

2. Fixing the Invalid Dex Path Error

The error pointing to external_libs_dex/debug/out/classes.dex is usually a side effect of the duplicate class issue, but if it persists after fixing Gson, try these steps:

  • Clean and rebuild your project
    Delete the app/build directory manually (or use Build > Clean Project), then run Build > Rebuild Project. This clears corrupted intermediate build files that might be causing the path error.

  • Disable incremental compilation
    Add this line to your gradle.properties file to turn off incremental builds, which can sometimes lead to dex file corruption:

    android.enableIncremental=false
    
  • Check for manually added JARs
    If you've manually added a gson.jar file to your project's libs folder, remove it—this will conflict with the Gradle-managed Gson dependency and cause duplicate class issues.

If you're still stuck, double-check that your Android Gradle Plugin and Gradle versions are compatible (stick to stable, matching versions recommended by Android Studio).

内容的提问来源于stack exchange,提问作者Jordy Voorham

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 14:58:10