Gradle Android集成测试遇重复类错误,求解决方案
Hey there, let's work through this duplicate class issue you're facing. The problem with your initial exclude attempt is that you're targeting the package name (org.xmlpull.v1) instead of the actual Maven group/module IDs of the conflicting libraries. Let's break down the solution:
Why Your First Attempt Failed
The two conflicting libraries have these distinct identifiers:
kxml2-2.3.0.jar→ Group:net.sf.kxml, Module:kxml2xpp3-1.1.3.3.jar→ Group:xpp3, Module:xpp3
org.xmlpull.v1 is just the shared package name between the two libraries, not the group/module you need to target for exclusion. That's why your initial rule didn't have any effect.
Step-by-Step Solutions
1. Exclude the Conflicting Library Directly from Module B's Dependency
Pick which library you want to keep (most Android projects work smoothly with kxml2, but you can choose based on your project's needs) and exclude the other in your module A's build.gradle:
Option 1: Exclude xpp3
implementation (project(':B')) { exclude group: 'xpp3', module: 'xpp3' }
Option 2: Exclude kxml2
implementation (project(':B')) { exclude group: 'net.sf.kxml', module: 'kxml2' }
2. Verify the Full Dependency Tree (If the First Step Fails)
Sometimes other dependencies (not just module B) might be pulling in these conflicting libraries. Run this command in your project root to generate a complete dependency tree for module A:
./gradlew :A:dependencies
Scan the output for entries of net.sf.kxml:kxml2 or xpp3:xpp3 across all configurations, then add exclude rules for those additional sources as needed.
3. Force Gradle to Use a Single Version (Fallback)
If excluding doesn't cover all edge cases, you can force Gradle to use one specific version of the library across your entire project. Add this to your module A's build.gradle (or the project-level build.gradle to apply it globally):
configurations.all { resolutionStrategy { // Force use of kxml2 (adjust version if needed) force 'net.sf.kxml:kxml2:2.3.0' // OR force xpp3 instead: // force 'xpp3:xpp3:1.1.3.3' } }
After making these changes, clean and rebuild your project, then run the integration tests again—this should resolve the duplicate class errors.
内容的提问来源于stack exchange,提问作者Comfy Cat

