在命令行运行 KSP

KSP is a Kotlin compiler plugin and needs to run with Kotlin compiler. Download and extract them.

  1. #!/bin/bash
  2. # Kotlin compiler
  3. wget https://github.com/JetBrains/kotlin/releases/download/v1.9.10/kotlin-compiler-1.9.10.zip
  4. unzip kotlin-compiler-1.9.10.zip
  5. # KSP
  6. wget https://github.com/google/ksp/releases/download/1.9.10-1.0.13/artifacts.zip
  7. unzip artifacts.zip

To run KSP with kotlinc, pass the -Xplugin option to kotlinc.

  1. -Xplugin=/path/to/symbol-processing-cmdline-1.9.10-1.0.13.jar

This is different from the symbol-processing-1.9.10-1.0.13.jar, which is designed to be used with kotlin-compiler-embeddable when running with Gradle. The command line kotlinc needs symbol-processing-cmdline-1.9.10-1.0.13.jar.

You’ll also need the API jar.

  1. -Xplugin=/path/to/symbol-processing-api-1.9.10-1.0.13.jar

See the complete example:

  1. #!/bin/bash
  2. KSP_PLUGIN_ID=com.google.devtools.ksp.symbol-processing
  3. KSP_PLUGIN_OPT=plugin:$KSP_PLUGIN_ID
  4. KSP_PLUGIN_JAR=./com/google/devtools/ksp/symbol-processing-cmdline/1.9.10-1.0.13/symbol-processing-cmdline-1.9.10-1.0.13.jar
  5. KSP_API_JAR=./com/google/devtools/ksp/symbol-processing-api/1.9.10-1.0.13/symbol-processing-api-1.9.10-1.0.13.jar
  6. KOTLINC=./kotlinc/bin/kotlinc
  7. AP=/path/to/your-processor.jar
  8. mkdir out
  9. $KOTLINC \
  10. -Xplugin=$KSP_PLUGIN_JAR \
  11. -Xplugin=$KSP_API_JAR \
  12. -Xallow-no-source-files \
  13. -P $KSP_PLUGIN_OPT:apclasspath=$AP \
  14. -P $KSP_PLUGIN_OPT:projectBaseDir=. \
  15. -P $KSP_PLUGIN_OPT:classOutputDir=./out \
  16. -P $KSP_PLUGIN_OPT:javaOutputDir=./out \
  17. -P $KSP_PLUGIN_OPT:kotlinOutputDir=./out \
  18. -P $KSP_PLUGIN_OPT:resourceOutputDir=./out \
  19. -P $KSP_PLUGIN_OPT:kspOutputDir=./out \
  20. -P $KSP_PLUGIN_OPT:cachesDir=./out \
  21. -P $KSP_PLUGIN_OPT:incremental=false \
  22. -P $KSP_PLUGIN_OPT:apoption=key1=value1 \
  23. -P $KSP_PLUGIN_OPT:apoption=key2=value2 \
  24. $*