Skip to content

Commit a7c26cb

Browse files
Fix plugin compatibility with Kotlin Gradle Plugin 1.9.0 release (#715) (#721)
Kotlin Gradle Plugin 1.9.0 release has changed how kapt tasks are configured: https://youtrack.jetbrains.com/issue/KT-54468/KAPT-Gradle-plugin-causes-eager-task-creation. Because of this change, generated proto sources are not passed to kaptGenerateStubs task leading to compilation error even in default setup. If the Kotlin plugin version is 1.7.20 - protobuf plugin now, instead of directly configuring KotlinCompile task inputs, configures related KotlinSourceSet. This way additional sources are passed both for KotlinCompile and KaptGenerateStubs tasks. Fixes issue #714 Co-authored-by: Yahor Berdnikau <[email protected]>
1 parent 5c15781 commit a7c26cb

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy

+14-5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import org.gradle.api.provider.Provider
5757
import org.gradle.api.tasks.SourceSet
5858
import org.gradle.language.jvm.tasks.ProcessResources
5959
import org.gradle.util.GradleVersion
60+
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
6061
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
6162

6263
/**
@@ -347,11 +348,19 @@ class ProtobufPlugin implements Plugin<Project> {
347348
variant.registerJavaGeneratingTask(generateProtoTask.get(), generateProtoTask.get().outputSourceDirectories)
348349

349350
project.plugins.withId("org.jetbrains.kotlin.android") {
350-
project.afterEvaluate {
351-
String compileKotlinTaskName = Utils.getKotlinAndroidCompileTaskName(project, variant.name)
352-
project.tasks.named(compileKotlinTaskName, KotlinCompile) { KotlinCompile task ->
353-
task.dependsOn(generateProtoTask)
354-
task.source(generateProtoTask.get().outputSourceDirectories)
351+
// Checking if Kotlin plugin is a recent one - 1.7.20+
352+
if (it.respondsTo("getPluginVersion")) {
353+
KotlinAndroidProjectExtension kotlinExtension = project.extensions.getByType(KotlinAndroidProjectExtension)
354+
kotlinExtension.target.compilations.named(variant.name) {
355+
it.defaultSourceSet.kotlin.srcDirs(variantSourceSet.output)
356+
}
357+
} else {
358+
project.afterEvaluate {
359+
String compileKotlinTaskName = Utils.getKotlinAndroidCompileTaskName(project, variant.name)
360+
project.tasks.named(compileKotlinTaskName, KotlinCompile) { KotlinCompile task ->
361+
task.dependsOn(generateProtoTask)
362+
task.source(generateProtoTask.get().outputSourceDirectories)
363+
}
355364
}
356365
}
357366
}

src/test/groovy/com/google/protobuf/gradle/ProtobufAndroidPluginKotlinTest.groovy

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import spock.lang.Unroll
88

99
@CompileDynamic
1010
class ProtobufAndroidPluginKotlinTest extends Specification {
11-
private static final List<String> GRADLE_VERSION = ["5.6", "6.5.1", "7.4.2", "7.6"]
12-
private static final List<String> ANDROID_PLUGIN_VERSION = ["3.5.0", "4.1.0", "7.2.1", "7.3.1"]
13-
private static final List<String> KOTLIN_VERSION = ["1.3.20", "1.3.20", "1.3.40", "1.7.20"]
11+
private static final List<String> GRADLE_VERSION = ["5.6", "6.5.1", "7.4.2", "7.6.2", "7.6.2"]
12+
private static final List<String> ANDROID_PLUGIN_VERSION = ["3.5.0", "4.1.0", "7.2.1", "7.3.1", "7.4.2"]
13+
private static final List<String> KOTLIN_VERSION = ["1.3.20", "1.3.20", "1.3.40", "1.7.20", "1.9.0"]
1414

1515
/**
1616
* This test may take a significant amount of Gradle daemon Metaspace memory in some

0 commit comments

Comments
 (0)