Skip to content

Commit 060095d

Browse files
committed
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.
1 parent 2506bb6 commit 060095d

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPlugin.kt

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ internal class Kotlin2JvmSourceSetProcessor(
137137
val kaptManager = AnnotationProcessingManager(kotlinTask, javaTask, sourceSetName,
138138
aptConfiguration.resolve(), aptOutputDir, aptWorkingDir)
139139

140-
kotlinAfterJavaTask = project.initKapt(kotlinTask, javaTask, kaptManager, sourceSetName, null, subpluginEnvironment, tasksProvider)
140+
kotlinAfterJavaTask = project.initKapt(kotlinTask, javaTask, kaptManager,
141+
sourceSetName, null, subpluginEnvironment, tasksProvider)
142+
} else {
143+
removeAnnotationProcessingPluginClasspathEntry(kotlinTask)
141144
}
142145

143146
sourceSet.java.srcDirs.forEach { kotlinSourceSet.kotlin.srcDir(it) }
@@ -398,19 +401,27 @@ internal open class KotlinAndroidPlugin(
398401
kotlinTask.description = "Compiles the $variantDataName kotlin."
399402
kotlinTask.setDependsOn(javaTask.dependsOn)
400403

401-
val isKapt2Enabled = Kapt3GradleSubplugin.isEnabled(project)
404+
val isKapt3Enabled = Kapt3GradleSubplugin.isEnabled(project)
402405

403406
val aptFiles = arrayListOf<File>()
404407

405-
if (!isKapt2Enabled) {
408+
if (!isKapt3Enabled) {
409+
var hasAnyKaptDependency: Boolean = false
406410
for (provider in variantData.sourceProviders) {
407411
val aptConfiguration = aptConfigurations[(provider as AndroidSourceSet).name]
408412
// Ignore if there's only an annotation processor wrapper in dependencies (added by default)
409413
if (aptConfiguration != null && aptConfiguration.allDependencies.size > 1) {
410414
javaTask.dependsOn(aptConfiguration.buildDependencies)
411415
aptFiles.addAll(aptConfiguration.resolve())
416+
hasAnyKaptDependency = true
412417
}
413418
}
419+
420+
if (!hasAnyKaptDependency) {
421+
removeAnnotationProcessingPluginClasspathEntry(kotlinTask)
422+
}
423+
} else {
424+
removeAnnotationProcessingPluginClasspathEntry(kotlinTask)
414425
}
415426

416427
val appliedPlugins = subpluginEnvironment.addSubpluginOptions(
@@ -422,7 +433,7 @@ internal open class KotlinAndroidPlugin(
422433

423434
var kotlinAfterJavaTask: KotlinCompile? = null
424435

425-
if (javaTask is JavaCompile && aptFiles.isNotEmpty() && !isKapt2Enabled) {
436+
if (javaTask is JavaCompile && aptFiles.isNotEmpty() && !isKapt3Enabled) {
426437
val (aptOutputDir, aptWorkingDir) = project.getAptDirsForSourceSet(variantDataName)
427438

428439
variantData.addJavaSourceFoldersToModel(aptOutputDir)
@@ -564,6 +575,18 @@ private fun createSyncOutputTask(
564575
project.logger.kotlinDebug { "Created task ${syncTask.path} to copy kotlin classes from $kotlinDir to $javaDir" }
565576
}
566577

578+
private val KOTLIN_ANNOTATION_PROCESSING_FILE_REGEX = "kotlin-annotation-processing-[\\-0-9A-Za-z.]+\\.jar".toRegex()
579+
580+
private fun removeAnnotationProcessingPluginClasspathEntry(kotlinCompile: KotlinCompile) {
581+
kotlinCompile.pluginOptions.classpath
582+
.map(::File)
583+
.filter { it.name.matches(KOTLIN_ANNOTATION_PROCESSING_FILE_REGEX) }
584+
.forEach {
585+
kotlinCompile.logger.kotlinDebug("Removing plugin classpath dependency $it")
586+
kotlinCompile.pluginOptions.removeClasspathEntry(it)
587+
}
588+
}
589+
567590
private fun loadSubplugins(project: Project): SubpluginEnvironment {
568591
try {
569592
val subplugins = ServiceLoader.load(KotlinGradleSubplugin::class.java, project.buildscript.classLoader)

libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/CompilerPluginOptions.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ internal class CompilerPluginOptions {
3434
mutableClasspath.add(file.canonicalPath)
3535
}
3636

37+
internal fun removeClasspathEntry(file: File) {
38+
mutableClasspath.remove(file.canonicalPath)
39+
}
40+
3741
fun addPluginArgument(pluginId: String, key: String, value: String) {
3842
mutableArguments.add("plugin:$pluginId:$key=$value")
3943
}

0 commit comments

Comments
 (0)