Dependency Injection library for Kotlin Multiplatform, support iOS and Android

Overview

Maven Central supports iOS supports Android GitHub tag (latest SemVer pre-release) GitHub last commit License: MIT GitHub stars GitHub forks GitHub watchers GitHub followers

Twitter URL

Buy Me A Coffee donate button

Multiplatform-DI library for Kotlin Multiplatform

Lightweight dependency injection framework for Kotlin Multiplatform application

Dependency injection (DI) is a software design pattern that implements Inversion of Control (IoC) and Service Locator for resolving dependencies. It could be implemented many ways. More info about DI here

Multiplatform-DI helps you to use DI in your KMM application easily, to provide shared common architecture solution that works both in iOS and Android native applications. Yes! It works even with iOS, in all cases.

Supports

  • iOS (Kotlin Native)
  • Android (Kotlin JVM)

Features

  • Thread safety
  • WeakReference
  • Singletones (no extra actions in iOS side)
  • Scopes
  • Easy register and resolve components

Future

  • KSP injection
  • Circullar dependencies

Installation

Just add a dependency into your build.gradle.kts in shared module. Available in maven central.

//build.gradle.kts

 repositories {
      mavenCentral()
    }
/*....*/

allprojects {
    repositories {
        google()
        mavenCentral()
        maven(url = "https://jitpack.io")
    }
}
 dependencies {
       implementation("io.github.anioutkazharkova:di-multiplatform-lib:1.0.4.5")
  }

Sample

Check the ready-mady solution here It is a small client for Movies DB API with common BL and architecture

Basic usage (good practice)

You can also use this KMM DI template to create your app with integrated DI. More info in wiki

Or you can also integrate if all by yourself. Just follow next steps:

  1. First, you need to enable an access to DI library from native apps, because their is no direct access to dependencies in shared module. You need create a class to manage registration and resolution of objects with DIContainer. It should be placed in commonMain of your shared module:
class DIManager {
    val appContainer: DIContainer by lazy { DIContainer() }

    fun<T:Any> resolve(type: KClass<T>):Any? {
        return appContainer.resolve(type)
    }

    fun<T:Any> addToScope(scope: ScopeType, type: KClass<T>, fabric: ()->T?) {
        appContainer.register(type, ScopeType.Graph,fabric)
    }
}
  1. Provide common configuration (used for both common and native apps). Also place it in commonMain:
class ConfigurationApp {
val appContainer: DIManager = DIManager()

   init {
       setup()
   }

    fun setup() {
       //register hear all your components from shared module
    }
}
  1. Register all your components from shared module (also commonMain):
 fun setup() {
        appContainer.addToScope(ScopeType.Container,NetworkClient::class) {
            NetworkClient()
        }
        appContainer.addToScope(ScopeType.Container,MoviesService::class) {
            val nc = appContainer.resolve<NetworkClient>(NetworkClient::class) as? NetworkClient
            MoviesService(nc)
        }
    }
  1. Now you can resolve components from any place of your shared part:
fun setup(di: DIManager) {
        this.moviesService = di.resolve<MoviesService>(MoviesService::class) as? MoviesService
        print(moviesService)
    }
  1. Next, create a property to get access container in Android native app:
@HiltAndroidApp
class App : Application() {
    companion object {
        val config = ConfigurationApp()
        val container = config.appContainer
        lateinit var INSTANCE:App
        var AppContext: Context? = null
    }
    override fun onCreate() {
        super.onCreate()
        INSTANCE = this
        AppContext = this
    }
}
  1. Repeat same for iOS:
class Util{
    static var shared = Util()
    private lazy var config: ConfigurationApp = {
       return ConfigurationApp()
    }()
    
    lazy var container: DIManager = {
        return config.appContainer
    }()
}

You can also use some decorations to your code (property wrappers, delegates and ets). Sample you can see here

You might also like...
Simple Kotlin Multiplatform PrayerTimes App for iOS and Android

Kotlin Multiplatform ___ _______ ___ / _ \_______ ___ _____ ___/_ __(_)_ _ ___ ___ / _ | __

Location Service Manager for Kotlin Multiplatform Mobile iOS and android
Location Service Manager for Kotlin Multiplatform Mobile iOS and android

Location Service Manager for Kotlin Multiplatform Mobile iOS and android Features Provides simple permission settings Dramatically reduce the amount o

Kotlin Multiplatform Mobile demo for Android and iOS - app for viewing Cat pictures
Kotlin Multiplatform Mobile demo for Android and iOS - app for viewing Cat pictures

CatViewerDemo Android demo iOS demo Kotlin Multiplatform Mobile demo for Android and iOS. App for viewing Cat pictures from Cats API. This sample show

A Kotlin Multiplatform Project using TMDB Api. Currently supports Android,iOS,Desktop and web platforms
A Kotlin Multiplatform Project using TMDB Api. Currently supports Android,iOS,Desktop and web platforms

A Kotlin Multiplatform Project using TMDB Api(https://www.themoviedb.org/). Currently this project is implemented in following platforms Andr

Kotlin Multiplatform is an SDK for cross-platform mobile development, which enables teams to use the same business logic in both Android and iOS client applications.
Kotlin Multiplatform is an SDK for cross-platform mobile development, which enables teams to use the same business logic in both Android and iOS client applications.

Kotlin Multiplatform is an SDK for cross-platform mobile development, which enables teams to use the same business logic in both Android and iOS client applications.

A Gradle plugin for Kotlin Multiplatform projects that generate a XCFramework for Apple targets or a FatFramework for iOS targets, and manages the publishing process in a CocoaPod Repository.

KMP Framework Bundler KMP Framework Bundler is a Gradle plugin for Kotlin Multiplatform projects that generate a XCFramework for Apple targets or a Fa

Ethereum Web3 implementation for mobile (android & ios) Kotlin Multiplatform development

Mobile Kotlin web3 This is a Kotlin MultiPlatform library that ... Table of Contents Features Requirements Installation Usage Samples Set Up Locally C

Kotlin Multiplatform Sample - Android, iOS, Web, Desktop
Kotlin Multiplatform Sample - Android, iOS, Web, Desktop

KMP-Sample Kotlin Multiplatform Sample Android iOS Web (Compose for web) Desktop (Compose for desktop) 💎 Structure Diagram 🛠 Build At least android

Kotlin multiplatform decorators support

DEKORATOR [WIP] Decorator support for Kotlin! Built with ❤ , powered by Kotlin compiler plugin. Support The plugin only works on targets using new IR

Releases(1.2.0)
Owner
Anna Zharkova
Anna Zharkova
Dependency Injection library for Compose Multiplatform, Koin wrapper.

?? Cokoin Injection library for Compose (Multiplatform and Jetpack), Koin wrapper. It uses @Composable functions to configure KoinContext and Scopes.

Bruno Wieczorek 57 Dec 29, 2022
Lightweight Kotlin DSL dependency injection library

Warehouse DSL Warehouse is a lightweight Kotlin DSL dependency injection library this library has an extremely faster learning curve and more human fr

Osama Raddad 18 Jul 17, 2022
Boilerplate code for implementing MVVM in Android using Jetpack libraries, coroutines, dependency injection and local persistance

MVVM Foundation This projects aims to speed up development of Android apps by providing a solid base to extend Libraries Jetpack Fragment Material3 :

Gabriel Gonzalez 2 Nov 10, 2022
Built with Jetpack compose, multi modules MVVM clean architecture, coroutines + flow, dependency injection, jetpack navigation and other jetpack components

RickAndMortyCompose - Work in progress A simple app using Jetpack compose, clean architecture, multi modules, coroutines + flows, dependency injection

Daniel Waiguru 9 Jul 13, 2022
BlurHash support for iOS, Android and JVM via Kotlin Multiplatform

blurhash A Kotlin Multiplatform library to use blurhash in your Android App, iOS / Mac App & JVM Backend. Android iOS JVM Why? If you've tried using b

Niklas Baudy 19 Nov 17, 2022
Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in Kotlin with Jetpack Compose and a backed in Kotlin hosted on AppEngine.

Conferences4Hall Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in K

Gérard Paligot 98 Dec 15, 2022
A Bluetooth kotlin multiplatform "Cross-Platform" library for iOS and Android

Blue-Falcon A Bluetooth "Cross Platform" Kotlin Multiplatform library for iOS, Android, MacOS, Raspberry Pi and Javascript. Bluetooth in general has t

Andrew Reed 220 Dec 28, 2022
A local storage management library for Kotlin Multiplatform Mobile iOS and android

A local storage management library for Kotlin Multiplatform Mobile iOS and android Features iOS and Android local storage in one interface Provides ge

LINE 20 Oct 30, 2022
An app architecture for Kotlin/Native on Android/iOS. Use Kotlin Multiplatform Mobile.

An app architecture for Kotlin/Native on Android/iOS. Use Kotlin Multiplatform Mobile. 项目架构主要分为原生系统层、Android/iOS业务SDK层、KMM SDK层、KMM业务逻辑SDK层、iOS sdkfra

libill 4 Nov 20, 2022
Cross-platform framework for building truly native mobile apps with Java or Kotlin. Write Once Run Anywhere support for iOS, Android, Desktop & Web.

Codename One - Cross Platform Native Apps with Java or Kotlin Codename One is a mobile first cross platform environment for Java and Kotlin developers

Codename One 1.4k Jan 9, 2023