stevebot
stevebot

Reputation: 24005

How to specify the JDK compile level of a module in Gradle

I have a pure Java module in an Android project. My project compiles by default using JDK 8, but I want my Java module to compile using JDK 7. How do I specify for my pure java module a compile level of 1.7? Keep in mind that I am not wanting to override the project level compile level of 1.8.

Upvotes: 0

Views: 2355

Answers (5)

Ali
Ali

Reputation: 390

You can do this in two ways:

1-Add this block to your app build.gradle

android{
....
  compileOptions {
      sourceCompatibility JavaVersion.VERSION_1_8
      targetCompatibility JavaVersion.VERSION_1_8
  }
}

2-Add this block with android studio:

  • Go to File>Project Structure.
  • In Modules section,select your app (or other modules) module.
  • Change Source compatibility and Target Compatibility values to 1.8.

Update:

Note: If you using kotlin as your Gradle language(kotlin dsl), you should do it this way:

android{
....
  compileOptions {
      setSourceCompatibility(JavaVersion.VERSION_1_8)
      setTargetCompatibility(JavaVersion.VERSION_1_8)
  }
}

Upvotes: 2

b00n12
b00n12

Reputation: 1428

Setting target/source compatibility levels of a java library module in Android Studio can be done adding this to the build.gradle of the java library module:

tasks.withType(JavaCompile) {
    sourceCompatibility = '1.7'
    targetCompatibility = '1.7'
}

Upvotes: 0

Neerajakshi P
Neerajakshi P

Reputation: 1

Two ways

  1. In gradle.properties in your HOME_DIRECTORY set org.gradle.java.home=/path_to_jdk_directory

  2. In your gradle.build

    compileJava.options.fork = true
    compileJava.options.forkOptions.executable = /path_to_javac
    

Upvotes: 0

Lennon Spirlandelli
Lennon Spirlandelli

Reputation: 3193

You can do the following into app/build.gradle:

android {    
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    } 
}

Upvotes: -1

stevebot
stevebot

Reputation: 24005

I found the answer:

//noinspection GroovyUnusedAssignment
sourceCompatibility = 1.7
//noinspection GroovyUnusedAssignment
targetCompatibility = 1.7

You can specify the source and target compatibility levels. This is needed often when compiling a mix of Android and Java modules.

Upvotes: 2

Related Questions