Eric
Eric

Reputation: 488

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

Upvotes: 3

Views: 9337

Answers (2)

ceph3us
ceph3us

Reputation: 7474

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

Upvotes: 8

Meysam Keshvari
Meysam Keshvari

Reputation: 1201

android {
    compileSdkVersion 28
    buildToolsVersion = '28.0.3'
    defaultConfig {
        applicationId "your package name"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.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
    }
}

Upvotes: 3

Related Questions