<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged dependecyinjection</title>
<link>https://coderlegion.com/tag/dependecyinjection</link>
<description>Powered by Question2Answer</description>
<item>
<title>Koin vs Dagger 2 vs Hilt: Which should we use?</title>
<link>https://coderlegion.com/2797/koin-vs-dagger-2-vs-hilt-which-should-we-use</link>
<description>&lt;p&gt;Have you ever heard about Dependency Injection? If you haven’t heard it before, let’s break down Dependency Injection (DI) in the simplest way possible.&lt;/p&gt;
&lt;p&gt;Imagine you’re building a coffee machine. Instead of the machine creating its own coffee beans and water, you provide them from the outside. This way, you can easily swap out different types of beans or water sources without rebuilding the entire machine.&lt;/p&gt;
&lt;p&gt;In coding, Dependency Injection (DI) works the same way. Instead of a class creating its own dependencies, they’re injected from the outside. This makes your code more flexible, testable, and easier to manage. Android frameworks like Dagger 2, Hilt, and Koin help automate this process, so you don’t have to manually pass everything around.&lt;/p&gt;
&lt;p&gt;These three are popular DI frameworks used in Android development. They help manage object creation and dependency resolution, making code cleaner, more modular, and easier to maintain. Let’s deep dive into among of these three and compare them by performance, usability, and community support.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When comparing Koin, Dagger 2, and Hilt for dependency injection in Android development, performance plays a crucial role. Dagger 2 is a compile-time dependency injection framework, meaning it generates code at build time, leading to optimal runtime performance with minimal overhead. Hilt, built on top of Dagger 2, maintains the same compile-time benefits while simplifying its setup. Koin, on the other hand, is a runtime dependency injection framework that uses reflection and service locators, making it slightly slower than Dagger 2 and Hilt, especially in large-scale applications. While the difference may not be significant for smaller apps, it can impact performance in more complex dependency graphs.&lt;/p&gt;
&lt;p&gt;Let say we demonstrate a “hello world” app using among of them and this is the result:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=15284996762236585071&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;from the illustration above we can conclude:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Koin: Koin’s compile-time is the fastest because it primarily relies on Kotlin’s reflection and doesn’t involve extensive code generation.&lt;/li&gt;
&lt;li&gt;Dagger 2: Dagger 2’s compile-time is significantly longer due to the code generation process, even for a simple app.&lt;/li&gt;
&lt;li&gt;Hilt: Hilt, being built on Dagger 2, also involves code generation, but it’s often more optimized for Android and might have slightly faster compile times than raw Dagger 2 in a very small project.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Usability&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In terms of usability, Koin is the easiest to implement, requiring minimal boilerplate code with a simple DSL for defining dependencies. Dagger 2 is more complex due to its annotation-based approach, requiring knowledge of components, modules, and generated code. Hilt simplifies Dagger 2’s complexity by introducing annotations like @HiltAndroidApp and @Inject, making dependency injection easier to manage while still leveraging compile-time validation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Community Support and Documentation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Regarding community support and documentation, Dagger 2 has been widely adopted in the Android ecosystem for years and has extensive documentation. Hilt, as an official AndroidX library, benefits from strong Google backing and comprehensive guides. Koin, while popular among Kotlin developers, has a smaller but growing community. Its documentation is clear but not as extensive as Google’s offerings. Ultimately, Dagger 2 and Hilt are preferred for performance-sensitive and large-scale projects, while Koin is an excellent choice for developers seeking simplicity and faster integration.&lt;/p&gt;
&lt;p&gt;Below are example implementations of Koin, Dagger 2, and Hilt:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Koin Implementation&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// Define a module
val appModule = module {
    single { UserRepository() }
}

// Start Koin
class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        startKoin {
            modules(appModule)
        }
    }
}

// Inject dependency
class MyViewModel(private val repository: UserRepository)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Dagger 2 Implementation&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// Define a module
@Module
class AppModule {
    @Provides
    fun provideUserRepository(): UserRepository {
        return UserRepository()
    }
}

// Define a component
@Component(modules = [AppModule::class])
interface AppComponent {
    fun inject(activity: MainActivity)
}

// Inject dependency
class MainActivity : AppCompatActivity() {
    @Inject lateinit var repository: UserRepository
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Hilt Implementation&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// Define a module
@InstallIn(SingletonComponent::class)
@Module
object AppModule {
    @Provides
    fun provideUserRepository(): UserRepository {
        return UserRepository()
    }
}

// Inject dependency
@HiltAndroidApp
class MyApplication : Application()

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    @Inject lateinit var repository: UserRepository
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Choose wisely what framework you want to use, these trio have &lt;strong&gt;pros&lt;/strong&gt; and &lt;strong&gt;cons&lt;/strong&gt;. You can choose and implement it based on your need. Happy coding :)&lt;/p&gt;
&lt;p&gt;If you want to know more about Koin framework setup, refer this&lt;/p&gt;
&lt;p&gt;Documentation:&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://dagger.dev/&quot;&gt;Dagger2&lt;/a&gt;,&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://dagger.dev/hilt/&quot;&gt;Hilt&lt;/a&gt;,&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://insert-koin.io/docs/quickstart/android/&quot;&gt;Koin&lt;/a&gt;&lt;/p&gt;
</description>
<category>Mobile Dev</category>
<guid isPermaLink="true">https://coderlegion.com/2797/koin-vs-dagger-2-vs-hilt-which-should-we-use</guid>
<pubDate>Wed, 23 Apr 2025 04:26:52 +0000</pubDate>
</item>
<item>
<title>Koin vs Hilt</title>
<link>https://coderlegion.com/1506/koin-vs-hilt</link>
<description>&lt;p&gt;In &lt;strong&gt;object-oriented programming (OOP)&lt;/strong&gt;, especially in &lt;em&gt;Android Development&lt;/em&gt;, to make our program code cleaner and maintainable, one of the principles we are used to apply is &lt;strong&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://simple.wikipedia.org/wiki/SOLID_(object-oriented_design)&quot;&gt;SOLID&lt;/a&gt;&lt;/strong&gt;. one of the points in the SOLID principle is &lt;strong&gt;Dependency Inversion&lt;/strong&gt;.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;One should depend upon abstractions, not concretions.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://en.wikipedia.org/wiki/Inversion_of_control&quot;&gt;Inversion of Control (IoC)&lt;/a&gt;&lt;/strong&gt; is a design pattern used to represent Dependency Inversion in real software.&lt;/p&gt;
&lt;p&gt;That makes Dependency Injection popular.&lt;/p&gt;
&lt;h3 id=&quot;what-is-dependency-injection-&quot;&gt;What is Dependency Injection?&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;Dependency injection&lt;/em&gt; (DI) is a software engineering technique that involves making the interactions between objects as thin as possible through specific dependencies. This allows for loosely coupled code — or code that only depends on the required portion of a separate class — to run. And it reduces hard-coded dependencies and allows for cleaner, more flexible code.&lt;/p&gt;
&lt;p&gt;some of most popular Dependency Injection frameworks are &lt;strong&gt;Koin&lt;/strong&gt; and &lt;strong&gt;Hilt&lt;/strong&gt;. &lt;/p&gt;
&lt;h3 id=&quot;then-what-s-different-between-koin-and-hilt-&quot;&gt;Then what's different between Koin and Hilt?&lt;/h3&gt;
&lt;p&gt;Let’s start by talking about Koin first.&lt;/p&gt;
&lt;p&gt;Koin stands out for its simplicity and flexibility. It is a simple DI framework that does not require annotations or code generation. Instead, it makes use of the Domain Specific Language (DSL) provided by Kotlin to specify dependencies and modules.&lt;/p&gt;
&lt;p&gt;Koin’s ease of use is one of its benefits. Its minimum setup requirements and its ability to create dependencies with a clear, readable syntax are the aspects that make it stand out. Therefore, Koin is easy for developers who are new to DI.&lt;/p&gt;
&lt;p&gt;On the other hand, this features of Koin might come with trade-offs in certain scenarios. Its runtime resolution might lead to potential issues during compile-time if dependencies are not properly configured. Additionally, Koin might not offer the same level of performance optimizations that some other DI frameworks, like Hilt, provide.&lt;/p&gt;
&lt;h3 id=&quot;how-to-use-koin-&quot;&gt;How to use Koin?&lt;/h3&gt;
&lt;p&gt;First, add dependencies to our project’s &lt;em&gt;build.gradle&lt;/em&gt; file,&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;dependencies {
    // Koin core features
    implementation 'org.koin:koin-core:3.2.0'

    // Koin Android features
    implementation 'org.koin:koin-android:3.2.0'
    implementation 'org.koin:koin-androidx-viewmodel:3.2.0'
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then, create Koin modules,&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import org.koin.dsl.module

val myKoinModule = module {
    single { UserRepository() } 
    factory { UserViewModel(get()) }
    // Define other dependencies...
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;single&lt;/strong&gt;: create an object that persistent with the entire container lifetime. It does not create a new instance with each call.&lt;br&gt;
&lt;strong&gt;factory&lt;/strong&gt;: create a new object each time. Short live. No persistence in the container&lt;/p&gt;
&lt;p&gt;Then in Application class, initialize Koin by loading the modules,&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;class MyKoinApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        startKoin {
            androidContext(this@MyKoinApplication )
            modules(myKoinModule )
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is how we call at Activity&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;class HomeActivity : AppCompatActivity() {
    private val viewModel by viewModel&amp;lt;UserViewModel&amp;gt;()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // Use viewModel or other dependencies...
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;how about Hilt?&lt;/p&gt;
&lt;p&gt;Hilt, developed by Google and built on top of Dagger, is a powerful dependency injection (DI) framework tailored for Android. It simplifies DI by leveraging annotations and compile-time checks to ensure correctness and performance. Additionally, it integrates seamlessly with Jetpack libraries like ViewModel and WorkManager, making it easier to manage dependencies across different Android components.&lt;/p&gt;
&lt;p&gt;One of Hilt’s biggest advantages is its strong alignment with the Android development ecosystem, making it a preferred choice for larger projects following architectural patterns like MVVM or Clean Architecture. However, its learning curve can be steep due to its reliance on Dagger and the specific annotations required. Compared to Koin, Hilt may also involve a more complex setup, but it offers better compile-time safety and performance benefits.&lt;/p&gt;
&lt;h3 id=&quot;how-to-use-hilt&quot;&gt;How to use Hilt&lt;/h3&gt;
&lt;p&gt;Add Hilt dependency at our project’s &lt;em&gt;build.gradle&lt;/em&gt; file,&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;dependencies {
    // Other dependencies...
    implementation &quot;com.google.dagger:hilt-android:$hilt_version&quot;
    kapt &quot;com.google.dagger:hilt-compiler:$hilt_version&quot;
    implementation &quot;androidx.hilt:hilt-lifecycle-viewmodel:$hilt_lifecycle_version&quot;
    kapt &quot;androidx.hilt:hilt-compiler:$hilt_lifecycle_version&quot;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Enable Hilt to our Application&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;@HiltAndroidApp
class MyHiltApplication : Application()
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Creating Hilt modules&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;@Module
@InstallIn(ApplicationComponent::class)
object MyModule {
    @Provides
    fun provideMyRepository(): MyRepository {
        return MyRepository()
    }
    
    @Provides
    fun provideMyViewModel(repository: MyRepository): MyViewModel {
        return MyViewModel(repository)
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using dependencies in Activity/Fragment,&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;@AndroidEntryPoint
class MyActivity : AppCompatActivity() {
    private val viewModel: MyViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;There is 3 different characteristics between Koin and Hilt:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Syntax and Configuration:&lt;/strong&gt; Koin offers a simple and readable syntax without relying on annotations or code generation, while Hilt heavily depends on annotations like @Inject and @Module for dependency injection.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning Curve and Complexity:&lt;/strong&gt; Koin is beginner-friendly with minimal setup, making it easier to adopt. In contrast, Hilt has a steeper learning curve due to its integration with Dagger and required annotations.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Performance and Optimization:&lt;/strong&gt; Koin has faster build times and a smaller APK size since it doesn’t generate code, but it is slightly slower at runtime due to resolving dependencies dynamically. Hilt, using code generation, ensures better runtime performance but can lead to longer build times and a larger APK.&lt;/p&gt;
&lt;h3 id=&quot;so-when-to-use-koin-and-hilt-&quot;&gt;So when to use Koin and Hilt?&lt;/h3&gt;
&lt;p&gt;_Koin _is ideal for smaller projects due to its simplicity and flexibility, making code more modular and maintainable. However, for larger projects requiring deep integration with Android components, _Hilt _is preferable for its performance optimizations and alignment with Android architecture.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;references: &lt;br&gt;
_&lt;a rel=&quot;nofollow&quot; href=&quot;https://medium.com/ibtech/hilt-vs-koin-f3532b5796eb&quot;&gt;https://medium.com/ibtech/hilt-vs-koin-f3532b5796eb&lt;/a&gt;&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://en.wikipedia.org/wiki/Dependency_injection&quot;&gt;https://en.wikipedia.org/wiki/Dependency_injection&lt;/a&gt;&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://simple.wikipedia.org/wiki/SOLID_(object-oriented_design)_&quot;&gt;https://simple.wikipedia.org/wiki/SOLID_(object-oriented_design)_&lt;/a&gt;&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://medium.com/@srimanikandan92/hilt-vs-koin-a-simple-guide-to-android-dependency-injection-ef77d00e7cf6&quot;&gt;https://medium.com/@srimanikandan92/hilt-vs-koin-a-simple-guide-to-android-dependency-injection-ef77d00e7cf6&lt;/a&gt;&lt;/p&gt;
</description>
<category>Mobile Dev</category>
<guid isPermaLink="true">https://coderlegion.com/1506/koin-vs-hilt</guid>
<pubDate>Sun, 16 Mar 2025 07:37:25 +0000</pubDate>
</item>
<item>
<title>Koin on modularization: How to use it?</title>
<link>https://coderlegion.com/1504/koin-on-modularization-how-to-use-it</link>
<description>&lt;p&gt;&lt;strong&gt;What is Modularization in Android?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Modularization in Android means breaking an app into smaller, independent modules instead of keeping all code in a single project. Each module has its own responsibility and can be developed, tested, and maintained separately.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Basic Prerequisites to Understand Modularization&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Before learning modularization, you should be familiar with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Android Project Structure – Understanding how an app is built using&lt;br&gt;
 app/src/main. &lt;/li&gt;
&lt;li&gt;Gradle &amp;amp; Dependencies – Knowing how dependencies are&lt;br&gt;
 managed in build.gradle.kts. &lt;/li&gt;
&lt;li&gt;Packages &amp;amp; Code Organization – Basics&lt;br&gt;
 of organizing files using packages. &lt;/li&gt;
&lt;li&gt;Types of Android Modules – App&lt;br&gt;
 Module (Main application) &lt;/li&gt;
&lt;li&gt;Library Module (Reusability across&lt;br&gt;
 projects) &lt;/li&gt;
&lt;li&gt;Feature Module (For dynamic delivery in large apps)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Why Modularization?&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;✅ Faster Build Times – Only modified modules get compiled. &lt;/li&gt;
&lt;li&gt;✅ Better&lt;br&gt;
Code Maintainability – Easier to manage and scale. &lt;/li&gt;
&lt;li&gt;✅ Reusability –&lt;br&gt;
Common features like networking or authentication can be shared&lt;br&gt;
across projects. &lt;/li&gt;
&lt;li&gt;✅ Parallel Development – Teams can work on different&lt;br&gt;
modules independently.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Today we are going to set up step by step how to use &lt;strong&gt;Koin&lt;/strong&gt; in modularization with &lt;strong&gt;viewModel&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Before we get started. Create some new modules and structured them like this:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=8058861093745042862&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You have to determine what koin gradle version you want to use. For this tutorial, I use &lt;strong&gt;2.2.2&lt;/strong&gt; version. Define the gradle into di module&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;implementation &quot;io.insert-koin:koin-android:2.2.2&quot;
implementation &quot;io.insert-koin:koin-androidx-viewmodel:2.2.2&quot;
implementation &quot;io.insert-koin:koin-androidx-scope:2.2.2&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step&lt;/strong&gt; 2&lt;/p&gt;
&lt;p&gt;After you have decided koin version you'd use, add other dependency your project would need for injecting to koin in di module as well. In this tutorial I want to inject Retrofit for network, AndroidX Room for local DB, Shared Preferences, ViewModel for managing data, and Coroutine for threading&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;implementation &quot;io.insert-koin:koin-android:2.2.2&quot;
implementation &quot;io.insert-koin:koin-androidx-viewmodel:2.2.2&quot;
implementation &quot;io.insert-koin:koin-androidx-scope:2.2.2&quot;
implementation &quot;javax.inject:javax.inject:1&quot;

implementation &quot;com.squareup.okhttp3:logging-interceptor:4.9.0&quot;
implementation &quot;com.squareup.retrofit2:retrofit:2.9.0&quot;
implementation &quot;com.squareup.retrofit2:converter-moshi:2.9.0&quot;
implementation &quot;com.squareup.retrofit2:converter-gson:2.9.0&quot;

implementation &quot;com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2&quot;

implementation &quot;androidx.room:room-runtime:2.4.2&quot;
kapt &quot;androidx.room:room-compiler:2.4.2&quot;
implementation &quot;androidx.room:room-ktx:2.4.2&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Define &lt;strong&gt;Network Module&lt;/strong&gt;, &lt;strong&gt;Api Module&lt;/strong&gt;, &lt;strong&gt;Shared Preferences Module&lt;/strong&gt;, &lt;strong&gt;Room Module&lt;/strong&gt;, and &lt;strong&gt;ViewModel Module&lt;/strong&gt; (we also use &lt;strong&gt;UseCase&lt;/strong&gt; and &lt;strong&gt;Repository&lt;/strong&gt; module for clean architecture principle)&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;class ApiModule {
    companion object {
        val apiModule = module(override = true) {
            single { provideDisneyApi(get()) }
        }

        fun provideDisneyApi(retrofit: Retrofit): Api {
            return retrofit.create(Api::class.java)
        }
    }
}

class NetworkModule  {
    companion object {
        private fun httpInterceptor() = HttpLoggingInterceptor().apply {
            return HttpLoggingInterceptor { _ -&amp;gt;
            }.apply {
                level = HttpLoggingInterceptor.Level.BODY
            }
        }

        fun provideNetworkHandler(context: Context) = NetworkHandler(context)

        fun provideOkHttpClient() : OkHttpClient {
            return Builder.initInterceptor(httpInterceptor())
        }

        fun provideRetrofitService(okHttpClient: OkHttpClient): Retrofit {
            return Builder.initRetrofit(okHttpClient)
        }
    }
}

class SharedPrefModule {
    companion object {
        fun providePreference(context: Context) : SharedPreferences {
            return context.getSharedPreferences(&quot;SharedPreferenceName&quot;, Context.MODE_PRIVATE)
        }
    }
}

class RoomModule {
    companion object {
        fun provideRoom(context: Context) : DBConfig {
            return Room.databaseBuilder(context, DBConfig::class.java, &quot;db_sample&quot;)
                .fallbackToDestructiveMigration()
                .build()
        }
    }
}

class RepositoryModule {
    companion object {
        val repositoryModule = module(override = true) {
            single&amp;lt;CharacterRepository&amp;gt; { return@single CharacterRepositoryImpl(get(), get()) }
        }
    }
}

class UseCaseModule {
    companion object {
        val useCaseModule = module(override = true) {
            single&amp;lt;CharacterUseCase&amp;gt; { return@single CharacterUseCaseImpl(get()) }
        }
    }
}

class ViewModelModule {
    companion object {
        val viewModelModule = module(override = true) {
            viewModel { GamesListVM(get(), get()) }
            viewModel { GamesDetailVM(get(), get()) }
            viewModel { GamesFavoriteVM(get(), get(), get()) }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After you add them all, locate and categorize module that you prioritize as a 'core' module and wrap them into &lt;strong&gt;AppModule&lt;/strong&gt; class. In this tutorial I'll wrap &lt;strong&gt;retrofit&lt;/strong&gt;, &lt;strong&gt;room&lt;/strong&gt;, and &lt;strong&gt;shared preferences&lt;/strong&gt; module like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;val MainAppModule = module(override = true) {
    single { NetworkModule.provideOkHttpClient() }
    single { NetworkModule.provideRetrofitService(get()) }
    single { NetworkModule.provideNetworkHandler(androidContext()) }
    single { RoomModule.provideRoom(androidContext()) }
    single { SharedPrefModule.providePreference(androidContext()) }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Call them with &lt;strong&gt;StartKoin&lt;/strong&gt; in &lt;strong&gt;Application Class&lt;/strong&gt; in &lt;strong&gt;App Module&lt;/strong&gt; (default module). I will call ViewModel, Repository, Usecase, and Api module as same level with MainApp Module&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;class DisneyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        startKoin {
            androidLogger(Level.ERROR)
            androidContext(this@DisneyApp)
            modules(MainAppModule, ApiModule.apiModule, RepositoryModule.repositoryModule,
                UseCaseModule.useCaseModule, ViewModelModule.viewModelModule
            )
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And Voila! Congratulation you've built Koin in modular version. it's easy isn't it?&lt;/p&gt;
&lt;p&gt;Happy Coding :)&lt;/p&gt;
&lt;p&gt;Sample project: &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/mikkelofficial7/android-game-search&quot;&gt;Github link&lt;/a&gt;&lt;/p&gt;
</description>
<category>Best Practices</category>
<guid isPermaLink="true">https://coderlegion.com/1504/koin-on-modularization-how-to-use-it</guid>
<pubDate>Sat, 15 Mar 2025 20:05:51 +0000</pubDate>
</item>
</channel>
</rss>