简体   繁体   English

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

[英]Run Android Studio with JRE 1.8 or later version

I am facing issue for Android N layout XML preview.我正面临 Android N 布局 XML 预览的问题。 it is showing following message :它显示以下消息:

"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.?我怎样才能改变它。?

这是 help->about JRE:1.7 时的图像

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查看附加的屏幕截图了解更多

在此处输入图片说明

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 {
...
    jackOptions {
      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.运行您的 Android N 模拟器现在应该可以工作了。 Regardless of whether Android Studio itself states it's using 1.7 or 1.8.无论 Android Studio 本身是否声明它使用的是 1.7 或 1.8。


Follow the guidelines that are provided on the Android site:请遵循 Android 站点上提供的准则:

Set up for Android N 为 Android N 设置

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

在此处输入图片说明

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 设置为什么

As it is mentioned in the documentation you can add the STUDIO_JDK env var to configure the IDE JDK.正如文档中提到的,您可以添加 STUDIO_JDK env var 来配置 IDE JDK。 In order to access it from your GUI, you can add this line to your ~/.profile file :为了从您的 GUI 访问它,您可以将此行添加到您的 ~/.profile 文件中:

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

I think you are using windows 7/8/10 with 64bits我认为您使用的是 64 位的 Windows 7/8/10

just install jdk with x86 version No need to reset your environment variable.只需安装 x86 版本的 jdk无需重置环境变量。 that will remain same as you have declared.这将与您声明的一样。

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

Choose the API 23 from the list as picture从列表中选择 API 23 作为图片

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.现在你可以走了。

As Krishna Meena is mentioning you should change the SDK/JDK Location settings found from within:正如 Krishna Meena 提到的,您应该更改从内部找到的 SDK/JDK 位置设置:
File -> Project Structure... then SDK Location and now press on the link Gradle Settings...文件 -> 项目结构...然后是 SDK 位置,现在点击链接 Gradle 设置...

在此处输入图片说明

Then choose your Gradle JDK to use...然后选择要使用的 Gradle JDK...

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM