简体   繁体   中英

Error:Could not initialize class com.android.sdklib.repositoryv2.AndroidSdkHandler

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

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

这是图片的链接

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

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.

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.

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

One simple solution:

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

Resolve this issue by simply downgrading to Java 8

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.

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.

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

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'
.........
.........
}

升级你的 gradle 版本和 gradle 插件版本

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