merinoff
merinoff

Reputation: 341

Code for different Java versions with Gradle

Can I possibly make Gradle compile one part of my app for Java version 1.8 and the other for 1.7? My situation is I'm writing a library, part of which will be used in a project where the version is 1.7.

I realize now that I could have broken logic in my thoughts, but the question still stands. Or, if possible, suggest something completely different.

EDIT: And if possible suggest any relevant terms, because I can't even think of a google query now.

Upvotes: 1

Views: 297

Answers (1)

kaushik
kaushik

Reputation: 2502

Suppose you have a multi project build with the following directory structure:

  • root
    • build.gradle.kts
    • sub-project
      • build.gradle.kts
      • src/main/java
    • java-1.7-project
      • build.gradle.kts -src/main/java

Root project build file:

plugins {
  java
}

allprojects {
    group = "com.company.example"
    version = "0.0.1"

    apply {
        plugin("java")
    }

    repositories {
        mavenCentral()
    }
}

configure(subprojects.filter { it.name != "java-1.7" }) {
   java.sourceCompatibility = org.gradle.api.JavaVersion.VERSION_11
}

Java-1.7-project build file:

configure<JavaPluginExtension> {
    sourceCompatibility = JavaVersion.VERSION_1_7
    targetCompatibility = JavaVersion.VERSION_1_7
}

Upvotes: 2

Related Questions