Java IC compatibility fix for Gradle 2.14+

Added version check to switch between old and new approaches because
Gradle versions before 2.14 have a bug in Java IC.
Added Kotlin classes to Java task input.
Added  annotationsFile into task input to include it into up-to-date check.

Related issues: #KT-16585 Fixed
This commit is contained in:
Jonathan Leitschuh
2017-03-06 14:58:17 -05:00
committed by Sergey Igushkin
parent 9b34e21619
commit cc0ac36fed
4 changed files with 83 additions and 10 deletions
@@ -31,6 +31,7 @@ import org.jetbrains.kotlin.gradle.internal.Kapt3KotlinGradleSubplugin.Companion
import org.jetbrains.kotlin.gradle.internal.initKapt
import org.jetbrains.kotlin.gradle.plugin.android.AndroidGradleWrapper
import org.jetbrains.kotlin.gradle.tasks.*
import org.jetbrains.kotlin.gradle.utils.ParsedGradleVersion
import org.jetbrains.kotlin.incremental.configureMultiProjectIncrementalCompilation
import org.jetbrains.kotlin.incremental.multiproject.ArtifactDifferenceRegistryProviderAndroidWrapper
import java.io.File
@@ -520,19 +521,25 @@ internal open class KotlinAndroidPlugin(
}
private fun configureJavaTask(kotlinTask: KotlinCompile, javaTask: AbstractCompile, logger: Logger) {
// Since we cannot update classpath statically, java not able to detect changes in the classpath after kotlin compiler.
// Therefore this (probably inefficient since java cannot decide "uptodateness" by the list of changed class files, but told
// explicitly being out of date whenever any kotlin files are compiled
// Gradle Java IC in older Gradle versions (before 2.14) cannot check .class directories updates.
// To make it work, reset the up-to-date status of compileJava with this flag.
kotlinTask.anyClassesCompiled = false
javaTask.outputs.upToDateWhen { task ->
val kotlinClassesCompiled = kotlinTask.anyClassesCompiled
if (kotlinClassesCompiled) {
logger.info("Marking $task out of date, because kotlin classes are changed")
val gradleSupportsJavaIcWithClassesDirs = ParsedGradleVersion.parse(javaTask.project.gradle.gradleVersion)
?.let { it >= ParsedGradleVersion(2, 14) } ?: false
if (!gradleSupportsJavaIcWithClassesDirs) {
javaTask.outputs.upToDateWhen { task ->
if (kotlinTask.anyClassesCompiled) {
logger.info("Marking $task out of date, because kotlin classes are changed")
false
} else true
}
!kotlinClassesCompiled
}
// Make Gradle check if the javaTask is up-to-date based on the Kotlin classes
javaTask.inputs.dir(kotlinTask.destinationDir)
// Also, use kapt1 annotations file for up-to-date check since annotation processing is done with javac
kotlinTask.kaptOptions.annotationsFile?.let { javaTask.inputs.file(it) }
javaTask.dependsOn(kotlinTask.name)
/*
* It's important to modify javaTask.classpath only in doFirst,