How to use kapt from command line (with kotlinc)?
Asked Answered
S

1

7

Official documentation instructs how to use kapt from Gradle and Maven. But how can I use kapt from command line, with kotlinc?

Shipley answered 20/7, 2017 at 14:19 Comment(3)
If 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.Gudgeon
@Gudgeon it seems to a good idea.Unfamiliar
@Gudgeon Yup, that was my approach =)Shipley
S
9

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_CLASSPATH with your desired classpath
  • /path/to/SomeAnnotationProcessor.jar with actual path to some annotation processor
  • ./sources, ./classes and ./stubs with paths do directories where respective intermediate artifacts should be stored
  • /path/to/MyKotlinFile.kt with path to the Kotlin file(s) you want to compile
  • (optionally) $KOTLIN_HOME with 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.

Shipley answered 20/7, 2017 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.