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

Android构建重复类错误求助(React-Native桥接Java库开发)

Fixing Duplicate Class Errors in React Native Android Bridge Development

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.CompareToBuilder comes from both commons-lang3-3.9.jar (likely pulled in by React Native/Unimodules) and creditCardNfcReader-1.0.3-runtime.jar (the NFC library you added)
  • bolts.BoltsExecutors$ImmediateExecutor conflicts between bolts-tasks-1.4.0.jar (React Native's dependency) and jetified-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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 08:02:55