Kapt3: Support Kotlin sources generation (KT-14070)
This commit is contained in:
+12
@@ -318,4 +318,16 @@ class Kapt3IT : BaseGradleIT() {
|
||||
assert(kaptArgs.contains(arg)) { "Kapt compiler arguments should contain '$arg'" }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOutputKotlinCode() {
|
||||
Project("kaptOutputKotlinCode", GRADLE_VERSION, directoryPrefix = "kapt2").build("build") {
|
||||
assertSuccessful()
|
||||
assertKaptSuccessful()
|
||||
assertFileExists("build/generated/source/kapt/main/example/TestClassCustomized.java")
|
||||
assertFileExists("build/generated/source/kaptKotlin/main/TestClass.kt")
|
||||
assertFileExists("build/classes/main/example/TestClass.class")
|
||||
assertFileExists("build/classes/main/example/TestClassCustomized.class")
|
||||
}
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenLocal()
|
||||
}
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'kotlin-kapt'
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
compile "org.jetbrains.kotlin:annotation-processor-example:$kotlin_version"
|
||||
kapt "org.jetbrains.kotlin:annotation-processor-example:$kotlin_version"
|
||||
}
|
||||
|
||||
kapt {
|
||||
generateStubs = true
|
||||
arguments {
|
||||
arg("suffix", "Customized")
|
||||
arg("generate.kotlin.code", "true")
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package example
|
||||
|
||||
@example.ExampleAnnotation
|
||||
public class TestClass {
|
||||
|
||||
fun test() {
|
||||
println(this.customToString())
|
||||
}
|
||||
|
||||
}
|
||||
+17
-7
@@ -29,6 +29,7 @@ import org.gradle.api.tasks.compile.JavaCompile
|
||||
import org.jetbrains.kotlin.gradle.plugin.*
|
||||
import org.jetbrains.kotlin.gradle.plugin.android.AndroidGradleWrapper
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
import org.jetbrains.kotlin.preprocessor.mkdirsOrFail
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
import java.io.ObjectOutputStream
|
||||
@@ -75,20 +76,28 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
||||
return File(project.project.buildDir, "generated/source/kapt/$sourceSetName")
|
||||
}
|
||||
|
||||
fun getKaptGeneratedDirForKotlin(project: Project, sourceSetName: String): File {
|
||||
return File(project.project.buildDir, "generated/source/kaptKotlin/$sourceSetName")
|
||||
}
|
||||
|
||||
fun getKaptStubsDir(project: Project, sourceSetName: String): File {
|
||||
val dir = File(project.project.buildDir, "tmp/kapt3/stubs/$sourceSetName")
|
||||
dir.mkdirs()
|
||||
return dir
|
||||
}
|
||||
|
||||
private class Kapt3SubpluginContext(
|
||||
private inner class Kapt3SubpluginContext(
|
||||
val project: Project,
|
||||
val kotlinCompile: KotlinCompile,
|
||||
val javaCompile: AbstractCompile,
|
||||
val variantData: Any?,
|
||||
val sourceSetName: String,
|
||||
val kaptExtension: KaptExtension,
|
||||
val kaptClasspath: MutableList<File>)
|
||||
val kaptClasspath: MutableList<File>) {
|
||||
val sourcesOutputDir = getKaptGeneratedDir(project, sourceSetName)
|
||||
val kotlinSourcesOutputDir = getKaptGeneratedDirForKotlin(project, sourceSetName)
|
||||
val classesOutputDir = getKaptClasssesDir(project, sourceSetName)
|
||||
}
|
||||
|
||||
override fun apply(
|
||||
project: Project,
|
||||
@@ -173,7 +182,11 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
||||
else
|
||||
emptyMap()
|
||||
|
||||
val apOptions = kaptExtension.getAdditionalArguments(project, variantData, androidPlugin) + androidOptions
|
||||
kotlinSourcesOutputDir.mkdirs()
|
||||
|
||||
val apOptions = kaptExtension.getAdditionalArguments(project, variantData, androidPlugin) +
|
||||
androidOptions +
|
||||
mapOf("kapt.kotlin.generated" to kotlinSourcesOutputDir.absolutePath)
|
||||
|
||||
pluginOptions += SubpluginOption("apoptions", encodeAnnotationProcessingOptions(apOptions))
|
||||
|
||||
@@ -211,9 +224,6 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
||||
}
|
||||
|
||||
private fun Kapt3SubpluginContext.createKaptKotlinTask() {
|
||||
val sourcesOutputDir = getKaptGeneratedDir(project, sourceSetName)
|
||||
val classesOutputDir = getKaptClasssesDir(project, sourceSetName)
|
||||
|
||||
// Replace compile*Kotlin to kapt*Kotlin
|
||||
assert(kotlinCompile.name.startsWith("compile"))
|
||||
val kaptTaskName = kotlinCompile.name.replaceFirst("compile", "kapt")
|
||||
@@ -232,7 +242,7 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
||||
kotlinCompile.dependsOn(kaptTask)
|
||||
|
||||
// Add generated source dir as a source root for kotlinCompile and javaCompile
|
||||
kotlinCompile.source(sourcesOutputDir)
|
||||
kotlinCompile.source(sourcesOutputDir, kotlinSourcesOutputDir)
|
||||
javaCompile.source(sourcesOutputDir)
|
||||
|
||||
val pluginOptions = kaptTask.pluginOptions
|
||||
|
||||
Reference in New Issue
Block a user