Leverage Dependency Injection as an Android Developer

nathan kayumbanathan kayumba
2 min read

🔹 Introduction

As your mobile application grows, managing dependencies becomes essential. Good architecture isn't just about writing working code — it's about writing modular, testable, and maintainable code.
That’s where Dependency Injection (DI) comes in.

🔹 What is Dependency Injection?

Dependency Injection is a design principle where a class receives its dependencies from the outside instead of creating them internally.

🔧 A dependency is any other class a component needs to function (e.g. UserRepository inside UserViewModel).

❌ Bad practice: direct instantiation

class UserViewModel {
    val repository = UserRepository() // Strong coupling
}

✅ Good practice: constructor injection

class UserViewModel(private val repository: UserRepository)

This allows better separation of concerns and testability.

🔹 Benefits of Dependency Injection

  • Decouples components

  • Reusability across contexts

  • Easy unit testing with mock injections

  • Flexibility for switching implementations

🔹 Manual DI in Android with Kotlin

💡 Simple manual injection

class UserRepository
class UserViewModel(private val repository: UserRepository)

fun main() {
    val repo = UserRepository()
    val viewModel = UserViewModel(repo)
}

Works for small apps — but becomes a mess in real-world, large-scale applications.

🔹 Limitations of manual DI

  • ❌ Repetitive and boilerplate-heavy

  • ❌ No lifecycle awareness (e.g. ViewModel vs singleton)

  • ❌ Not scalable

  • ❌ Harder to maintain and test in big teams

🔹 Overcome limitations with Koin or Dagger Hilt

✅ With Koin (Kotlin-first, lightweight)

val appModule = module {
    single { UserRepository() }
    viewModel { UserViewModel(get()) }
}

With Dagger Hilt

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
    @Provides fun provideRepository() = UserRepository()
}

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    @Inject lateinit var viewModel: UserViewModel
}

🔹 Conclusion

Dependency Injection isn’t just “clean code.” It’s a must-have practice for building modular, testable, and maintainable Android apps.
With tools like Koin or Hilt, it becomes easier than ever to adopt DI in your architecture — no excuses!

If you want to build solid, scalable Android apps, DI is the way to go.

1
Subscribe to my newsletter

Read articles from nathan kayumba directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

nathan kayumba
nathan kayumba

I'm a passionate software engineer with a strong focus on backend development and mobile app creation. I specialize in building native Android applications using Kotlin and Jetpack Compose. With a background in software engineering, I enjoy solving complex problems and creating seamless user experiences. When I'm not coding, you'll find me immersed in music, constantly seeking inspiration from new sounds. Let's build the future, one app at a time!