React Native升级至0.76.7后执行./gradlew build依赖查找失败求助
升级React Native 0.76.7时Gradle构建报错:Could not find com.facebook.react:react-native:0.76.7
完整错误信息
Could not determine the dependencies of task ':react-native-async-storage_async-storage:extractDebugAnnotations'. > Could not resolve all dependencies for configuration ':react-native-async-storage_async-storage:debugCompileClasspath'. > Could not find com.facebook.react:react-native:0.76.7. Required by: project :react-native-async-storage_async-storage
当前android/build.gradle内容
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext { buildToolsVersion = "35.0.0" minSdkVersion = 24 compileSdkVersion = 35 targetSdkVersion = 34 kotlinVersion = "1.9.25" } repositories { google() mavenCentral() } dependencies { classpath("com.android.tools.build:gradle") classpath('com.facebook.react:react-native-gradle-plugin') classpath("org.jetbrains.kotlin:kotlin-gradle-plugin") } } // apply plugin: "com.facebook.react.rootproject" // apply plugin: "com.facebook.react" allprojects { repositories { mavenLocal() mavenCentral() google() maven { url 'https://www.jitpack.io' } } def REACT_NATIVE_VERSION = "0.76.7" // Manually specify the version configurations.all { resolutionStrategy { force "com.facebook.react:react-native:$REACT_NATIVE_VERSION" // force "com.facebook.react:react-native:+" } } }
已尝试的解决方法
- 启用注释的
apply plugin: "com.facebook.react.rootproject",但报错:"Failed to apply plugin 'com.facebook.react.rootproject'." - 完全注释掉
configurations.all代码块,报错:"Could not find any matches for com.facebook.react:react-native:+" - 确认package.json中已安装RN 0.76.7
- 执行
./gradlew clean - 查阅官方文档
- 检查已安装的Android版本(android-31, android-34-ext8, android-35)
解决方案
1. 修复Gradle依赖版本绑定与插件配置
修改android/build.gradle,确保React Native Gradle插件版本与RN版本一致,同时添加本地RN仓库地址:
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext { buildToolsVersion = "35.0.0" minSdkVersion = 24 compileSdkVersion = 35 targetSdkVersion = 34 kotlinVersion = "1.9.25" reactNativeVersion = "0.76.7" // 绑定RN版本到全局变量 } repositories { google() mavenCentral() } dependencies { // 指定AGP版本,RN 0.76要求至少8.5.0 classpath("com.android.tools.build:gradle:8.5.0") // 绑定RN Gradle插件版本与RN版本一致 classpath("com.facebook.react:react-native-gradle-plugin:${reactNativeVersion}") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}") } } // 正确应用根项目插件(版本匹配后即可正常使用) apply plugin: "com.facebook.react.rootproject" allprojects { repositories { mavenLocal() mavenCentral() google() maven { url 'https://www.jitpack.io' } // 添加本地RN模块的Maven仓库,优先从本地node_modules读取 maven { url "$rootDir/../node_modules/react-native/android" } maven { url "$rootDir/../node_modules/jsc-android/dist" } } // 移除手动版本强制,RN插件会自动管理依赖版本 // configurations.all { // resolutionStrategy { // force "com.facebook.react:react-native:$REACT_NATIVE_VERSION" // } // } }
2. 检查Gradle wrapper版本
确保gradle/wrapper/gradle-wrapper.properties中的Gradle版本与AGP匹配(AGP 8.5.0对应Gradle 8.5):
distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists
3. 清理缓存并重新构建
执行以下命令彻底清理并重新安装依赖:
rm -rf node_modules rm package-lock.json yarn.lock npm install # 或 yarn install ./gradlew clean ./gradlew build
4. 验证本地RN包完整性
检查node_modules/react-native/android/com/facebook/react/react-native/0.76.7目录是否存在,若缺失则重新执行npm install确保依赖安装完整。
内容的提问来源于stack exchange,提问作者fordat
相关产品推荐
相关产品推荐

