File Compilation
The Analysis API enables in-memory compilation of Kotlin files. This allows your tools and plugins to generate and compile Kotlin code without writing files to disk or invoking the command-line compiler.
Usage
To compile Kotlin code, you need to provide:
KtFile
: The Kotlin file to compile (might be aKtCodeFragment
);CompilerConfiguration
: A configuration instance containing compiler settings, such as a module name, and a target language version;KaCompilerTarget
: The target platform for compilation (currently, only JVM is supported withKaCompilerTarget.Jvm
);allowedErrorFilter
: A function that filters allowed diagnostics. Compilation fails if there are errors that this function rejects.
Here's an example of compiling a KtFile
:
The compile
function throws a KaCodeCompilationException
if any unexpected errors occur during compilation. Handle this exception appropriately in your code.
KaCompilationResult
The compile
function returns a KaCompilationResult
, which can be either:
KaCompilationResult.Success
: Compilation was successful. You can access the generated output files through theoutput
property.KaCompilationResult.Failure
: Compilation failed due to errors. Theerrors
property contains a list of the encountered diagnostics.
Output Files
The output
property of KaCompilationResult.Success
provides a list of KaCompiledFile
instances, each containing:
val path: String
The relative path of the compiled file.
val sourceFiles: List<File>
The source files that contributed to the compiled file.
val content: ByteArray
The content of the compiled file as a byte array.