React Native构建时com.yqritc.scalablevideoview类重复问题求助
问题背景
使用React Native 0.70.6开发,Android Studio构建时抛出重复类错误:
Duplicate class com.yqritc.scalablevideoview.BuildConfig found in modules jetified-Android-ScalableVideoView-10e7b4da8b-runtime (com.github.adityaxjha:Android-ScalableVideoView:10e7b4da8b) and jetified-Android-ScalableVideoView-v1.0.4-jitpack-runtime (com.github.MatrixFrog:Android-ScalableVideoView:v1.0.4-jitpack)
已尝试的无效操作:
- 将
react-video从5.2.1降级到5.1.1,触发com.yqritc.scalablevideoview not found错误 - 参考旧指南配置已废弃的
jcenter(),改用mavenCentral()无效果 - 执行缓存清理、项目重建,问题仍存在
项目android/build.gradle部分配置:
buildscript { ext { buildToolsVersion = "31.0.0" minSdkVersion = 24 compileSdkVersion = 33 targetSdkVersion = 33 ext.kotlin_version = '1.6.0' kotlinVersion = '1.6.10' googlePlayServicesAuthVersion = "19.2.0" if (System.properties['os.arch'] == "aarch64") { // For M1 Users we need to use the NDK 24 which added support for aarch64 ndkVersion = "24.0.8215888" } else { // Otherwise we default to the side-by-side NDK version from AGP. ndkVersion = "21.4.7075529" } } }
解决方案
1. 定位依赖冲突路径
执行依赖树分析命令,找出两个冲突依赖的引入来源:
./gradlew app:dependencies > dependencies.txt
打开生成的dependencies.txt,搜索com.github.adityaxjha:Android-ScalableVideoView和com.github.MatrixFrog:Android-ScalableVideoView,确定是哪个第三方库(如react-video)引入了冲突版本。
2. 强制统一官方依赖版本
两个冲突版本均为官方库yqritc/Android-ScalableVideoView的fork,直接强制使用官方版本可避免冲突。在android/app/build.gradle的dependencies块中添加:
dependencies { // 其他依赖... implementation('com.github.yqritc:Android-ScalableVideoView:1.0.4') { force = true } }
3. 排除冲突的冗余依赖
若不想强制版本,可在引入冲突库的第三方依赖中排除其中一个版本。例如假设react-video引入了冲突库,修改android/app/build.gradle:
dependencies { // 其他依赖... implementation('com.brentvatne:react-native-video:5.2.1') { exclude group: 'com.github.MatrixFrog', module: 'Android-ScalableVideoView' // 或排除另一个冲突版本:exclude group: 'com.github.adityaxjha', module: 'Android-ScalableVideoView' } }
4. 配置正确的仓库
在android/build.gradle的allprojects/repositories中添加JitPack仓库(部分fork版本托管于此),保留mavenCentral():
allprojects { repositories { mavenCentral() maven { url 'https://jitpack.io' } google() // 其他仓库... } }
注意:不要添加已废弃的
jcenter(),避免构建不稳定。
5. 彻底清理并重建
执行以下命令清理缓存后重新构建:
./gradlew clean rm -rf ~/.gradle/caches rm -rf android/app/build npx react-native run-android
内容的提问来源于stack exchange,提问作者giuseppe carini

