13

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

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

also i install JAVA 1.8 in my OS.

also change the project JDK location with Java 1.8.

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

how can i change it.?

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

thahgr
  • 718
  • 1
  • 10
  • 27
Prashant Jajal
  • 3,469
  • 5
  • 24
  • 38

8 Answers8

23

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

See the attached screenshot for more

enter image description here

Krishna Meena
  • 5,693
  • 5
  • 32
  • 44
  • 1
    This solution was the only working one for me. Thank you ! :-) – Loenix Oct 10 '16 at 13:59
  • 1
    Great solution, I was battling different system Java versions, and it is quite an elegant solution to point to the JDK that ships with Android Studio. – Booger Jan 29 '20 at 00:38
11

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.

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

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

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

Check that the JDK Location is pointing to 1.8 and not 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.


Follow the guidelines that are provided on the Android site:

Set up for Android N

TejjD
  • 2,571
  • 1
  • 17
  • 37
  • android N emulator i can run. but the problem is that, when i see the layout xml file preview then it show "Android N requires the IDE to be running with Java 1.8 or later" so i want to run android studio with jre 1.8 – Prashant Jajal Apr 15 '16 at 05:48
  • Follow the guidelines I provided. Ensure you have set your JDK location correctly – TejjD Apr 15 '16 at 05:50
  • Check that your default JDK is the Oracle JDK and not OpenJDK. Many distributions ship with OpenJDK by default due to licensing issues and even if you install the Oracle JDK it might not then be the default. Running java -version from a terminal should tell you which is the default. – TejjD Apr 15 '16 at 05:54
  • Remove all versions of Java that you have installed, and only install Java 8 - http://www.linuxdeveloper.space/install-android-studio-oracle-jdk/ – TejjD Apr 15 '16 at 06:04
9

You have to set JAVA_HOME in your operating system.

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 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.
  6. Enter the variable value as the installation path for the Java Development Kit.

enter image description here

Linux

How to set JAVA_HOME in Linux for all users

OSX

What should I set JAVA_HOME to on OSX

Community
  • 1
  • 1
Caolem
  • 124
  • 1
  • 4
2

As Krishna Meena is mentioning you should change the SDK/JDK Location settings found from within:
File -> Project Structure... then SDK Location and now press on the link Gradle Settings...

enter image description here

Then choose your Gradle JDK to use...

enter image description here

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
1

As it is mentioned in the documentation you 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 :

launchctl setenv STUDIO_JDK /Library/Java/JavaVirtualMachines/jdk<version>.jdk
akofman
  • 339
  • 2
  • 10
1

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

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

1

Choose the API 23 from the list as picture

Choose the API 23 from the list as picture

Khalid Taha
  • 3,183
  • 5
  • 27
  • 43
0

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

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.

Jwala Kumar
  • 525
  • 7
  • 9