Java 错误:无法初始化类 com.android.sdklib.repositoryv2.AndroidSdkHandler

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37513651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:24:17  来源:igfitidea点击:

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

javaandroidandroid-studiogradlebuild

提问by Usm

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

我想在启动时用 Android Studio构建这个项目,但是出错了。堆栈跟踪在这里

采纳答案by Mihir Shah

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

当您的系统中安装了多个JDK时会出现此问题,我遇到了与错误安装oracle-jdk-9和Android studio需要oracle-jdk-8相同的问题

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

如果您使用的是 Ubuntu,则可以从此问题安装 jdk-8 。

So, Make following changes as shown below:

因此,进行以下更改,如下所示:

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

ctrl+ shift+ alt+s将打开项目结构,也可以从

File -> Project Structure

文件 -> 项目结构

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

然后更改您可能已安装 JDK 8 的 JDK Location

Here is the link to image

这是图片的链接

回答by Roberto Erhard

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

我有同样的问题。我已经安装了 Java 8 和 Java 9。我设置JAVA_HOME为 Java 8,但 Gradle 使用的是 Java 9。

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

我将 File->Project Structure 中的 JDK 更改为 Java 8 并且它起作用了。

回答by FinHead

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.

将 Android Studio 中的 JDK 更改为指向 8 对我不起作用。我卸载了 Java 9,这解决了这个问题。

回答by Ramon Melo

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-> SDKswith a folder behind a tiny blue cup of coffee.

如果有人在使用 IntelliJ IDEA 时遇到问题,请转到File-> Project Structure(或按Ctrl+ Alt+ Shift+ S)并删除所有 JDK 9 条目。JDK 被标记在Platform Settings-> 下SDKs,在蓝色的小咖啡杯后面有一个文件夹。

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.

我尝试将正确的路径添加到 JDK 1.8,但无论出于何种原因,IntelliJ IDEA 都会默认返回到 JDK 9。不幸的是,我需要 JDK 9 用于其他项目,所以卸载它太麻烦了。如果您需要将 JDK 9 用于另一个项目(就像我一样),您仍然可以通过相同的菜单添加一个 JDK 9 条目,然后在需要时再次将其删除。

回答by serv-inc

If you have several Javas in Ubuntu, you can

如果你在 Ubuntu 中有多个 Java,你可以

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

to select the Java 8 that Android and Gradle need.

选择 Android 和 Gradle 需要的 Java 8。

回答by reza rahmad

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

这是因为您在 build.gradle 根项目中的类路径构建工具已被弃用,对于新的 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

之后将您的最小 sdk 和构建工具更新为最新版本,再次没有问题

回答by Sanchit Bhatnagar

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

如果问题仍然存在,只需将 gradle 导入 Android Studio 并在 Android Studio 中构建文件。

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

Android Studio 会自动处理 gradle 和 sdk 问题。成功构建后,您还可以尝试通过react-native run-android命令提示符中运行该应用程序。希望这可以帮助。这是解决此问题的唯一修复程序。我尝试了上述所有答案,但问题仍然存在

回答by HimalayanCoder

JAVA 9 has forcefully taken over the JAVA_HOME to itself.

JAVA 9 已经强行接管了 JAVA_HOME 给自己。

after installing java9 you will find

安装java9后你会发现

# 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

因此,与其卸载 java 9,不如让我们将 JAVA_HOME 指向 java 8

First find out the Android studio embeded JRE location

首先找出Android studio内嵌JRE的位置

and add an entry in ~./bash_profile at last

最后在 ~./bash_profile 添加一个条目

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

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

回答by Vaibhav Bacchav

One simple solution:

一个简单的解决方案:

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

React-Native 与 Java 9 或 Java 10 不兼容

Resolve this issue by simply downgrading to Java 8

只需降级到 Java 8 即可解决此问题