简体   繁体   中英

How to solve (Could not initialize class org.codehaus.groovy.reflection.ReflectionCache) issue in react native

$ npx react-native run-android info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag. Jetifier found 864 file(s) to forward-jetify. Using 4 workers... info Starting JS server... info Launching emulator... error Failed to launch the emulator. Reason: Could not start an emulator within 30 seconds. warn Please launch an emulator manually or connect a device. Otherwise, the app may fail to launch. info Installing the app...

FAILURE: Build failed with an exception.

  • What went wrong: Could not initialize class org.codehaus.groovy.runtime.InvokerHelper

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 1m 23s

error Failed to install the app. Make sure you have the Android development environment set up: https://facebook.github.io/react-native/docs/getting-started.html#android-development-environment . Run CLI with --verbose flag for more details. Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081

FAILURE: Build failed with an exception.

  • What went wrong: Could not initialize class org.codehaus.groovy.runtime.InvokerHelper

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 1m 23s

at checkExecSyncError (child_process.js:629:11)
at execFileSync (child_process.js:647:13)
at runOnAllDevices (E:\work\react-native\AwesomeProject1\node_modules\@react-native-community\cli-platform-android\build\commands\runAndroid\runOnAllDevices.js:94:39)

I think I had the same issue. After installing RN (May 20th, 2020), I bumped into this error. I found this solution on https://github.com/gradle/gradle/issues/10248 .

In short:

  1. Go to /android/gradle/wrapper/gradle-wrapper.properties file in your RN project

  2. Find this line that starts with distributionUrl . Change the gradle version into gradle-6.3-all.zip .

In my project, it was:

distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-all.zip

and I changed it into:

distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip

That's it.

I had the same error on Mac OS and React Native v0.62.

Apply these instructions correctly: React native environment setup

Adding this variable solved my problem:

export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home"

In the React Native version 0.62.x even the initialized application is not ready for a development build.

I change the Gradle version to the latest and I could to have a clean build, so change this file:

// this file => /android/gradle/wrapper/gradle-wrapper.properties

from:

distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-all.zip

to:

distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip

Pay Attention : The latest version of the Gradle for my time is 6.5 . when you are reading this answer please check this link to see the latest version number of the Gradle.

Update Jul 2021

7.1.1

I fixed this by these steps:

  1. install android studio 3.5 (or later)
  2. install java 1.8
  3. if you have java JDE 14.xx you have to remove its folder from library/java/JavaVirtualMachines/jdk-14.xxjdk (on mac os)
  4. install an older version of JDE (I installed 11.0.6)
  5. npm run android!

Just follow these 2 steps only and take your coffee.

  1. Navigate to 'android' > gradle > wrapper > gradle-wrapper.properties

  2. Change 'distributionUrl' from old to >

    distributionUrl=https://services.gradle.org/distributions/gradle-6.3-all.zip

If you are a macOS user then you should install

brew cask install adoptopenjdk/openjdk/adoptopenjdk8
brew cask install android-platform-tools

update your .bash_profile

nano ~/.bash_profile
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

This would resolve your problem. If you are working on a legacy codebase then changing the distribution URL would lead to other problems. It usually happens because of mismatching java versions.

It's probably because you use Gradle as the build system and JDK14 and the Gradle version is old. Reference:

If you use Gradle Wrapper then refer to

$PROJECT_ROOT/gradle/wrapper/gradle-wrapper.properties

Property distributionUrl should be:

distributionUrl=https://services.gradle.org/distributions/gradle-6.3-bin.zip

If it's an older version then change it, run ./gradlew clean build and try again.

It is because your Gradle version is not supported by the Java JDK. If you use the JDK version 15.0.2 then you need to Gradle version change 6.6.1 or 6.7

To Change the Gradle Version in Android Studio.

android[project_name]/gradle/wrapper/gradle-wrapper.properties

Open this file and set this

distributionUrl=https://services.gradle.org/distributions/gradle-6.6.1-all.zip

then this problem will be solved.

To check the java JDK version.

open cmd. run echo %JAVA_HOME%

Then you can also search on google to check the supported version of Gradle to the JDK.

在 mac OS 中更新 JAVA_HOME 对我有用

export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home"

This issue is caused by incorrect version of graddle. so to fix this follow these steps

  • Make sure you have Android studio installed and SDK is setuped from Android studio menu Tool-> SDK Manager you can setup SDK, install latest SDK if you are using latest version of React Native .
  • From Android Studio menu Tools > AVD Manager Setup one device and download the image. and test it to see that emulator is running OK.
  • Make Sure JDK is installed for latest react native install latest JDK : https://www.oracle.com/java/technologies/javase-downloads.html
  • Next from Android Studio menu File > New > new project create one dummy project aka Hello world and build make sure all android studio issues resolved and your Android studio hello world app is running on emulator.
  • Now time to fix your react native app. Stop your Android studio hello world app and keep the emulator running.
  • Locate your Android studio hello world app on your system and go to gradle\\wrapper folder and copy the gradle-wrapper.properties file

example in windows case my hello world app is located in my documents: C:\\Users\\Superman\\Documents\\android\\hello world\\gradle\\wrapper

  • Next go to your react native app folder like : myReactApp\\android\\gradle\\wrapper and paste the gradle-wrapper.properties file you copied from your Android studio hello world app.
  • finally in your terminal execute : npx react-native run-android

当您使用的gradle版本与您的环境不兼容时会发生此错误,因此您只需要将您的 gradle 版本更新到最新的Gradle 版本,只需转到: /android/gradle/wrapper/gradle-wrapper.properties到您的项目文件.

I have the same issue with new project and windows. I try all these way but it doesn'. Ridiculously, I move java/jdk folder from C:\\Program Files to another dir in D:\\ and it works like a charm. May be it's useful.

Auguest 2021

I was using JDK 16.0.1 and faced this issue. So, for JDK 16.0.1 you need 7.0 version of gradle .

I fixed it by going to /android/gradle/wrapper/gradle-wrapper.properties and changing

distributionUrl=https\://services.gradle.org/distributions/gradle-6.2-all.zip

to

distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-all.zip

December 2021

If you're using the latest version of Java Development Kit, you'll need to change the Gradle version of your project so it can recognize the JDK. You can do that by going to {project root folder}\\android\\gradle\\wrapper\\gradle-wrapper.properties and changing the distributionUrl value to upgrade the Gradle version. You can check out here the lastest releases of Gradle .

If you're looking for a solution for a Java/Kotlin Core Android project and not a React Project because i was also facing this problem in my core android project so here you go... None of the above solutions worked for me. So basically the idea is to Change the version of gradle. How to do that you ask? Follow these steps:

  1. File > Project Structure
  2. Select SDK Location
  3. You will find something written as "JDK was moved to Gradle Settings". Click on Gradle Settings .
  4. Use Gradle Version 11.0
  5. Sync the project again.

And there you go! A successfully built project! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM