Android构建重复类错误求助(React-Native桥接Java库开发)
Hey there, let's work through those duplicate class errors you're hitting—they're a super common headache when mixing Android dependencies in React Native projects, so you're not alone here.
First, Let's Break Down the Problem
The errors happen because multiple dependencies are including the same Java classes:
org.apache.commons.lang3.builder.CompareToBuildercomes from bothcommons-lang3-3.9.jar(likely pulled in by React Native/Unimodules) andcreditCardNfcReader-1.0.3-runtime.jar(the NFC library you added)bolts.BoltsExecutors$ImmediateExecutorconflicts betweenbolts-tasks-1.4.0.jar(React Native's dependency) andjetified-bolts-android-1.1.2.jar(probably from Unimodules or another dependency)
Your previous exclusion attempts missed the mark because you were targeting the wrong dependency sources. Let's fix that.
Step 1: Diagnose Exact Dependency Sources (Optional but Helpful)
First, run this command in your Android project directory to generate a full dependency tree—it shows exactly which libraries are pulling in the conflicting classes:
./gradlew :your-module-name:dependencies
Replace your-module-name with your library module name (e.g., :library) or :app if you're working directly in the app module. This will help you confirm where duplicates are coming from.
Step 2: Fix the Commons-Lang3 Duplicate
The NFC library creditCardNfcReader includes its own copy of commons-lang3, but React Native or Unimodules already provides this. We'll exclude the embedded version from the NFC library:
implementation ('com.github.pro100svitlo:creditCardNfcReader:1.0.3') { exclude group: 'org.apache.commons', module: 'commons-lang3' }
Step 3: Fix the Bolts Duplicate
For the bolts conflict, you have two reliable options:
Option A: Force a Unified Bolts Version
Tell Gradle to use a single version of all bolts dependencies (we'll use the newer 1.4.0 version that React Native uses):
// Add this block outside the dependencies closure in your build.gradle configurations.all { resolutionStrategy { force 'com.parse.bolts:bolts-tasks:1.4.0' force 'com.parse.bolts:bolts-android:1.4.0' } }
Option B: Exclude the Conflicting Bolts Dependency
If you prefer to exclude the older bolts-android entirely, add this configuration (this works if the older version isn't actually needed by your project):
// Add this block outside the dependencies closure configurations { all { exclude group: 'com.parse.bolts', module: 'bolts-android' } }
Step 4: Clean Up Your Previous Attempts
Delete this problematic block from your build.gradle—it's way too broad and will break your project by excluding all runtime dependencies:
configurations { runtime.exclude group: '*' }
Step 5: Verify the Fix
Run these commands to clean and rebuild your project:
./gradlew clean ./gradlew build
Bonus Tip: Lock React Native Version
Using com.facebook.react:react-native:+ lets Gradle pull the latest version automatically, which can introduce unexpected dependency changes. It's better to lock it to a specific stable version, like:
implementation "com.facebook.react:react-native:0.72.6"
内容的提问来源于stack exchange,提问作者James

