PopKorn is a lightweight and powerful Dependency Injector designed specifically for Kotlin Multiplatform projects. It simplifies the process of managing dependencies without the hassle of boilerplate code. In this article, we will explore how to set up and use PopKorn effectively across various platforms, including Android, iOS, JVM, JS, and Native.
Installation
To get started with PopKorn, you’ll need to add it to your project. You can easily do this using Gradle.
implementation "cc.popkorn:popkorn:2.3.1"
kapt "cc.popkorn:popkorn-compiler:2.3.1"
If you’re working on an Android project, use:
implementation "cc.popkorn:popkorn-jvm:2.3.1"
kapt "cc.popkorn:popkorn-compiler:2.3.1"
Working with Scopes and Environments
Scopes define the lifecycle of an instance in PopKorn. You can choose from four types of scopes:
- Scope.BY_APP: The instance lives for the entire duration of the application.
- Scope.BY_USE: An instance is created only if it’s being used.
- Scope.BY_HOLDER: The instance lives as long as its holder lives.
- Scope.BY_NEW: A new instance is created every time it is requested.
Let’s use an analogy to better understand these scopes. Imagine you have a library (your app) that lends out books (instances):
- If a book is important and frequently checked out, the library keeps one copy (BY_APP).
- If a book is occasionally required but not always, the library only gets it when there’s a demand (BY_USE).
- Some books are shared among study groups (BY_HOLDER), where their lifespan is as long as the group exists.
- Lastly, if someone always asks for the latest news magazine, the library buys a fresh copy every time (BY_NEW).
Injecting Project Classes
You can easily mark classes for injection with the @Injectable annotation:
@Injectable
class HelloWorld { ... }
You can inject this class anywhere using:
val helloWorld = injectHelloWorld()
Injecting External Classes
If you need to inject a class from an external library, use the @InjectableProvider annotation:
@InjectableProvider(scope = Scope.BY_APP)
class MyRetrofitProvider {
fun createRetrofit(client: OkHttp): Retrofit { ... }
}
Testing with PopKorn
PopKorn also supports dynamic testing by allowing you to override default dependencies:
val hello = popKorn().createHello
override(HelloTestBarcelona())
Using PopKorn in Android and iOS
PopKorn provides seamless support for both Android and iOS platforms. For Android, use the provided injection methods without any initialization. For iOS, make sure to set up a Bridge.kt file in your module to establish the connection:
fun init(creator: (ObjCClass) -> Mapping) = cc.popkorn.setup(creator)
Troubleshooting
If you encounter any issues during setup or usage of PopKorn, consider the following troubleshooting tips:
- Ensure you have the correct Gradle version compatible with Kotlin Gradle Plugin 1.4.0 or above.
- When using Android, make sure to add the necessary packaging options at Gradle to avoid merging errors:
android {
packagingOptions {
merge "META-INF/popkorn.provider.mappings"
merge "META-INF/popkorn.resolver.mappings"
}
}
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
PopKorn is an efficient tool for managing dependencies across different platforms in Kotlin Multiplatform. It reduces boilerplate code and enhances maintainability in your projects. We invite you to explore its functionalities and integrate them into your applications.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

