Best way to design a plugin-style architecture in Kotlin (JVM) #187754
-
|
Hi everyone 👋 What I’m trying to doI want to allow external modules (plugins) to extend my application by:
Plugins will be distributed as JAR files and dropped into a Current designRight now, I’m using Java-style class loading from Kotlin: val classLoader = URLClassLoader(
arrayOf(pluginJar.toURI().toURL()),
this::class.java.classLoader
)
val pluginClass = classLoader.loadClass("plugin.Main")
val plugin = pluginClass.getDeclaredConstructor().newInstance() as Plugin
plugin.onLoad(api)This works, but I’m not sure if this is the right approach in idiomatic Kotlin. Concerns
Environment
What I’ve considered
Questions
GoalI want a plugin system that is:
Any architectural advice, examples, or references would be greatly appreciated 🙏 Guidelines
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
“What is the recommended way to implement plugins in Kotlin/JVM today?”Short answer: 🧠 Key PrincipleKotlin runs on the JVM, so the recommended approach is still:
Kotlin’s strengths (null safety, data classes, DSLs) help you design a clean API, but plugin loading itself remains a JVM concern. 🏗️ Recommended Architecture1️⃣ Separate
|
Beta Was this translation helpful? Give feedback.
“What is the recommended way to implement plugins in Kotlin/JVM today?”
Short answer:
👉 Use a Java-compatible plugin model built around a shared API module +
ServiceLoader, with explicit lifecycle management. Kotlin does not replace JVM fundamentals here—it builds on them.🧠 Key Principle
Kotlin runs on the JVM, so the recommended approach is still:
Kotlin’s strengths (null safety, data classes, DSLs) help you design a clean API, but plugin loading itself remains a JVM concern.
🏗️ Recommended Architecture
1️⃣ Separate
apimodule (critical)Create a pure API module that both the host app and plugins depend on.