Android Release构建报错:重复条目javax/xml/XMLConstants.class
Hey there! Let's work through this duplicate class error that's blocking your release build. The javax/xml/XMLConstants.class duplicate entry typically pops up when multiple dependencies in your project pull in the same XML-related library (like xml-apis) behind the scenes. Looking at your build.gradle, here's how to fix it:
Step 1: Pinpoint the Conflicting Dependencies
First, run this Gradle command to generate your project's full dependency tree. It will show you exactly which libraries are bringing in the duplicate xml-apis module:
./gradlew app:dependencies
Search the output for xml-apis — you’ll likely find it listed under both net.sourceforge.htmlunit:htmlunit:2.23 and com.squareup.retrofit2:converter-simplexml:2.3.0 (two dependencies in your setup that commonly include this module).
Step 2: Exclude the Duplicate Module
You can exclude the conflicting xml-apis module either globally or for specific dependencies. Here are both approaches:
Option A: Global Exclusion (Simpler)
Add this line to your existing configurations.all block to exclude the duplicate module across your entire project:
configurations { all { exclude module: 'httpclient' exclude module: 'xpp3' exclude group: 'xml-apis', module: 'xml-apis' // Add this line } }
Option B: Per-Dependency Exclusion (More Targeted)
If global exclusion causes unexpected issues, you can exclude the module from specific dependencies instead:
- Update your HtmlUnit dependency:
implementation ('net.sourceforge.htmlunit:htmlunit:2.23') { exclude module: 'commons-logging' exclude module: 'httpclient' exclude module: 'xml-apis' // Add this line } - And your Retrofit SimpleXML converter:
implementation ('com.squareup.retrofit2:converter-simplexml:2.3.0') { exclude module: 'xml-apis' // Add this line }
Step 3: Unify Support Library Versions
Notice that your support library versions are mixed (some are 27.0.0, others 27.0.2). This can lead to subtle hidden conflicts. Update all your support library dependencies to use the same version (27.0.2 is the latest in your setup):
implementation 'com.android.support:cardview-v7:27.0.2' implementation 'com.android.support:appcompat-v7:27.0.2' implementation 'com.android.support:support-v4:27.0.2' implementation 'com.android.support:recyclerview-v7:27.0.2' implementation 'com.android.support:design:27.0.2' implementation 'com.android.support:customtabs:27.0.2'
Step 4: Clean and Rebuild
After making these changes, clean your project and rebuild the release variant to apply the fixes:
./gradlew clean ./gradlew assembleRelease
Bonus Check
If the error still persists, double-check the libs directory in your app module. If any jar files there include javax/xml/XMLConstants.class, remove the duplicate jar to resolve the conflict.
内容的提问来源于stack exchange,提问作者Manjushree Shinde

