android blog kotlin

Building a podcast app series: 5. Dependency injection

Koin to the rescue

I am a big fan of dependency injection, I think no matter how small or big your application is, some sort of dependency injection is a must. Luckily there are multiple options to choose from when it comes to DI frameworks and my personal favorite is Koin.

If you are new to Koin or dependency injection, please check out my other blog series on Koin here.

In the context of our podcast app, Koin is responsible for creating and maintaining a single instance of ExoPlayer among other things, in fact, every object that we create and need to share its instance we will utilize Koin.

So how does Koin provide our ExoPlayer instance across the app? We will create an instance of ExoPlayer and wrap it around a single {} keyword (which is basically an infix extension function).

  single<ExoPlayer> {
    SimpleExoPlayer.Builder(androidContext()).build().apply {
                    .setUsage(C.USAGE_MEDIA).build(), true

single is a special Koin function that will make sure we are always injecting the same ExoPlayer instance in our app.

Now to inject this ExoPlayer instance we can use Kon’s inject or get extension methods to use lazy or eager injection. Here is the link to two Koin module files where we create all dependencies of our podcast app

With this its time to finish up our Building a podcast app blog series, I’ll see you again soon :).