Search code examples
android-studiojava-8

Android Studio compile project with Java 8?


I'm trying to compile a project using java 8.

I've set the correct jdk as shown in the image:

JDK location

Also changed the target version:

Target version

But I still got the error: compileSdkVersion 'android-24' requires JDK 1.8 or later to compile.

Where am I wrong?

EDIT: I'm on Ubuntu 12.04


Solution

  • When goes to JDK setting you can:

    add in "android app" module build.gradle

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    

    setup sdk path in gradle.properties

    org.gradle.java.home=/path_to_java_sdk_8/jdk1.8
    

    check also .idea/modules/compiler.xml against 1.7/1.8

    <?xml version="1.0" encoding="UTF-8"?>
    <project version="4">
      <component name="CompilerConfiguration">
          ...
        <bytecodeTargetLevel target="1.8">
          <module name="app" target="1.7" />
        </bytecodeTargetLevel>
      </component>
    

    and misc.xml

    <?xml version="1.0" encoding="UTF-8"?>
      <project version="4">
        ....
       <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" 
                  default="false" assert-keyword="true" 
                  jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
         <output url="file://$PROJECT_DIR$/build/classes" />
      </component>
      <component name="ProjectType">
        <option name="id" value="Android" />
      </component>
     </project>
    

    BTW: if you want compile against java 1.8 you need to use "jack" see https://source.android.com/source/jack.html