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

React Native Android编译报错:Multiple dex files定义冲突求助

Fixing Multiple Dex Files Conflict with Google Play Services in React Native 0.40

Hey there, I’ve run into this exact kind of dependency mess with React Native and Google Play Services before—let’s work through how to fix it. The core issue here is that even though you excluded conflicting modules from react-native-maps, other third-party libraries in your project are pulling in different versions of Google Play Services (GMS), which triggers that duplicate class dex error.

Here’s a step-by-step breakdown of the solution:

1. Pinpoint All Conflicting GMS Versions

First, let’s get a clear view of which dependencies are bringing in mismatched GMS versions. Run this command in your project’s android directory:

./gradlew app:dependencies

Scroll through the output and look for every entry under com.google.android.gms. You’ll probably see libraries like react-native-fcm, react-native-firebase-analytics, or react-native-branch requesting a different version than your explicitly declared 10.2.1.

2. Force a Unified GMS Version Across All Dependencies

Google requires all GMS libraries to use the exact same version to avoid conflicts. Add this configuration to your android/app/build.gradle file inside the android block to override any version requests from third-party libraries:

android {
    // ... your existing android configuration

    configurations.all {
        resolutionStrategy {
            // Force all GMS libraries to align with your specified version
            force 'com.google.android.gms:play-services-base:10.2.1'
            force 'com.google.android.gms:play-services-maps:10.2.1'
            force 'com.google.android.gms:play-services-gcm:10.2.1'
            force 'com.google.android.gms:play-services-analytics:10.2.1'
            force 'com.google.android.gms:play-services-location:10.2.1'
        }
    }
}

This ensures every dependency uses the GMS version you’ve defined, eliminating version mismatches.

3. Clean Build Artifacts and Rebuild

Old cached files can still cause issues, so let’s wipe them clean before rebuilding:

  • Run this in the android directory:
    ./gradlew clean
    
  • Delete the android/app/build folder entirely
  • Finally, rebuild your app:
    react-native run-android
    

4. Check Third-Party Library Compatibility

Since you’re using React Native 0.40 (a fairly old version), make sure all your third-party libraries are compatible with this RN release. For example, newer versions of react-native-fcm or react-native-firebase-analytics might require a newer GMS version that clashes with your setup. Check each library’s README to confirm they support RN 0.40, and downgrade them if needed.

5. Confirm Google Services Plugin Placement

You already have apply plugin: 'com.google.gms.google-services' at the end of your build.gradle file—perfect! This plugin adjusts dependencies based on your google-services.json file, and it must run after all dependencies are declared to work correctly.

If you follow these steps, the duplicate dex error should resolve itself. The key is ensuring every dependency in your project uses the same GMS version, which the forced resolution strategy handles effectively.

内容的提问来源于stack exchange,提问作者Proxy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:20:54