React Native v6.2.2集成react-native-nested-scroll-view安卓编译报错
问题:React Native 6.2.2 编译失败,找不到 com.android.tools.build:gradle:2.3.3
基于 React Native v6.2.2 开发应用,安装了 react-native-nested-scroll-view@^9.0.0 后,执行 npm run android 时编译失败,错误提示:
配置':react-native-nested-scroll-view'项目时出错,无法解析该模块classpath的所有构件,找不到com.android.tools.build:gradle:2.3.3
已尝试以下操作但未解决问题:
- 手动添加该依赖项
- 配置 google、mavenCentral 等仓库
- 删除 node_modules 并重新执行
npm install
相关配置信息
gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists
项目根目录 build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. def REACT_NATIVE_VERSION = new File(['node', '--print',"JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim()) buildscript { ext { buildToolsVersion = "33.0.3" minSdkVersion = 21 compileSdkVersion = 31 targetSdkVersion = 31 playServicesVersion = "17.0.0" androidMapsUtilsVersion = "2.2.0" supportLibVersion = "28.0.0" agp_version = '4.2.0' kotlin_version = "1.5.31" } repositories { google() jcenter() mavenCentral() } dependencies { classpath("com.android.tools.build:gradle:$agp_version") //classpath("com.android.tools.build:gradle:2.3.3") classpath('com.google.gms:google-services:4.3.5') classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { configurations.all { resolutionStrategy { // Remove this override in 0.65+, as a proper fix is included in react-native itself. force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION } } repositories { google() mavenCentral() maven { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm url("$rootDir/../node_modules/react-native/android") } maven { // Android JSC is installed from npm url("$rootDir/../node_modules/jsc-android/dist") } jcenter() maven { url 'https://www.jitpack.io' } } }
package.json 依赖片段
"dependencies": { "react-native-nested-scroll-view": "^9.0.0" }
解决方案
问题根源是 react-native-nested-scroll-view@9.0.0 内部硬依赖了老旧的 Gradle 插件版本 2.3.3,该版本已从主流仓库下线,可通过以下方法解决:
方法1:强制统一 Gradle 插件版本
在项目根目录的 build.gradle 中,给 buildscript 添加版本强制规则,让所有模块使用你项目指定的 AGP 版本(4.2.0):
buildscript { ext { // 保留现有配置... agp_version = '4.2.0' } repositories { // 保留现有仓库配置... } dependencies { // 保留现有依赖... } // 添加这段版本强制规则 configurations.all { resolutionStrategy { force "com.android.tools.build:gradle:$agp_version" } } }
方法2:降级 react-native-nested-scroll-view 版本
若强制版本后出现兼容问题,可降级到适配 RN 6.2.2 的稳定版本,比如 v3.2.0:
npm uninstall react-native-nested-scroll-view npm install react-native-nested-scroll-view@3.2.0
方法3:添加旧仓库(不推荐)
如果一定要使用 v9.0.0,可在 buildscript 的 repositories 中添加 jcenter 镜像仓库,但 jcenter 已停止维护,存在安全风险:
buildscript { repositories { // 保留现有仓库... maven { url "https://maven.aliyun.com/repository/jcenter" } } }
内容的提问来源于stack exchange,提问作者Patxu
相关产品推荐
相关产品推荐

