### Run KSP2 JVM Main from Command Line Source: https://github.com/google/ksp/blob/main/docs/ksp2cmdline.md Example of executing KSPJvmMain with various required and optional arguments. Ensure all JARs are correctly listed in the classpath. ```bash java -cp \ symbol-processing-aa-2.3.7.jar:kotlin-analysis-api-2.3.7.jar:common-deps-2.3.7.jar:symbol-processing-api-2.3.7.jar:kotlin-stdlib-2.3.20.jar:kotlinx-coroutines-core-jvm-1.10.2.jar \ com.google.devtools.ksp.cmdline.KSPJvmMain \ -jvm-target 11 \ -module-name=main \ -source-roots project_dir/src/kotlin/main \ -project-base-dir project_dir/ \ -output-base-dir=project_dir/build/ \ -caches-dir=project_dir/build/caches/ \ -class-output-dir=project_dir/build/out/main/classes \ -kotlin-output-dir=project_dir/build/out/main/kotlin/ \ -java-output-dir project_dir/build/out/main/java/ \ -resource-output-dir project_dir/build/out/main/res/ \ -language-version=2.0 \ -api-version=2.0 \ path/to/processor.jar ``` -------------------------------- ### KSP2: Multi-override scenario example Source: https://github.com/google/ksp/blob/main/docs/ksp2api.md This code demonstrates a multi-override scenario. KSP2 uses a DFS order for finding overridden symbols, prioritizing recursive super type lookups within the direct super type list. This contrasts with KSP1's BFS order. ```kotlin interface GrandBaseInterface1 { fun foo(): Unit } interface GrandBaseInterface2 { fun foo(): Unit } interface BaseInterface1 : GrandBaseInterface1 { } interface BaseInterface2 : GrandBaseInterface2 { } class OverrideOrder1 : BaseInterface1, GrandBaseInterface2 { override fun foo() = TODO() } class OverrideOrder2 : BaseInterface2, GrandBaseInterface1 { override fun foo() = TODO() } ``` -------------------------------- ### Defer Invalid Symbols in KSP Processor Source: https://github.com/google/ksp/wiki/KSP-multiple-round-preview-guide Modify your KSP processor's `process` function to return deferred symbols and use `validate()` to filter them. This example shows how to defer invalid symbols and process valid ones. ```kotlin override fun process(resolver: Resolver): List { val symbols = resolver.getSymbolsWithAnnotation("com.example.annotation.Builder") val ret = symbols.filter { !it.validate() } symbols .filter { it is KSClassDeclaration && it.validate() } .map { it.accept(BuilderVisitor(), Unit) } return ret } ``` -------------------------------- ### KSP2 Command Line Options Help Source: https://github.com/google/ksp/blob/main/docs/ksp2cmdline.md Lists available command-line options for KSP2, indicating required arguments with an asterisk (*). Supports list arguments separated by colons and map arguments in key=value format. ```text Available options: -java-source-roots=List * -java-output-dir=File -jdk-home=File? * -jvm-target=String -jvm-default-mode=String * -module-name=String * -source-roots=List -common-source-roots=List -libraries=List -friends=List -processor-options=Map * -project-base-dir=File * -output-base-dir=File * -caches-dir=File * -class-output-dir=File * -kotlin-output-dir=File * -resource-output-dir=File -incremental=Boolean -incremental-log=Boolean -modified-sources=List -removed-sources=List -changed-classes=List * -language-version=String * -api-version=String -all-warnings-as-errors=Boolean -map-annotation-arguments-in-java=Boolean * where: * is required List is colon separated. E.g., arg1:arg2:arg3 Map is in the form key1=value1:key2=value2 ``` -------------------------------- ### Execute KSP2 Programmatically Source: https://github.com/google/ksp/blob/main/docs/ksp2entrypoints.md This snippet demonstrates the four main steps to execute KSP2 programmatically: initializing a logger, loading symbol processor providers, configuring KSP, and running the execution. Use `KspGradleLogger` for logging and ensure all configurations are set in `KSPJvmConfig.Builder`. ```kotlin val logger = KspGradleLogger(KspGradleLogger.LOGGING_LEVEL_WARN) val processorClassloader = URLClassLoader(classpath.map { File(it).toURI().toURL() }.toTypedArray()) val processorProviders = ServiceLoader.load( processorClassloader.loadClass("com.google.devtools.ksp.processing.SymbolProcessorProvider"), processorClassloader ).toList() as List val kspConfig = KSPJvmConfig.Builder().apply { // All configurations happen here. See KSPConfig.kt for all available options. moduleName = "main" sourceRoots = listOf(File("/path/to/src1"), File("/path/to/src2")) kotlinOutputDir = File("/path/to/kotlin/out") // ... }.build() val exitCode = KotlinSymbolProcessing(kspConfig, processorProviders, kspLoggerImpl).execute() ``` -------------------------------- ### Main function annotated with HelloWorld Source: https://github.com/google/ksp/blob/main/examples/hello-world/README.md The main function in the app module, annotated with HelloWorld to trigger the KSP processor during compilation. ```kotlin package com.example.app import com.example.annotations.HelloWorld fun main() { sayHello() } @HelloWorld fun greet() { println("This is the greet function.") } ``` -------------------------------- ### Debug KSP2 with Gradle Source: https://github.com/google/ksp/blob/main/docs/ksp2.md Pass this system property in the Gradle command line to enable debugging for KSP2 and/or processors. This is required because KSP2 runs in the Gradle daemon. ```bash -Dorg.gradle.debug=true ``` -------------------------------- ### App Build Configuration (build.gradle.kts) Source: https://github.com/google/ksp/blob/main/examples/hello-world/README.md Configures the app module's build script. It includes dependencies for the annotations and the KSP processor. ```gradle-kts dependencies { implementation(project(":annotations")) ksp(project(":processor")) } ``` -------------------------------- ### KSP Processor Entry Point Source: https://github.com/google/ksp/blob/main/examples/hello-world/README.md The main entry point for the KSP processor. It uses the Resolver to find symbols annotated with HelloWorld and then applies a visitor pattern to process them. ```kotlin package com.example.processor import com.example.annotations.HelloWorld import com.google.devtools.ksp.processing.* import com.google.devtools.ksp.symbol.* import com.google.devtools.ksp.validate class HelloWorldProcessor( private val codeGenerator: CodeGenerator, private val logger: KSPLogger, ) : SymbolProcessor { override fun process(resolver: Resolver): List { val symbols = resolver.getSymbolsWithAnnotation(HelloWorld::class.java.name) var results = emptyList() symbols.filterIsInstance().forEach { function -> function.accept(HelloWorldVisitor(codeGenerator, logger), Unit) results = results + function } results } override fun finish() = Unit override fun onError() = Unit } ``` -------------------------------- ### Enable KSP2 Beta in Gradle Source: https://github.com/google/ksp/blob/main/docs/ksp2.md Add this property to your gradle.properties file to enable KSP2 Beta. This flag is supported with KSP 1.0.21 and above. ```properties ksp.useKSP2=true ``` -------------------------------- ### KSP Visitor for Code Generation Source: https://github.com/google/ksp/blob/main/examples/hello-world/README.md Implements the Visitor pattern to traverse the KSP AST and generate code. This visitor specifically targets functions annotated with HelloWorld. ```kotlin package com.example.processor import com.google.devtools.ksp.processing.CodeGenerator import com.google.devtools.ksp.processing.KSPLogger import com.google.devtools.ksp.symbol.KSFunctionDeclaration import com.google.devtools.ksp.symbol.KSVisitorVoid class HelloWorldVisitor(private val codeGenerator: CodeGenerator, private val logger: KSPLogger) : KSVisitorVoid() { override fun visitFunctionDeclaration(function: KSFunctionDeclaration, data: Unit) { val functionName = function.simpleName.asString() val packageName = function.containingFile!!.packageName.asString() val generatedFunction = "fun sayHello() { println(\"Hello from $functionName!\") }" codeGenerator.createNewFile(packageName = packageName, defaultName = "GeneratedHelloWorld").use {\n it.write(generatedFunction.toByteArray()) } } } ``` -------------------------------- ### Processor Build Configuration (build.gradle.kts) Source: https://github.com/google/ksp/blob/main/examples/hello-world/README.md Configures the processor module's build script. It depends on the annotations module. ```gradle-kts dependencies { implementation(project(":annotations")) ksp(gradleApi()) } ``` -------------------------------- ### Check API Signature with Gradle Source: https://github.com/google/ksp/blob/main/CONTRIBUTING.md Use this Gradle command to verify API changes. Ensure your modifications are reflected correctly in the API signature. ```gradle ./gradlew :api:checkApi ``` -------------------------------- ### Run Kotlin Compiler in Gradle Daemon Source: https://github.com/google/ksp/blob/main/DEVELOPMENT.md Configure the Kotlin compiler to run within the Gradle daemon process. This simplifies debugging by allowing direct IDE attachment to the Gradle process, but may have performance and correctness implications. ```bash kotlin.compiler.execution.strategy=in-process ``` -------------------------------- ### Run KSP Test Suite Source: https://github.com/google/ksp/blob/main/CONTRIBUTING.md Execute all tests in the KSP test suite using this Gradle task. This command runs tests for the compiler plugin. ```gradle :compiler-plugin:test ``` -------------------------------- ### Run KSP2 Test Suite Source: https://github.com/google/ksp/blob/main/CONTRIBUTING.md Execute all tests in the KSP2 test suite using this Gradle task. This command runs tests for the Kotlin Analysis API. ```gradle :kotlin-analysis-api:test ``` -------------------------------- ### Define HelloWorld Annotation Source: https://github.com/google/ksp/blob/main/examples/hello-world/README.md Defines the annotation used to mark functions for KSP processing. Place annotations in a separate module to avoid leaking processor implementation details. ```kotlin package com.example.annotations annotation class HelloWorld ``` -------------------------------- ### Add Kotlin-Dev Maven Repository Source: https://github.com/google/ksp/wiki/KSP-multiple-round-preview-guide Configure your project to use the `kotlin-dev` Maven repository to access necessary compiler versions. ```gradle repositories { ... maven("https://dl.bintray.com/kotlin/kotlin-dev") } ``` -------------------------------- ### Add Maven Local Repository Source: https://github.com/google/ksp/wiki/KSP-multiple-round-preview-guide Include `mavenLocal()` in your repository configuration to access locally built KSP versions. ```gradle repositories { ... mavenLocal() ... } ``` -------------------------------- ### KSP Processor Provider Registration Source: https://github.com/google/ksp/blob/main/examples/hello-world/README.md Registers the SymbolProcessorProvider implementation. This file must be located in `META-INF/services/` and contain the fully qualified name of your provider class. ```properties com.example.processor.HelloWorldProcessorProvider ``` -------------------------------- ### Debug KotlinCompileDaemon with Gradle Source: https://github.com/google/ksp/blob/main/DEVELOPMENT.md Invoke a Gradle build with KSP debugging enabled. This command attaches a debugger to the KotlinCompileDaemon process, allowing for step-by-step debugging of KSP processors. Ensure the address and port match your debugger configuration. ```bash ./gradlew :app:kspDebugKotlin --rerun-tasks -Dkotlin.daemon.jvm.options="-Xdebug,-Xrunjdwp:transport=dt_socket\,address=8765\,server=y\,suspend=n" ``` -------------------------------- ### Update API Signature with Gradle Source: https://github.com/google/ksp/blob/main/CONTRIBUTING.md Execute this Gradle command to generate a new API signature after making changes. This is crucial for maintaining API consistency. ```gradle ./gradlew :api:updateApi ``` -------------------------------- ### Add KSP Nightly Builds Repository Source: https://github.com/google/ksp/blob/main/README.md Include this Maven repository in your Gradle build to access nightly builds of KSP. This allows you to test the latest, potentially unstable, versions of KSP. ```gradle maven("https://central.sonatype.com/repository/maven-snapshots/") ``` -------------------------------- ### Configure KSP2 in Gradle Source: https://github.com/google/ksp/blob/main/README.md Use this configuration in your Gradle build scripts to explicitly disable KSP2 and revert to KSP1. This is useful for projects that require KSP1 compatibility during a migration period. ```gradle ksp { useKsp2 = false } ``` -------------------------------- ### Run Specific Generated Test Source: https://github.com/google/ksp/blob/main/CONTRIBUTING.md To run a single generated test, specify the test name using the --tests flag with the Gradle command. Replace '' with the actual test name. ```gradle --tests "com.google.devtools.ksp.test.KSPCompilerPluginTest." ``` -------------------------------- ### Increase Gradle Daemon Heap Limit Source: https://github.com/google/ksp/blob/main/docs/ksp2.md KSP2 runs in the Gradle daemon, which may have a lower default heap limit than KSP1's KotlinCompileDaemon. Increase the heap limit using this Gradle property if necessary. ```properties org.gradle.jvmargs=-Xmx4096M -XX:MaxMetaspaceSize=1024m ``` -------------------------------- ### Apply Kotlin Compiler Version Source: https://github.com/google/ksp/wiki/KSP-multiple-round-preview-guide Specify the `1.4.30-M2-104` version of the Kotlin compiler for your project. ```gradle plugins { kotlin("jvm") version "1.4.30-M2-104" apply false } ``` -------------------------------- ### Stop Gradle Daemon and KotlinCompileDaemon Source: https://github.com/google/ksp/blob/main/DEVELOPMENT.md Gracefully stop the Gradle daemon and forcefully terminate any running KotlinCompileDaemon processes. This is useful for resetting the build environment before debugging or when encountering issues with existing daemons. ```bash ./gradlew --stop; pkill -f KotlinCompileDaemon ``` -------------------------------- ### Specify Annotation for Symbol Lookup Source: https://github.com/google/ksp/blob/main/kotlin-analysis-api/testData/getSymbolsWithAnnotation/README.md When using GetSymbolsWithAnnotationProcessor, include a comment with the fully qualified names of annotations to be used for symbol lookup. ```kotlin // PROCESSOR INPUT: Anno ``` ```kotlin // PROCESSOR INPUT: kotlin.annotation.Retention, kotlin.annotation.Target ``` -------------------------------- ### Find KotlinCompileDaemon PID Source: https://github.com/google/ksp/blob/main/DEVELOPMENT.md Locate the process ID (PID) of the KotlinCompileDaemon. This command filters running processes to find the one associated with the specified debug port, which is crucial for attaching a debugger. ```bash ps ax | grep 8765 | grep KotlinCompileDaemon ``` -------------------------------- ### KSP2: Resolve implicit type from function call Source: https://github.com/google/ksp/blob/main/docs/ksp2api.md In KSP2, the container type is resolved successfully, and errors are reported specifically on the type argument if it's a non-existent type. This differs from KSP1 where the entire type would be an error type. ```kotlin val error = mutableMapOf() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.