安装react-native-camera后项目无法构建,求助解决依赖版本冲突
我完全懂你折腾一整天的崩溃感——第三方依赖版本冲突确实是React Native Android构建里的大坑,尤其是Google Play Services和Firebase这类全家桶库,版本必须严格对齐才行。
问题根源
你遇到的报错:
Android dependency 'com.google.android.gms:play-services-base' has different version for the compile (11.8.0) and runtime (12.0.1) classpath. You should manually set the same version via DependencyResolution
本质是react-native-camera内部依赖了12.0.1版本的play-services-base,但你手动在build.gradle里指定了10.0.1的版本,导致编译期和运行时拉取的库版本不一致,触发了冲突。而且Google有硬性要求:所有Play Services和Firebase的组件必须使用完全相同的版本,混用不同版本必然会出问题。
分步解决方案
1. 统一所有Play Services和Firebase依赖版本
直接修改你app模块下的build.gradle里的dependencies,把所有com.google.android.gms和com.google.firebase开头的库版本统一成同一个(这里选报错里的12.0.1,也可以根据react-native-camera的文档选它兼容的稳定版本):
dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation project(':tipsi-stripe') implementation "com.facebook.react:react-native:+" implementation project(':react-native-maps') implementation project(':react-native-camera') // 统一Firebase版本到12.0.1 implementation 'com.google.firebase:firebase-core:12.0.1' implementation "com.android.support:appcompat-v7:23.0.1" // 统一Play Services相关库版本到12.0.1 implementation 'com.google.android.gms:play-services-base:12.0.1' implementation 'com.google.android.gms:play-services-maps:12.0.1' }
2. 添加全局依赖强制规则(可选但推荐)
为了防止其他第三方库(比如tipsi-stripe、react-native-maps)偷偷引入不同版本的依赖,在项目根目录的build.gradle里添加全局解析规则,强制所有相关库使用指定版本:
allprojects { repositories { // 保持你原有的仓库配置,比如Google、jcenter等 } configurations.all { resolutionStrategy { // 强制所有Play Services组件使用12.0.1 force 'com.google.android.gms:play-services-base:12.0.1' force 'com.google.android.gms:play-services-maps:12.0.1' // 强制Firebase组件使用12.0.1 force 'com.google.firebase:firebase-core:12.0.1' } } }
3. 清理缓存并重新构建
做完上面的修改后,一定要清理Gradle缓存再构建,避免旧缓存干扰:
# 进入android目录 cd android # 清理缓存 ./gradlew clean # 返回项目根目录并运行 cd .. react-native run-android
额外提示
如果还是有问题,建议去react-native-camera的官方文档查看它要求的Play Services最低版本,确保你统一的版本和它兼容。另外,com.android.support:appcompat-v7的版本23.0.1比较老,也可以考虑升级到和你的compileSdkVersion匹配的版本(比如如果compileSdk是28,就用28.0.3),避免其他潜在的兼容问题。
内容的提问来源于stack exchange,提问作者eQuinox

