I have been scratching my head for awhile trying to set up my library to use multi-release jars to use java 9+ features with backup java 8 implementations. However, it is only needed for a module of my project.
My current build.gradle for the module looks like this:
apply plugin: 'java'
apply plugin: 'java-library'
dependencies {
compile project(":common")
compile 'org.ow2.asm:asm:6.2'
compile 'org.ow2.asm:asm-util:6.2'
testCompile "junit:junit:$junit_version"
testCompileOnly "com.google.auto.service:auto-service:$autoservice_version"
java9Implementation files(sourceSets.main.output.classesDirs) { builtBy compileJava }
}
compileJava {
sourceCompatibility = 8
targetCompatibility = 8
options.compilerArgs.addAll(['--release', '8'])
}
compileJava9Java {
sourceCompatibility = 9
targetCompatibility = 9
options.compilerArgs.addAll(['--release', '9'])
}
sourceSets {
java9 {
java {
srcDirs = ['src/main/java9']
}
}
}
jar {
into('META-INF/versions/9') {
from sourceSets.java9.output
}
manifest {
attributes(
"Manifest-Version": "1.0",
"Multi-Release": true
)
}
}
However refreshing my build.gradle in intellij is getting me this error:
Could not find method java9Implementation() for arguments [file collection] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
I should also note this is on gradle 4.8.1.
You're problem is, that your
sourceSet
is defined after thedependencies
block. Put it abovedependencies
and it should work.The reference you used also explains it:
So before you can use 'java9'Implementation, gradle needs to know what 'java9' is supposed to mean.