Skip to content

Instantly share code, notes, and snippets.

@jb08
Last active May 8, 2019 18:10
Show Gist options
  • Save jb08/8f3fe5d93bd49a0f24d312eb87df772e to your computer and use it in GitHub Desktop.
Save jb08/8f3fe5d93bd49a0f24d312eb87df772e to your computer and use it in GitHub Desktop.
gradle cross-compile to an older Java version
// If on JDK 9+, verify project cross-compiles on its 'sourceCompatible' JVM version (see https://github.com/melix/mrjar-gradle/blob/master/jdks.gradle)
if (project.hasProperty('crossCompile')) {
if (JavaVersion.current().java9Compatible) {
project.afterEvaluate {
tasks.withType(JavaCompile) {
def version = compat(sourceCompatibility)
project.logger.info("Configuring $name to use --release $version")
options.compilerArgs.addAll(['--release', version])
}
}
} else {
project.logger.warn("-PcrossCompile not supported prior to JDK 9; using JDK ${JavaVersion.current()}")
}
}
// This function converts 1.8 -> 8
static String compat(String src) {
if (src.contains('.')) {
src.substring(src.lastIndexOf('.')+1)
} else {
src
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment