204

I am new in Android Studio. After setup, When I am trying to import an application I am getting that error So that gradle not able to build.

Error:Could not initialize class org.codehaus.groovy.runtime.InvokerHelper

I checked that my classpath setting for Java is fine. I am running Windows OS. Does anyone know the source of the error?

Sabyasachi
  • 3,499
  • 2
  • 14
  • 21

26 Answers26

347

Please make changes as per below to resolve this error.

Install Java SDK version: 14 or above.

JDK Download link: https://www.oracle.com/java/technologies/javase-jdk14-downloads.html

In gradle-wrapper.properties please use grade version 6.3 or above.

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

Patel Pinkal
  • 8,984
  • 4
  • 28
  • 50
185
  1. This is because of the gradle version.

  2. Go to: gradle/wrapper/gradle-wrapper.properties.

  3. Change a version of the course by this:

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

The console outputs:

Welcome to Gradle 6.3!

Here are the highlights of this release:
 - Java 14 support
 - Improved error messages for unexpected failures

For more details see https://docs.gradle.org/6.3/release-notes.html

Starting a Gradle Daemon (subsequent builds will be faster)
choogoor
  • 15
  • 4
Josileudo Rodrigues
  • 1,979
  • 1
  • 6
  • 9
  • Thanks! This works for me given that I am in a react native project and JAVA 14 / Open JDK! – locropulenton Sep 18 '20 at 20:55
  • 3
    I'm on `java version "15" 2020-09-15` and this worked for me on MacOS Catalina. – Mahad Ahmed Sep 24 '20 at 19:49
  • Thanks this helped. Not sure why this was not yet selected as the accepted answer. – dilantha111 Oct 10 '20 at 11:51
  • If you go to the URL `https://services.gradle.org/distributions/` this will list the versions, then edit your gladle-wrapper.properties file with the relevant link and click `retry` – Rich S Nov 17 '20 at 22:54
  • this was the ideal solution for me, even my gradle on 6.2 version didn't work, just switching to 6.3 that started working – Edson Filho Nov 20 '20 at 20:17
  • This is the correct solution as of 1 des 20 – Stephan Bakkelund Valois Dec 01 '20 at 20:50
  • Apparently, this is not a global solution. I updated gradle with the provided URL. I received a different errors, `Could not open settings remapped class cache for...`, `Could not open settings generic class cache for settings file`, and `BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 60` Got the same error when using `6.4.1` – Mike S. Apr 08 '21 at 13:40
39

First watch this and try reinstall - https://youtu.be/trHinrIm6DM

After if you get "Could not initialize class org.codehaus.groovy.runtime.InvokerHelper"

1.install Java SDK 14 and update env path.

2."[Your RN or Flutter Project Folder]\android\gradle\wrapper\gradle-wrapper.properties" this file edit as ...(upgrade gradle version)

#Fri Jun 23 08:50:38 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip

3.run gradlew in cmd path "[Your RN or Flutter Project Folder]\android\"

4.it takes time.after if shows "BUILD SUCCESSFUL" you can "flutter run" in ur project folder

Yeah It's work

ThamaBoy
  • 391
  • 3
  • 4
26

What was helpful for me (MacBook):

  • first go to File -> Invalidate Caches / Restart -> Invalidate and Restart
  • then check the value of JAVA_HOME enviroment variable, by calling echo $JAVA_HOME (it should be equal to "/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home")
  • if it is not, change its value by calling export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home"
  • This is actually worked with me without the need to update wrapper version, the case that I did a fresh install on my macOS and installed a lot of things and this could have changed the JAVA_HOME and cause this issue, thanks a lot – Ahmed Elshaer Sep 26 '20 at 08:24
  • This worked for me. But it sort of gets reset on every bootup. How can I make this change permanent? – sudonitin Oct 25 '20 at 15:27
  • This saved my life – Pradap Pandian Nov 15 '20 at 08:46
  • 1
    Hey @sudonitin, check there to see how to save enviroment variables permanently: https://medium.com/@youngstone89/setting-up-environment-variables-in-mac-os-28e5941c771c – jakub-adamczewski Nov 17 '20 at 10:21
  • This is huge because you DO NOT have to go through the trouble of installing Java if you have Android Studio. See also https://stackoverflow.com/a/48957460 for shell config to set some things automatically. – dragon788 Mar 05 '21 at 18:24
  • This is not working for me because the Path Please set the JAVA_HOME variable in your environment to match the location of your Java installation., However following this link: https://stackoverflow.com/a/19663996/4696051 allowed me to find the correct Java Home Path in MacOs Catalina /usr/libexec/java_home – JeanCarlos Chavarria May 14 '21 at 00:46
  • Thank you, you save my live – Pentagura May 17 '21 at 07:43
10

I faced this issue because of lower version of Jdk. Previously I installed Jdk 1.7 and Android Studio 1.5.1, I got this issue. If you install Android Studio 1.5.1 or above JDK 1.8 required

So Installing JDK 1.8 solved this issue.

Sabyasachi
  • 3,499
  • 2
  • 14
  • 21
10

I'm using Pop OS 20.04 and I have Java versions 8, 11 and 14 installed on my notebook.

This error was happening to me when version 14 was standard.

When I switched to using version 11 as the default, the error no longer occurred.

sudo update-alternatives --config java
Deyve Machado
  • 130
  • 1
  • 4
8

In gradle-wrapper.properties file, updating the Gradle to 6.3 solved the problem on Mac OS Catalina

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

bongtoi
  • 181
  • 2
  • 9
7

In my case the problem was that my version of Gradle was incompatible with JDK 14, but despite in project structure dialog was selected JDK 8.

It was necessarily to set JDK home for Gradle separately in gradle.propperties:

org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home
informatik01
  • 16,038
  • 10
  • 74
  • 104
txqe
  • 85
  • 1
  • 5
6
  1. Go to \android\gradle\wrapper\gradle-wrapper.properties.
  2. Update the installing distribution version (all.zip) of Gradle in distributionUrl. As an example for Gradle version 6.8:

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

Pasindu Jayanath
  • 892
  • 10
  • 27
5

I face this issue when I was Building my Flutter Application. This error is due to the gradle version that you are using in your Android Project. Follow the below steps:

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

Now Save the File (Ctrl + S), Go to the console and run the command

flutter run

It will take some time, but the issue that you were facing will be solved.

Deep Patel
  • 51
  • 1
  • 3
5

So, you are in 2022, and changing the distribution URL does not work?

Yeah, React Native is such a pain. Make sure you have followed the React Native official guide, then check the Java version with:

java -version

Check compatibility matrix of Gradle versions. Change distribution URL to the version accordingly, from:

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

to

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

where Y.Y is the suitable version for your Java version.

And now, check that $JAVA_HOME is set:

echo $JAVA_HOME

Damn, is empty!

Yes, this environment variable is required, and on Internet you will see examples of exporting this path, for example for MacOS:

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

Be careful, this is not always the correct path. Check it navigating using cd command. For me the path was:

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

Spot the difference. This could be anywhere, so check it carefully as this is the last step that will allow you to run your project with yarn example android

Tested on: macOS Bug Sur, version 11.4. Intel Core i5. Good luck with M1.

PS: I hate React Native required configurations.

2

The problem in my case was in the discrepancy between the Gradle version installed globally and the one required by React Native. To fix it, I had to update the folder android/gradle/wrapper from the current 6.5 RN version from GH.

Vladyslav Zavalykhatko
  • 15,202
  • 8
  • 65
  • 100
  • Also see [“Failed to install the following Android SDK packages as some licences have not been accepted” error](https://stackoverflow.com/a/55641042/11667949https://stackoverflow.com/a/55641042/11667949) – Shivam Jha Mar 25 '21 at 21:48
2

I fixed it just by editing the gradle-wrapper.properties file.

You must go to the project folder, then /android/grandle/wrapper/gradle-wrapper.properties. In DistributionUrl, change to https \: //services.gradle.org/distributions/gradle-6.4.1-all.zip.

Elletlar
  • 3,136
  • 7
  • 32
  • 38
2

If you are using jdk 16 downgrade to 14 and gradle-wrapper.properties use 6.3 don't forget to upgrade build gradle in build.gradle to 4+

2

So I also got the same error on my system when I Cloned a project from github. The project that i created on my system worked without any errors. So if you are also in similar situation this could help 1-Go to this path

project-name/android/gradle/wrapper/gradle-wrapper.properties

2-Replace everything inside with this

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Dharman
  • 30,962
  • 25
  • 85
  • 135
1

When you upgrade to the latest version of the gradle in the gradle-wrapper.properties file i.e. distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip please do not forget to change the gradle version in the build.gradle file as well

wrapper {
    gradleVersion = '6.6.1'
}
Misanty
  • 86
  • 10
1

I get this error when I open a new project with VS Code & Flutter. I solved the problem by editing the gradle.build and gradle-wrapper.properties files.

Edit android>build.gradle like this:

dependencies { classpath 'com.android.tools.build:gradle:3.5.0' classpath 'com.google.gms: google-services: 4.3.3' //add this line classpath "org.jetbrains.kotlin: kotlin-gradle-plugin: $ kotlin_version" }

Edit gradle-wrapper.properties

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

Ali Murat
  • 131
  • 5
1

I also had the same problem and and it looks like this problem is due to the gradle version in the project directory

  • This is the version of my jdk:
openjdk version "15-ea" 2020-09-15
OpenJDK Runtime Environment (build 15-ea+32-Ubuntu-220.04)
OpenJDK 64-Bit Server VM (build 15-ea+32-Ubuntu-220.04, mixed mode, sharing)
  • Please check gradle whether it is installed. This is the version of my gradle:
------------------------------------------------------------
Gradle 6.8
------------------------------------------------------------

Build time:   2021-01-08 16:38:46 UTC
Revision:     b7e82460c5373e194fb478a998c4fcfe7da53a7e

Kotlin:       1.4.20
Groovy:       2.5.12
Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM:          15-ea (Private Build 15-ea+32-Ubuntu-220.04)
OS:           Linux 5.4.0-65-generic amd64
  • Then open the gradle-wrapper.properties file in your project folder, in the folder: project name/android/gradle/wrapper/gradle-wrapper.properties

  • Change the gradle version. Here I changed the gradle version to 6.5

The video version can be seen here: https://www.youtube.com/watch?v=mcclcscUpV0

Fuad Rifki
  • 11
  • 2
1

Resolve It:

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

and I changed it into:

distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-all.zip
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
1

I had the same issue when building the Flutter project, I edited this file gradle-wrapper.properties
location: android -> grade\wrapper -> gradle-wrapper.properties

from this to:

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

to this:

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

but was not solved, I did flutter doctor and found some error in the android toolchain. I open android studio and installed "Android SDK Command-line tools" in SDK manager. https://stackoverflow.com/a/69604498/13577765

and was solved.

Sanjay TM
  • 369
  • 2
  • 17
1

This solution is for M1 Mac User

Find your local Java version by typing /usr/libexec/java_home -V. If didn't exist, install the latest ARM Java version here Set Java_Home path (in my case jdk-17.0.1.jdk) by typing

/usr/libexec/java_home /Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home

Type all of these in terminal

cd /Applications/Android\ Studio.app/Contents/jre ln -s ../jre jdk ln -s "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin" jdk

Finally run flutter doctor -v to see all the details changes

0

gradle-wrapper.properties please use grade version 6.3 or above

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

../android/gradle/wrapper/gradle-wrapper.properties

Vivek
  • 176
  • 1
  • 8
0

For me the solution was to upgrade the gradle version to 6.3 from the android project structure (java 14.0.1 is already installed on my pc).

3.dayma
  • 99
  • 1
  • 5
0

(Solution) I tried my first flutter app in android studio , i was getting same error " Could not initialize class org.codehaus.groovy.runtime.InvokerHelper"

open build.gradle and update dependencies

    classpath 'com.android.tools.build:gradle:4.0.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

or just hover over com.android.tools.build:grandle:(your-version)

0

When I tried "react-native run-android" I was receiving errors "Could not initialize class org.codehaus.groovy.runtime.InvokerHelper" + "Failed to install the app. Make sure you have the Android development environment set up"....

Solved it by updating the gradle in Android Studio. When I opened my project in Android Studio it showed a message asking to update Gradle, and I just clicked.

0

It is not enough to change distributionUrl in gradle-wrapper.properties, you also need to change gradle version in your project structure.

  1. Go to gradle-wrapper.properties and change:

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

  1. Go to Android studio -> file -> project structure

Change gradle verison to 6.8 or whatever verison you selected that is compatible with your jdk.

M.Mark
  • 71
  • 1
  • 6