MacOS环境下运行React Native Android项目Gradle编译报错求助
yarn android Build Hey there! Let's get this Gradle error sorted out for you. That "major version 61" error is a super common Java/Gradle compatibility hiccup—here's what's going on and how to fix it:
What's Causing This?
Class file major version 61 corresponds to Java 17, and your current Gradle version doesn't support it. Gradle has strict compatibility rules with Java versions; older Gradle releases can't handle newer Java runtime files, which is exactly what's happening here.
Solution 1: Downgrade Java to a Version Supported by Your Gradle
If you're using an older React Native version (pre-0.70) or haven't updated Gradle yet, this is the most straightforward fix:
- First, check your current Java version:
java -version - Install Java 11 (it's a stable, widely supported version for most React Native projects pre-0.70)
- Switch your system's Java environment to use Java 11:
- On macOS/Linux: Set the
JAVA_HOMEenvironment variable temporarily with:
(Or use a version manager likeexport JAVA_HOME=/path/to/your/java11/installationsdkmanto switch permanently for all projects) - On Windows: Update the
JAVA_HOMEsystem environment variable to point to your Java 11 installation folder
- On macOS/Linux: Set the
- Run
yarn androidagain—this should resolve the compatibility issue right away
Solution 2: Upgrade Gradle to Support Java 17
If you want to keep using Java 17 (perfect for newer React Native versions like 0.70+), you'll need to upgrade your Gradle setup:
- Open
android/gradle/wrapper/gradle-wrapper.properties - Update the
distributionUrlto a Gradle version that supports Java 17 (Gradle 7.3 or later works great). For example:distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip - Open
android/build.gradleand update the Android Gradle Plugin to match your new Gradle version. For Gradle 7.5, use plugin version 7.4.2:dependencies { classpath("com.android.tools.build:gradle:7.4.2") // ... keep your other dependencies here } - Let Gradle sync the project (it'll download the new version automatically), then run
yarn androidagain
Quick Check: React Native Version Compatibility
Just a heads-up to make sure your setup aligns with your RN version:
- React Native 0.70+: Supports Java 17 and requires Gradle 7.3+
- React Native 0.68-0.69: Works best with Java 11 and Gradle 7.x
- Older RN versions: Stick to Java 8/11 and their matching Gradle versions
内容的提问来源于stack exchange,提问作者David Odinaka Enyoghasim

