React Native运行Android项目时GradleWrapperMain类加载失败求助
问题描述
执行yarn android或npm run android时触发错误:
Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain Caused by: java.lang.ClassNotFoundException: org.gradle.wrapper.GradleWrapperMain
环境配置
- Node: v18.20.3
- Yarn: 1.22.22
- Java: java version "17.0.10" 2024-01-16 LTS
- react-native: 0.69.4
- react: 18.0.0
- Android模拟器:Android 13
- 操作系统:MacOS Sonoma 14.4 (M1)
项目关键配置
package.json依赖片段
{ "private": true, ... "dependencies": { "@invertase/react-native-apple-authentication": "^2.2.2", "@react-native-async-storage/async-storage": "^1.17.10", "@react-native-google-signin/google-signin": "^8.0.0", "@react-native-picker/picker": "^2.4.4", "@react-navigation/bottom-tabs": "^6.3.3", "@react-navigation/native": "^6.0.12", "@react-navigation/native-stack": "^6.8.0", "@reduxjs/toolkit": "^1.8.5", "axios": "^0.27.2", "jwt-decode": "^3.1.2", "moment": "^2.29.4", "react": "18.0.0", "react-hook-form": "^7.35.0", "react-native": "0.69.4", "react-native-calendars": "^1.1289.0", "react-native-confirmation-code-field": "6", "react-native-date-picker": "^4.2.5", "react-native-fbsdk-next": "^10.1.0", "react-native-gesture-handler": "^2.8.0", "react-native-get-location": "^2.2.1", "react-native-image-picker": "^4.10.0", "react-native-keyboard-aware-scroll-view": "^0.9.5", "react-native-linear-gradient": "^2.6.2", "react-native-maps": "^1.7.1", "react-native-modal": "^13.0.1", "react-native-modalize": "^2.1.1", "react-native-onesignal": "^4.4.1", "react-native-pager-view": "^6.1.4", "react-native-picker-select": "^8.0.4", "react-native-qrcode-svg": "^6.1.2", "react-native-rename": "^3.2.12", "react-native-safe-area-context": "^4.3.3", "react-native-screens": "^3.18.2", "react-native-simple-encryption": "^1.2.1", "react-native-svg": "^13.9.0", "react-native-switch-selector": "^2.2.1", "react-native-tab-view": "^3.5.1", "react-native-vision-camera": "^2.14.1", "react-redux": "^8.0.2" }, "devDependencies": { "@babel/core": "^7.12.9", "@babel/runtime": "^7.12.5", "@react-native-community/eslint-config": "^2.0.0", "babel-jest": "^26.6.3", "eslint": "^7.32.0", "jest": "^26.6.3", "metro-react-native-babel-preset": "^0.70.3", "react-test-renderer": "18.0.0" }, "jest": { "preset": "react-native" } }
gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip networkTimeout=10000 zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists
android/build.gradle
buildscript { ext { buildToolsVersion = "33.0.0" minSdkVersion = 21 compileSdkVersion = 33 targetSdkVersion = 33 googlePlayServicesAuthVersion = "19.2.0" kotlinVersion = "1.6.10" 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" } } repositories { google() mavenCentral() } dependencies { classpath("com.android.tools.build:gradle:7.6.1") // I made the verion changes here between 7.6 and 7.6.1 classpath("com.facebook.react:react-native-gradle-plugin") classpath("de.undercouch:gradle-download-task:5.0.1") classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10" classpath 'com.google.gms:google-services:4.3.10' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { 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") } mavenCentral { // We don't want to fetch react-native from Maven Central as there are // older versions over there. content { excludeGroup "com.facebook.react" } } google() maven { url 'https://www.jitpack.io' } } }
已尝试的无效方案
- 下载
gradle-wrapper-7.6.jar放入android/gradle/wrapper/路径 - 执行
./gradlew wrapper命令重建Gradle - 修改
gradle-wrapper.properties和android/build.gradle中的Gradle版本为7.6.1
解决方案
方案1:彻底清理Gradle缓存与本地Wrapper文件
- 删除项目内的
android/gradle/wrapper/gradle-wrapper.jar文件 - 删除本地Gradle缓存目录:
~/.gradle/wrapper/dists/gradle-7.6-bin/ - 执行以下命令重建Wrapper并构建:
确保cd android ./gradlew clean ./gradlew wrapper --gradle-version 7.6 --distribution-type bin cd .. yarn androidgradle-wrapper.properties内的distributionUrl与指定版本一致。
方案2:检查Java环境变量配置
Gradle 7.6兼容Java 17,需确认环境变量指向正确:
- 执行
echo $JAVA_HOME,确认路径指向Java 17安装目录 - 若路径错误,在
~/.zshrc或~/.bash_profile中添加:export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.0.10.jdk/Contents/Home export PATH=$JAVA_HOME/bin:$PATH - 执行
source ~/.zshrc生效后,重新运行构建命令。
方案3:修复文件权限(M1 Mac专属)
M1 Mac易出现权限异常,执行以下命令:
cd android chmod +x gradlew ./gradlew wrapper --gradle-version 7.6
完成后重新尝试构建。
方案4:全量重置项目依赖与Gradle配置
- 删除项目内
android/.gradle、node_modules目录,以及yarn.lock/package-lock.json文件 - 重新安装依赖:
yarn install - 进入android目录执行:
./gradlew build --refresh-dependencies - 回到根目录运行
yarn android
内容的提问来源于stack exchange,提问作者Abdiel49
相关产品推荐
相关产品推荐

