Gradle kotlin plugin: passing sources into the KotlinCompile task


#1

Hi,
I work on a plugin that compiles .proto files to .java and would like to integrate with the kotlin plugin. I want the outputted java files to be included by the KotlinCompile task as inputs. It seems that for non android kotlin projects, I can pass a File or SourceDirectorySet to the task’s source like so, and it works. But the analogous thing does not work on android.

project.sourceSets.each { SourceSet sourceSet ->
project.protobuf.generateProtoTasks.ofSourceSet(sourceSet.name).each { GenerateProtoTask genProto ->
  ['java', 'kotlin'].each { String lang ->
    Task compileTask = project.tasks.findByName(sourceSet.getCompileTaskName(lang))
    if (compileTask != null) {
      compileTask.dependsOn(genProto)
      genProto.getAllOutputDirs().each { dir ->
        SourceDirectorySet generatedDirSet = new DefaultSourceDirectorySet(dir.toString(), fileResolver, new DefaultDirectoryFileTreeFactory())
        generatedDirSet.srcDirs(dir)
        generatedDirSet.include("**/*.java")
        compileTask.source generatedDirSet
      }
    }
  }
}

The beginning of linkGenerateProtoTasksToJavaCompile is the code for handling android projects:

When I build testProjectAndroid by running ProtobufAndroidPluginTest, I get an error that KotlinBar.kt failed to resolve the HelloWorld class. If I set a breakpoint on line 445, I see that the KotlinCompile task does have HelloWorld.java in its sources by adding a breakpoint to the kotlin tasks’s doBefore{}.

The mutableSourceRoots field:

The source field:

Anyone know why KotlinCompile gives this different behavior for android projects?


#2

Turns out this is a mistake in my test code, there was a typo in my import statement (HelloWorld should be Helloworld).

For anyone who stumbles upon this thread trying to debug the gradle plugin, I have found it helpful to System.setProperty("kotlin.compiler.execution.strategy", "out-of-process"). This makes it possible to set a breakpoint in this method and see exactly which arguments were passed to the kotlin compiler: