36

I want to build this project with Android Studio at launching, but take a error. Stacktrace is here

mehrdad khosravi
  • 2,228
  • 9
  • 29
  • 34
Usm
  • 451
  • 2
  • 5
  • 8

10 Answers10

40

This problem occurs when there are multiple JDKs installed in your system, I had the same issue as I had mistakenly installed oracle-jdk-9 and Android studio requires oracle-jdk-8

If you are using Ubuntu you can install jdk-8 from this question.

So, Make following changes as shown below:

Press ctrl+shift+alt+s that will open project structure which can also be opened from

File -> Project Structure

And then change JDK Location where you may have installed JDK 8

Here is the link to image

B W
  • 692
  • 1
  • 8
  • 21
Mihir Shah
  • 516
  • 5
  • 3
23

this because your classpath build tools in build.gradle root project is deprecated update like this for new android studio 3.2.0

buildscript {
dependencies {
    classpath 'com.android.tools.build:gradle:3.2.0-alpha14'
}

and after that update your minimum sdk and build tools to latest and no problem again

reza rahmad
  • 1,009
  • 10
  • 16
14

I had the same problem. I had installed Java 8 and Java 9. I set JAVA_HOME to Java 8, but Gradle was using Java 9.

I changed the JDK in File->Project Structure to Java 8 and it worked.

Laurel
  • 5,965
  • 14
  • 31
  • 57
8

JAVA 9 has forcefully taken over the JAVA_HOME to itself.

after installing java9 you will find

# java -version
java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)

So instead of uninstalling java 9 let us point the JAVA_HOME back to java 8

First find out the Android studio embeded JRE location

and add an entry in ~./bash_profile at last

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

# source ~/.bash_profile
# java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

source http://scalebean.blogspot.com/2018/01/fix-gradle-could-not-initialize-class.html

HimalayanCoder
  • 9,630
  • 6
  • 59
  • 60
7

Changing the JDK in Android Studio to point back at 8 didn't work for me. I uninstalled Java 9 and this solved the issue.

FinHead
  • 517
  • 4
  • 12
6

One simple solution:

React-Native isn't compatible with Java 9 or Java 10

Resolve this issue by simply downgrading to Java 8

Vaibhav KB
  • 1,695
  • 19
  • 17
3

Go to build.gradle,
and make sure your gradle dependencies up to date.
For Android studio 4.2.2
dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
.........
.........
}

2

If you have several Javas in Ubuntu, you can

sudo update-alternatives --config java
sudo update-alternatives --config javac

to select the Java 8 that Android and Gradle need.

serv-inc
  • 35,772
  • 9
  • 166
  • 188
1

If anyone is having issues with IntelliJ IDEA, go to File -> Project Structure (or press Ctrl + Alt + Shift + S) and remove all JDK 9 entries. JDKs are marked under Platform Settings -> SDKs with a folder behind a tiny blue cup of coffee.

I tried adding the correct path to JDK 1.8 but IntelliJ IDEA will default back to JDK 9 for whatever reason. Unfortunately, I need JDK 9 for other projects, so uninstalling it would be too much of a hassle. If you need to use JDK 9 for another project (like I do), you can still add a JDK 9 entry via the same menu and then remove it again when required.

Ramon Melo
  • 270
  • 2
  • 9
  • 21
0

If the Problem Persists, just import the gradle to Android Studio and build the file inside the Android Studio.

Android Studio automatically handles gradle and sdk problems. After successful build you can also try running the app via react-native run-android in the Command Prompt. Hope this Helps. This was the only fix that fixed this issue. I tried all the above answers but problem persisted

Sanchit Bhatnagar
  • 874
  • 2
  • 10
  • 16