我在运行 React Native 项目时遇到问题,我正在使用纱线,但 npm 也显示错误:
$ yarn android
yarn run v1.22.22
$ react-native run-android
info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 1990 file(s) to forward-jetify. Using 8 workers...
info JS server already running.
info Installing the app...
Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain
Caused by: java.lang.ClassNotFoundException: org.gradle.wrapper.GradleWrapperMain
error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081
Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain
Caused by: java.lang.ClassNotFoundException: org.gradle.wrapper.GradleWrapperMain
at makeError (/Users/.../app/node_modules/execa/index.js:174:9)
at /Users/.../app/node_modules/execa/index.js:278:16
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async runOnAllDevices (/Users/.../app/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevices.js:109:5)
at async Command.handleAction (/Users/.../app/node_modules/@react-native-community/cli/build/index.js:192:9)
info Run CLI with --verbose flag for more details.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
我正在使用:
项目首次运行,
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/gradle-wrapper-7.6.jar
,但结果是一样的。
我还尝试使用命令
./gradlew wrapper
重建 gradle 并返回此错误
./gradlew wrapper
Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain
Caused by: java.lang.ClassNotFoundException: org.gradle.wrapper.GradleWrapperMain
我尝试更改
gradle-wrapper.properties
和 android/build.gradle
文件中的 gradlew 版本,并在版本 7.6.1 中添加 gradle.wrapper 但错误仍然存在。
当我(今天)遇到这个问题时,我通过修复 .gitignore 解决了这个问题。 expo 构建使用 git commit,我的问题是我的 mono-repo 中的 .gitignore 包含一行来排除所有 .jar 文件。所以我必须明确允许包含 gradle-wrapper.jar ..
下面文件中的 !.. 行明确允许它。
.gitignore:
# Package files
*.jar
!**/android/gradle/wrapper/gradle-wrapper.jar
如果您的 gitignore 不是问题所在,另请参阅此处更早的讨论以获取其他解决方案: