Java 使用 JRE 1.8 或更高版本运行 Android Studio

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36638657/
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 18:30:28  来源:igfitidea点击:

Run Android Studio with JRE 1.8 or later version

javaandroid

提问by Prashant Jajal

I am facing issue for Android N layout XML preview. it is showing following message :

我正面临 Android N 布局 XML 预览的问题。它显示以下消息:

"Android N requires the IDE to be running with Java 1.8 or later"

“Android N 要求 IDE 运行 Java 1.8 或更高版本”

also i install JAVA 1.8 in my OS.

我也在我的操作系统中安装了 JAVA 1.8。

also change the project JDK location with Java 1.8.

还使用 Java 1.8 更改项目 JDK 位置。

but when i see help->about of android studio it is show always 1.7.

但是当我看到帮助->关于 android studio 时,它总是显示 1.7。

how can i change it.?

我怎样才能改变它。?

please help me.here is the image when help->about JRE:1.7

请帮我。这是 help->about JRE:1.7 时的图像

回答by TejjD

Android Studio might show the Java Version that it is set to use as 1.7, but your project can still be set to use 1.8.

Android Studio 可能会显示它设置为使用 1.7 的 Java 版本,但您的项目仍然可以设置为使用 1.8。

Firstly add the following to your gradle file (Refer to: Official Android Dev - Java 8 in Android Studio)

首先将以下内容添加到您的 gradle 文件中(请参阅:Official Android Dev - Android Studio 中的 Java 8

defaultConfig {
...
    HymanOptions {
      enabled true
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

Secondly go to "File" -> "Project" -> "SDK Location"

其次转到“文件”->“项目”->“SDK位置”

Check that the JDK Location is pointing to 1.8 and not 1.7

检查 JDK 位置是否指向 1.8 而不是 1.7

Running your Android N emulator should now work. Regardless of whether Android Studio itself states it's using 1.7 or 1.8.

运行您的 Android N 模拟器现在应该可以工作了。无论 Android Studio 本身是否声明它使用的是 1.7 或 1.8。



Follow the guidelines that are provided on the Android site:

请遵循 Android 站点上提供的准则:

Set up for Android N

为 Android N 设置

回答by Krishna Meena

Go to File -> Project Structure --> SDK Location and check JDK location to set jdk 1.8 or not

转到 File -> Project Structure --> SDK Location 并检查 JDK 位置以设置 jdk 1.8 与否

See the attached screenshot for more

查看附加的屏幕截图了解更多

enter image description here

在此处输入图片说明

回答by akofman

As it is mentioned in the documentationyou can add the STUDIO_JDK env var to configure the IDE JDK. In order to access it from your GUI, you can add this line to your ~/.profile file :

正如文档中提到的,您可以添加 STUDIO_JDK env var 来配置 IDE JDK。为了从您的 GUI 访问它,您可以将此行添加到您的 ~/.profile 文件中:

launchctl setenv STUDIO_JDK /Library/Java/JavaVirtualMachines/jdk<version>.jdk

回答by Caolem

You have to set JAVA_HOME in your operating system.

您必须在您的操作系统中设置 JAVA_HOME。

Windows

视窗

  1. Install the JDK 1.8 and note the destination path, usually C:\Program Files\Java\jdk1.8.0_xx
  2. Right-click My Computerand select Properties.
  3. Click the Advanced System Settingslink in the left column.
  4. In the tab Advancedclick Environment Variables
  5. Create or modify the variable JAVA_HOME.
  6. Enter the variable value as the installation path for the Java Development Kit.
  1. 安装 JDK 1.8 并记下目标路径,通常是 C:\Program Files\Java\jdk1.8.0_xx
  2. 右键单击 我的电脑并选择属性。
  3. 单击左栏中的高级系统设置链接。
  4. 在选项卡高级中单击环境变量
  5. 创建或修改变量JAVA_HOME
  6. 输入变量值作为 Java Development Kit 的安装路径。

enter image description here

在此处输入图片说明

Linux

Linux

How to set JAVA_HOME in Linux for all users

如何在 Linux 中为所有用户设置 JAVA_HOME

OSX

操作系统

What should I set JAVA_HOME to on OSX

我应该在 OSX 上将 JAVA_HOME 设置为什么

回答by umang shukla

I think you are using windows 7/8/10 with 64bits

我认为您使用的是 64 位的 Windows 7/8/10

just install jdk with x86 versionNo need to reset your environment variable. that will remain same as you have declared.

只需安装 x86 版本的 jdk无需重置环境变量。这将与您声明的一样。

回答by Khalid Taha

Choose the API 23 from the list as picture

从列表中选择 API 23 作为图片

Choose the API 23 from the list as picture

从列表中选择 API 23 作为图片

回答by Jwala Kumar

just add the tag below, in your build.gradel ( app level file), under android{ } tag,

只需添加下面的标签,在你的 build.gradel(应用程序级别文件)中,在 android{} 标签下,

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

like this,

像这样,

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
    applicationId "com.example.code"
    minSdkVersion 21
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

}

}

And now you are good to go.

现在你可以走了。