Gradle error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain:
I faced this issue on a react native project. This issue might appear on an Android project as well. This is related to gradle wrapper and the reason is something related to gradle wrapper is broken or deleted from the project.
When you create a release Android APK, you use
./gradlew assembleRelease. Here,
gradlew is the gradle wrapper executable. On windows, it is a batch script and on linux or mac it is a shell script. This script downloads the gradle that is defined in the project and uses it to build the project.
This is included with the project files and it helps anyone who works on the project to use the same gradle version without needing to install it beforehand.
Full stack trace:
Below is the complete stack trace:
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/cvc/Desktop/reduxRN/node_modules/execa/index.js:174:9) at /Users/cvc/Desktop/reduxRN/node_modules/execa/index.js:278:16 at processTicksAndRejections (node:internal/process/task_queues:96:5) at async runOnAllDevices (/Users/cvc/Desktop/reduxRN/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevices.js:109:5) at async Command.handleAction (/Users/cvc/Desktop/reduxRN/node_modules/@react-native-community/cli/build/index.js:192:9)
Cause and Solution of this error:
The common reason of this error is that something related to gradle wrapper is deleted and most probably it is the gradle-wrapper.jar file. You can check this file in gradle/wrapper/gradle-wrapper.jar path of your project.
gitignore file removes all jar files and if this is excluded in the .gitignore file, you should add it:
git add -f gradle/wrapper/gradle-wrapper.jar
How to restore gradle wrapper:
You can either get the jar file from other team member and put it in the gradle/wrapper folder to make it work. Or, you can install gradle and add the wrapper jar file with the below command:
To learn how to install gradle on your system, please refer to this guide.
You can also create a new react native project. It will add the .jar file. Copy and paste it to the failed project and it should work.