aspectk

Compile-time aspect-oriented weaving via compiler IR transformation, injecting @Aspect/@Before advice with zero runtime overhead; supports suspend, inline, extension, expect/actual functions, inheritance, many-to-many targets, rich JoinPoint metadata.

JVMKotlin/NativeWasmJS
GitHub stars0
AuthorsMole-Labs
Open issues1
LicenseApache License 2.0
Creation date3 months ago

Last activity26 days ago
Latest release0.2.0 (12 days ago)

AspectK

Compile-time Aspect-Oriented Programming for Kotlin Multiplatform.

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

Maven Central License Kotlin KMP


Quick Setup

1. Apply the Gradle plugin

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

For Kotlin Multiplatform:

plugins {
    kotlin("multiplatform")
    id("io.github.mole-labs.aspectk") version "LATEST_VERSION"
}

2. Define a target annotation

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

3. Create an aspect

import io.github.molelabs.aspectk.runtime.Aspect
import io.github.molelabs.aspectk.runtime.Before
import io.github.molelabs.aspectk.runtime.JoinPoint

@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
}

Features

Feature Details
Zero runtime overhead Advice is woven at compile time — the generated code calls the advice function directly
Kotlin Multiplatform JVM, Android, JS (IR), WASM/JS, Native Tier 1–3
K2 IR powered Built on the Kotlin 2.x IR transformation API
Many-to-many targeting One @Before can list multiple target annotations; one function can match multiple aspects
Inheritance support @Before(inherits = true) intercepts overriding functions automatically
Rich join point metadata JoinPoint exposes receiver, method signature, parameters, annotations, and arguments

Supported Function Types

AspectK works with the full range of Kotlin function kinds — including those that other AOP solutions struggle with. Because advice is injected during the IR transformation phase, before platform-specific lowering, AspectK intercepts functions exactly as Kotlin defines them:

  • Class member functions
  • Top-level functions
  • Extension functions
  • suspend functions — interception happens before coroutine lowering, so the advice sees the function in its original, unsuspended form
  • inline functions — intercepted at the call site before the inliner runs
  • Property getters and setters
  • expect/actual functions across all platforms

Supported Platforms

JVM · Android · JS (IR) · WASM/JS · macOS (arm64, x64) · iOS (arm64, sim, x64) · Linux (arm64, x64) · Windows (x64) · watchOS · tvOS · Android Native


Documentation

Full documentation: https://mole-labs.github.io/aspectk/


License

Copyright 2026 Mole Labs

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0
JVMKotlin/NativeWasmJS
GitHub stars0
AuthorsMole-Labs
Open issues1
LicenseApache License 2.0
Creation date3 months ago

Last activity26 days ago
Latest release0.2.0 (12 days ago)

AspectK

Compile-time Aspect-Oriented Programming for Kotlin Multiplatform.

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

Maven Central License Kotlin KMP


Quick Setup

1. Apply the Gradle plugin

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

For Kotlin Multiplatform:

plugins {
    kotlin("multiplatform")
    id("io.github.mole-labs.aspectk") version "LATEST_VERSION"
}

2. Define a target annotation

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

3. Create an aspect

import io.github.molelabs.aspectk.runtime.Aspect
import io.github.molelabs.aspectk.runtime.Before
import io.github.molelabs.aspectk.runtime.JoinPoint

@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
}

Features

Feature Details
Zero runtime overhead Advice is woven at compile time — the generated code calls the advice function directly
Kotlin Multiplatform JVM, Android, JS (IR), WASM/JS, Native Tier 1–3
K2 IR powered Built on the Kotlin 2.x IR transformation API
Many-to-many targeting One @Before can list multiple target annotations; one function can match multiple aspects
Inheritance support @Before(inherits = true) intercepts overriding functions automatically
Rich join point metadata JoinPoint exposes receiver, method signature, parameters, annotations, and arguments

Supported Function Types

AspectK works with the full range of Kotlin function kinds — including those that other AOP solutions struggle with. Because advice is injected during the IR transformation phase, before platform-specific lowering, AspectK intercepts functions exactly as Kotlin defines them:

  • Class member functions
  • Top-level functions
  • Extension functions
  • suspend functions — interception happens before coroutine lowering, so the advice sees the function in its original, unsuspended form
  • inline functions — intercepted at the call site before the inliner runs
  • Property getters and setters
  • expect/actual functions across all platforms

Supported Platforms

JVM · Android · JS (IR) · WASM/JS · macOS (arm64, x64) · iOS (arm64, sim, x64) · Linux (arm64, x64) · Windows (x64) · watchOS · tvOS · Android Native


Documentation

Full documentation: https://mole-labs.github.io/aspectk/


License

Copyright 2026 Mole Labs

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0