Skip to content

AspectK

Compile-time Aspect-Oriented Programming for Kotlin Multiplatform.

AspectK is a Kotlin compiler plugin that injects advice code at compile time — zero runtime reflection, no proxies, no overhead. Declare an @Aspect, annotate your advice with @Before, and AspectK weaves it directly into your functions during IR transformation.

Why AspectK?

  • Zero Runtime Overhead

    Advice is woven at compile time via K2 IR transformation. No reflection, no dynamic proxies — the generated bytecode is as if you wrote the calls by hand.

  • Kotlin Multiplatform First

    Runs on JVM, JS, WASM, and all major Native targets. One aspect definition, every platform.

  • K2 IR Powered

    Built on Kotlin's next-generation K2 compiler IR API. Fully compatible with Kotlin 2.x progressive mode.

Quick Start

1. Add the plugin

// build.gradle.kts
plugins {
    id("io.github.mole-labs.aspectk.compiler") version "LATEST_VERSION"
}

aspectk-runtime is added automatically — no explicit dependency declaration needed.

2. Define a target annotation

@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.BINARY)
annotation class Logged

3. Create an Aspect

@Aspect
object LoggingAspect {
    @Before(target = [Logged::class])
    fun log(joinPoint: JoinPoint) {
        println("→ ${joinPoint.signature.methodName}(${joinPoint.args.joinToString()})")
    }
}

4. Annotate your functions

@Logged
fun processOrder(orderId: String, amount: Double) {
    // AspectK injects LoggingAspect.log() here at compile time
    println("Processing order $orderId")
}

Supported Platforms

Platform Target
JVM
Android
JS (IR)
WASM/JS
macOS (arm64, x64)
iOS (arm64, x64, sim)
Linux (arm64, x64)
Windows (x64)

Get started → API Reference →