React Native 0.64.0项目构建失败:Gradle同步及运行异常求助
解决React Native 0.64.0 Gradle同步依赖缺失问题
我用React Native 0.64.0创建了一个全新项目,未添加任何额外模块或代码,但Gradle同步和项目运行均失败,同步时抛出依赖找不到的错误。
报错信息
FAILURE: Build completed with 7 failures. 1: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':app:checkDebugAarMetadata'. > Could not resolve all files for configuration ':app:debugRuntimeClasspath'. > Could not find androidx.swiperefreshlayout:swiperefreshlayout:1.0.0. Searched in the following locations: - https://repo.maven.apache.org/maven2/androidx/swiperefreshlayout/swiperefreshlayout/1.0.0/swiperefreshlayout-1.0.0.pom - file:/Users/amirrezabehrouzi/.m2/repository/androidx/swiperefreshlayout/swiperefreshlayout/1.0.0/swiperefreshlayout-1.0.0.pom - file:/Users/amirrezabehrouzi/testChart2/node_modules/react-native/android/androidx/swiperefreshlayout/swiperefreshlayout/1.0.0/swiperefreshlayout-1.0.0.pom - file:/Users/amirrezabehrouzi/testChart2/node_modules/jsc-android/dist/androidx/swiperefreshlayout/swiperefreshlayout/1.0.0/swiperefreshlayout-1.0.0.pom - https://dl.google.com/dl/android/maven2/androidx/swiperefreshlayout/swiperefreshlayout/1.0.0/swiperefreshlayout-1.0.0.pom - https://www.jitpack.io/androidx/swiperefreshlayout/swiperefreshlayout/1.0.0/swiperefreshlayout-1.0.0.pom Required by: project :app project :app > com.facebook.react:react-native:0.64.0 > Could not find androidx.appcompat:appcompat:1.1.0. Searched in the following locations: - https://repo.maven.apache.org/maven2/androidx/appcompat/appcompat/1.1.0/appcompat-1.1.0.pom - file:/Users/amirrezabehrouzi/.m2/repository/androidx/appcompat/appcompat/1.1.0/appcompat-1.1.0.pom - file:/Users/amirrezabehrouzi/testChart2/node_modules/react-native/android/androidx/appcompat/appcompat/1.1.0/appcompat-1.1.0.pom - file:/Users/amirrezabehrouzi/testChart2/node_modules/jsc-android/dist/androidx/appcompat/appcompat/1.1.0/appcompat-1.1.0.pom - https://dl.google.com/dl/android/maven2/androidx/appcompat/appcompat/1.1.0/appcompat-1.1.0.pom - https://www.jitpack.io/androidx/appcompat/appcompat/1.1.0/appcompat-1.1.0.pom Required by: project :app > com.facebook.flipper:flipper:0.75.1 > Could not find androidx.sqlite:sqlite-framework:2.1.0. Searched in the following locations: - https://repo.maven.apache.org/maven2/androidx/sqlite/sqlite-framework/2.1.0/sqlite-framework-2.1.0.pom - file:/Users/amirrezabehrouzi/.m2/repository/androidx/sqlite/sqlite-framework/2.1.0/sqlite-framework-2.1.0.pom - file:/Users/amirrezabehrouzi/testChart2/node_modules/react-native/android/androidx/sqlite/sqlite-framework/2.1.0/sqlite-framework-2.1.0.pom - file:/Users/amirrezabehrouzi/testChart2/node_modules/jsc-android/dist/androidx/sqlite/sqlite-framework/2.1.0/sqlite-framework-2.1.0.pom - https://dl.google.com/dl/android/maven2/androidx/sqlite/sqlite-framework/2.1.0/sqlite-framework-2.1.0.pom - https://www.jitpack.io/androidx/sqlite/sqlite-framework/2.1.0/sqlite-framework-2.1.0.pom Required by:
当前android/build.gradle配置
repositories { exclusiveContent { filter { includeGroup "com.facebook.react" } forRepository { maven { url "$rootDir/../node_modules/react-native/android" } } } mavenCentral() mavenLocal() 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") } google() maven { url 'https://www.jitpack.io' } }
系统信息
- CPU: (8) x64 Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
- Node: 14.19.3 - ~/.nvm/versions/node/v14.19.3/bin/node
- Yarn: 1.22.19 - /usr/local/bin/yarn
- npm: 6.14.17 - ~/.nvm/versions/node/v14.19.3/bin/npm
- Java: 17.0.5 - /usr/bin/javac
解决方案
- 调整仓库顺序,优先检索Google仓库
Gradle按仓库列表顺序检索依赖,google()仓库包含绝大多数AndroidX依赖,需将其移至列表最前端。修改后的仓库配置如下:
repositories { google() mavenCentral() mavenLocal() exclusiveContent { filter { includeGroup "com.facebook.react" } forRepository { maven { url "$rootDir/../node_modules/react-native/android" } } } 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") } maven { url 'https://www.jitpack.io' } }
- 清理Gradle缓存
执行命令清理本地缓存,避免损坏的缓存依赖干扰同步:
./gradlew cleanBuildCache rm -rf ~/.gradle/caches
清理完成后重新执行Gradle同步。
- 检查网络访问性
确保网络可正常访问Google Maven仓库,若网络受限,可替换为国内镜像仓库,示例如下:
maven { url 'https://maven.aliyun.com/repository/google' } maven { url 'https://maven.aliyun.com/repository/public' }
- 切换兼容的Java版本
React Native 0.64.0推荐使用Java 8或Java 11,当前Java 17可能存在兼容性问题,建议通过版本管理工具切换至Java 11,或修改Android Studio的JDK配置。
内容的提问来源于stack exchange,提问作者amir behrouzi
相关产品推荐
相关产品推荐

