导入项目库后遇Android类重复定义及classes.dex错误,求解决方案
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:dependenciesSearch the output for
com.google.code.gsonto 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 anexcluderule in your app-levelbuild.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 theandroidblock in your app-levelbuild.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 toFile > Invalidate Caches... > Invalidate and Restartin Android Studio, then runBuild > Rebuild Projectto 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 theapp/builddirectory manually (or useBuild > Clean Project), then runBuild > Rebuild Project. This clears corrupted intermediate build files that might be causing the path error.Disable incremental compilation
Add this line to yourgradle.propertiesfile to turn off incremental builds, which can sometimes lead to dex file corruption:android.enableIncremental=falseCheck for manually added JARs
If you've manually added agson.jarfile to your project'slibsfolder, 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

