Official documentation instructs how to use kapt from Gradle and Maven. But how can I use kapt from command line, with kotlinc?
- 7,833
- 4
- 41
- 65
-
2If you find nothing about this, you can try to run a Gradle build with the `--debug` log level and find the line that looks like `[KOTLIN] Kotlin compiler args: ...`. Find the one that's related to kapt, not the regular Kotlin compilation, and it will contain the required command line arguments. – hotkey Jul 20 '17 at 14:25
-
@hotkey it seems to a good idea. – holi-java Jul 20 '17 at 14:40
-
@hotkey Yup, that was my approach =) – cubuspl42 Jul 20 '17 at 14:54
1 Answers
Add tools.jar to Kotlin compilers' classpath
As of Kotlin version 1.1.3-2, kotlinc does not add tools.jar to compiler's classpath. tools.jar is required by kapt.
As a workaround, you can patch kotlinc.
vim $KOTLIN_HOME/bin/kotlinc
Edit line 79.
From:
kotlin_app=("${KOTLIN_HOME}/lib/kotlin-preloader.jar" "org.jetbrains.kotlin.preloading.Preloader" "-cp" "${KOTLIN_HOME}/lib/kotlin-compiler.jar" $KOTLIN_COMPILER)
To:
kotlin_app=("${KOTLIN_HOME}/lib/kotlin-preloader.jar" "org.jetbrains.kotlin.preloading.Preloader" "-cp" "${KOTLIN_HOME}/lib/kotlin-compiler.jar:$JAVA_HOME/lib/tools.jar" $KOTLIN_COMPILER)
Note: $JAVA_HOME must point to JDK, not JRE.
Note: This is a hack.
Invoke kotlinc with right arguments
kotlinc -cp $MY_CLASSPATH \
-Xplugin=$KOTLIN_HOME/lib/kotlin-annotation-processing.jar -P \
plugin:org.jetbrains.kotlin.kapt3:aptMode=aptAndStubs,\
plugin:org.jetbrains.kotlin.kapt3:apclasspath=/path/to/SomeAnnotationProcessor.jar,\
plugin:org.jetbrains.kotlin.kapt3:sources=./sources,\
plugin:org.jetbrains.kotlin.kapt3:classes=./classes,\
plugin:org.jetbrains.kotlin.kapt3:stubs=./stubs \
/path/to/MyKotlinFile.kt
Replace:
$MY_CLASSPATHwith your desired classpath/path/to/SomeAnnotationProcessor.jarwith actual path to some annotation processor./sources,./classesand./stubswith paths do directories where respective intermediate artifacts should be stored/path/to/MyKotlinFile.ktwith path to the Kotlin file(s) you want to compile- (optionally)
$KOTLIN_HOMEwith the path to Kotlin's installation directory (you should already have this in your env)
Note: -X arguments (advanced options) are non-standard and may be changed or removed without any notice
Note: kapt's interface is undocumented. You can check the source code: https://github.com/JetBrains/kotlin/blob/master/plugins/kapt3/src/org/jetbrains/kotlin/kapt3/Kapt3Plugin.kt#L295
This stuff was reverse-engineered from running gradle build --debug in kotlin-examples/gradle/kotlin-dagger (https://github.com/JetBrains/kotlin-examples/tree/master/gradle/kotlin-dagger).
This is just a starting point. I'm still not sure of a few things. Feel free to edit this answer.
Thanks to runningcode: https://github.com/facebook/buck/issues/956#issuecomment-309080611
If it wasn't obvious: this stuff sucks. JetBrains just assumed that CLI doesn't matter and they made the crucial interfaces undocumented / reserved for internal use.
- 7,833
- 4
- 41
- 65