How to use kapt from the command line (with kotlinc)?

The official documentation gives instructions on using kapt from Gradle and Maven. But how can I use kapt from the command line, kotlinc ?

+5
source share
1 answer

Add tools.jar to the path to the Kotlin compiler classes

As in Kotlin version 1.1.3-2, kotlinc does not add tools.jar to the compiler path. tools.jar requires kapt .

As a workaround, you can install the kotlinc patch.

 vim $KOTLIN_HOME/bin/kotlinc 

Change 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 the JDK, not the JRE.

Note. This is a hack.

Call kotlinc with the correct 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 the desired path
  • /path/to/SomeAnnotationProcessor.jar with the actual path to some annotation processor
  • ./sources , ./classes and ./stubs with path directories in which the corresponding intermediate artifacts should be stored.
  • /path/to/MyKotlinFile.kt with the path to the Kotlin files you want to compile
  • (optional) $KOTLIN_HOME with the path to the Kotlin installation directory (you should already have this in your env)

Note: the -X arguments (advanced options) are non-standard and may be changed or deleted without notice.

Note. The kapt interface kapt 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 material was remodeled using 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 about some things. Feel free to edit this answer.

Thanks to runningcode : https://github.com/facebook/buck/issues/956#issuecomment-309080611

If this was not obvious: this material sucks. JetBrains simply assumed that the CLI did not matter and they made important interfaces undocumented / reserved for internal use.

+5
source

Source: https://habr.com/ru/post/1270018/


All Articles