Requires a pro subscription Pro

Dependency Injection with Koin

Learn how to get started with the Dependency Injection framework Koin and how it can make developing CLEAN code and testing easier. By Kevin D Moore.

Login to leave a rating/review
Save for later
Share

Who is this for?

This course is for intermediate to advanced users who are interested in using Dependency Injection in their project or that want to replace Dagger 2. The user should be familiar with Android Studio and the Android Framework.

Covered concepts

  • Dependency Injection
  • Adding Koin as a dependency
  • Creating Modules
  • Starting Koin
  • KoinComponent
  • Using a network module with Retrofit

Part 1: Dependency Injection

1
Toggle description

What is Dependency Injection, and why would we want to use it? In this episode, learn the principles of Dependency Injection, Inversion of Control and testing.

Toggle description

Find out about other popular Dependency Injection solutions, notibly Dagger, and learn why Koin is a better solution.

Toggle description

Learn what Koin is, how it works, and the basic injection method: modules. Learn about the Domain Specific language that Koin uses.

Toggle description

Learn about the sample app, which uses a NASA API to retrieve a daily photo and description and inject resources into your app.

Toggle description

Learn about the NASA API for retrieving the daily photo and description and find out how to obtain your key.

Toggle description

Add the Koin libraries and learn about the existing libraries needed to run the app.

Part 2: Create a Network Module

Toggle description

Learn about Singles and Factories, then create a module with the Single & Factory DSL calls.

Toggle description

Add the base url needed for the NASA API. This will be the starting point for injection calls.

Toggle description

Create a logging interceptor to see the requests and responses from the API. This will be added to OkHttpClient.

Toggle description

Create the OkHttpClient class with the builder, set network timeouts, and add a logging interceptor.

Toggle description

Create an instance of Moshi with its builder, which is used to convert from JSON to the model classes.

Toggle description

Create an instance of retrofit using the components created earlier. Add the Base Url, Moshi and OkHttpClient.

Toggle description

Create an instance of our NASA API Interface. This is the class that will be returning the photos and descriptions.

Part 3: Activity Problem

Toggle description

Since the system creates Activities, this episode shows how to add a special class that allows Koin to inject Activities.

Toggle description

Create an AppModule with the DefaultCurrentActivityListener and ActivityRetriever used to inject Activities.

Part 4: Space Daily

Start Koin 2:05
Toggle description

Learn how to start Koin: adding logging, a context and modules. Then learn to register activity lifecycle callbacks for injecting Activities.

Toggle description

Add the NASAAPIInterface to the MainViewModel and call getDailyPhoto to get the photo and give it to the view.

Toggle description

Create DailyPhotoView for showing the photo and description, and retrieve the ViewModel using our Activity injector.

Part 5: Testing

Toggle description

Create a test to test the Koin modules, implementing the KoinTest interface. This will test that our modules are defined correctly.

Toggle description

Set up tests by using the @before and @After annotations to start and stop Koin, and use Koin to inject test fields.

Toggle description

Create tests to make sure all the network singles were correctly created. This will make sure each class was created and set up properly.