Skip to content

Commit

Permalink
Fix plugin compatibility with Kotlin Gradle Plugin 1.9.0 release
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Tapchicoma committed Jul 7, 2023
1 parent 6b74668 commit d322cce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
23 changes: 18 additions & 5 deletions src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import org.gradle.api.provider.Provider
import org.gradle.api.tasks.SourceSet
import org.gradle.language.jvm.tasks.ProcessResources
import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

/**
Expand Down Expand Up @@ -347,11 +348,23 @@ class ProtobufPlugin implements Plugin<Project> {
variant.registerJavaGeneratingTask(generateProtoTask.get(), generateProtoTask.get().outputSourceDirectories)

project.plugins.withId("org.jetbrains.kotlin.android") {
project.afterEvaluate {
String compileKotlinTaskName = Utils.getKotlinAndroidCompileTaskName(project, variant.name)
project.tasks.named(compileKotlinTaskName, KotlinCompile) { KotlinCompile task ->
task.dependsOn(generateProtoTask)
task.source(generateProtoTask.get().outputSourceDirectories)
// Checking if Kotlin plugin is a recent one - 1.7.20+
if (it.respondsTo("getPluginVersion")) {
def kotlinExtension = project.extensions.getByType(KotlinAndroidProjectExtension.class)
kotlinExtension.target.compilations.named(variant.name) {
defaultSourceSet.kotlin.srcDir(
project.objects.fileCollection().from(
generateProtoTask.map { it.outputSourceDirectories }
)
)
}
} else {
project.afterEvaluate {
String compileKotlinTaskName = Utils.getKotlinAndroidCompileTaskName(project, variant.name)
project.tasks.named(compileKotlinTaskName, KotlinCompile) { KotlinCompile task ->
task.dependsOn(generateProtoTask)
task.source(generateProtoTask.get().outputSourceDirectories)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import spock.lang.Unroll

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

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

0 comments on commit d322cce

Please sign in to comment.