Remove kotlin-annotation-processing artifact from the serialized compiler options. It is unused in kapt3 (or if no annotation processor dependencies are provided) but is still imported into the IDEA module files. This leads to compilation error due to the plugin incompatibility.

Incompatibility reason: kotlin-annotation-processing has references to the shaded intellij-core, so, being provided to the unshaded kotlinc from the Kotlin plugin, it throws the AbstractMethodError.

This fixes #KT-16184.
This commit is contained in:
Yan Zhulanow
2017-02-22 16:00:34 +03:00
committed by Yan Zhulanow
parent 2506bb6673
commit 060095d39f
2 changed files with 31 additions and 4 deletions
@@ -137,7 +137,10 @@ internal class Kotlin2JvmSourceSetProcessor(
val kaptManager = AnnotationProcessingManager(kotlinTask, javaTask, sourceSetName,
aptConfiguration.resolve(), aptOutputDir, aptWorkingDir)
kotlinAfterJavaTask = project.initKapt(kotlinTask, javaTask, kaptManager, sourceSetName, null, subpluginEnvironment, tasksProvider)
kotlinAfterJavaTask = project.initKapt(kotlinTask, javaTask, kaptManager,
sourceSetName, null, subpluginEnvironment, tasksProvider)
} else {
removeAnnotationProcessingPluginClasspathEntry(kotlinTask)
}
sourceSet.java.srcDirs.forEach { kotlinSourceSet.kotlin.srcDir(it) }
@@ -398,19 +401,27 @@ internal open class KotlinAndroidPlugin(
kotlinTask.description = "Compiles the $variantDataName kotlin."
kotlinTask.setDependsOn(javaTask.dependsOn)
val isKapt2Enabled = Kapt3GradleSubplugin.isEnabled(project)
val isKapt3Enabled = Kapt3GradleSubplugin.isEnabled(project)
val aptFiles = arrayListOf<File>()
if (!isKapt2Enabled) {
if (!isKapt3Enabled) {
var hasAnyKaptDependency: Boolean = false
for (provider in variantData.sourceProviders) {
val aptConfiguration = aptConfigurations[(provider as AndroidSourceSet).name]
// Ignore if there's only an annotation processor wrapper in dependencies (added by default)
if (aptConfiguration != null && aptConfiguration.allDependencies.size > 1) {
javaTask.dependsOn(aptConfiguration.buildDependencies)
aptFiles.addAll(aptConfiguration.resolve())
hasAnyKaptDependency = true
}
}
if (!hasAnyKaptDependency) {
removeAnnotationProcessingPluginClasspathEntry(kotlinTask)
}
} else {
removeAnnotationProcessingPluginClasspathEntry(kotlinTask)
}
val appliedPlugins = subpluginEnvironment.addSubpluginOptions(
@@ -422,7 +433,7 @@ internal open class KotlinAndroidPlugin(
var kotlinAfterJavaTask: KotlinCompile? = null
if (javaTask is JavaCompile && aptFiles.isNotEmpty() && !isKapt2Enabled) {
if (javaTask is JavaCompile && aptFiles.isNotEmpty() && !isKapt3Enabled) {
val (aptOutputDir, aptWorkingDir) = project.getAptDirsForSourceSet(variantDataName)
variantData.addJavaSourceFoldersToModel(aptOutputDir)
@@ -564,6 +575,18 @@ private fun createSyncOutputTask(
project.logger.kotlinDebug { "Created task ${syncTask.path} to copy kotlin classes from $kotlinDir to $javaDir" }
}
private val KOTLIN_ANNOTATION_PROCESSING_FILE_REGEX = "kotlin-annotation-processing-[\\-0-9A-Za-z.]+\\.jar".toRegex()
private fun removeAnnotationProcessingPluginClasspathEntry(kotlinCompile: KotlinCompile) {
kotlinCompile.pluginOptions.classpath
.map(::File)
.filter { it.name.matches(KOTLIN_ANNOTATION_PROCESSING_FILE_REGEX) }
.forEach {
kotlinCompile.logger.kotlinDebug("Removing plugin classpath dependency $it")
kotlinCompile.pluginOptions.removeClasspathEntry(it)
}
}
private fun loadSubplugins(project: Project): SubpluginEnvironment {
try {
val subplugins = ServiceLoader.load(KotlinGradleSubplugin::class.java, project.buildscript.classLoader)
@@ -34,6 +34,10 @@ internal class CompilerPluginOptions {
mutableClasspath.add(file.canonicalPath)
}
internal fun removeClasspathEntry(file: File) {
mutableClasspath.remove(file.canonicalPath)
}
fun addPluginArgument(pluginId: String, key: String, value: String) {
mutableArguments.add("plugin:$pluginId:$key=$value")
}