Now In Android with Koin — half 1 | by Arnaud Giuliani | Dec, 2022


This 12 months I used to be talking at a number of Android conferences, about Android structure design with Koin (Silicon Brighton) and concerning the new Kotlin developer expertise since Koin 3.2 (Droicon Berlin and Kotlin Dev Day Amsterdam).

On the identical time, the Google Android staff was closely engaged on gathering greatest practices for the group, associated to the newest improvement applied sciences corresponding to Jetpack Compose, Materials Design Kotlin Coroutines, and different applied sciences requirements to construct Android purposes in 2022.

Now In Android is an open-source Android utility that covers these greatest practices and retains them updated. The app is maintained by the Android staff. An amazing due to them for such assets!

I suggest right now to take a particular tour of Now In Android: a model constructed with the Koin dependency injection framework. It is a good time to refresh practices with Koin and Trendy Android Growth, from normal elements construction to extra superior instances.

You could find all of the associated sources at this location: https://github.com/InsertKoinIO/nowinandroid

Koin is a very talked-about Kotlin dependency injection framework (https://insert-koin.io/), well-known for its ease of use and its capability to convey elegant and highly effective options due to the Kotlin language.

In Android, Koin comes magically prepared to make use of out of the field due to the Kotlin extensions mechanism. This implies you can simply use Koin features straight from any Android half.

The Now In Android (aka Nia) utility is an unlimited instance to play with. Nice content material, however the place to begin? 🤔

Options image from https://github.com/android/nowinandroid

The venture proposes an outline of the prevailing modules. Chances are you’ll check out the modularization doc. Here’s a fast preview of the group of the present modules:

Module group — https://github.com/android/nowinandroid

We are able to discover the venture on the next elements:

  • App Module — the primary module and utility entry level
  • Widespread modules — gathering all widespread elements (database, repository, community, area …)
  • Characteristic modules — implementing every half/display screen of the app
  • Sync module — devoted to information resyncing

First, let’s setup every thing. We use the next Koin dependencies within the venture:

  • koin-android — Android options (widespread android elements)
  • koin-androidx-compose — options associated to Jetpack Compose (characteristic module)
  • koin-androidx-workmanager — WorkManager options (widespread information sync module)
  • koin-core — pure Kotlin elements (for Kotlin solely widespread module)
  • koin-test — verification and testing elements

For this articles sequence, we’ll use the newest Koin 3.3 variations. Test the setup web page for extra particulars: https://insert-koin.io/docs/setup/koin

The Gradle configuration has been up to date to have the ability to use Koin bundle straight with the model catalog already established. Test the libs.variations.toml file about Koin artifacts.

The AndroidFeatureConventionPlugin file can also be up to date to convey koin-androidx-compose for every characteristic module.

Let’s open the NiaApplication class, the appliance entry level, to see the onCreate technique written to begin Koin with startKoinoperate:

Beginning Koin

We use a number of choices:

  • androidLogger — allow Koin logging on an Android
  • androidContext— reference the Android Software context
  • workManagerFactory— begin WorkManager elements declared in Koin

We use the modules() operate to load Koin modules. Right here we’ll load the niaAppModule.

The decision toSync.initialize() will init the WorkManager to resync information. We’ll have a look at these particulars later.

observe: androidLogger is logging in INFO degree by default. You may select the DEBUG degree to have extra full data if you could examine round Koin.

The very first thing we want is one primary module that may embrace all different sub-modules. The NiaAppModule.kt file declares the primary Koin module as observe:

The primary Koin module

The consists of operate makes use of a checklist of modules to load with the present module. This additionally helps Koin flatten all of your module graphs and optimizes your utility startup.

Beneath we declare MainActivityViewModel as a ViewModel in Koin, with the viewModelOf() operate. This operate targets straight a category constructor to construct.

viewModelOf(::MainActivityViewModel)

For this reason now we have using :: characters, to establish the constructor of the MainActivityViewModel class.

We suggest utilizing consists of() operate to make sure gathering all app modules into the primary one. Greater than serving to manage modules and sub-modules, the consists of operate will allow you to confirm globally your Koin configuration. Let’s verify under.

One new necessary characteristic of Koin launched lately with Koin 3.3, is the capability to confirm a Koin configuration in a couple of milliseconds with only a JUnit take a look at. The koin-test Gradle bundle is required to have entry to such testing options.

How does it work? Use the confirm() extension operate on a Koin Module. That’s it! Underneath the hood, This may confirm all constructor lessons and crosscheck with the Koin configuration to know if there’s a part declared for this dependency. In case of failure, the operate will throw a MissingKoinDefinitionException.

Let’s see the NiaAppModuleCheck.kt file:

confirm() on the primary Koin module

Launch the JUnit take a look at and also you’re executed! ✅

As you might even see, we use the extraTypesparameter to checklist varieties used within the Koin configuration however not declared straight. That is the case for SavedStateHandle and WorkerParameters varieties, which can be used as injected parameters. The Context is said by androidContext() operate at begin.

The confirm() API is ultra-light to run and doesn’t require any sort of mock/stubb to run in your configuration.

Let’s proceed our exploration of the venture, nonetheless within the app module. There’s a small module that allow you to construct JankStats situations for an Exercise: the JanksStatsModule .

Beneath is the unique dagger model:

JankStatsModule in Dagger model

The concept is to create a JankStats object to allow you to allow/disable efficiency monitoring in an Exercise:

Lazy Dagger Property for JankStats
Utilizing lazyStats

Within the Koin model, we might write this half with a easy definition. Let’s open the JankStatsKoinModule file:

JankStats Koin module

Right here now we have a manufacturing unit definition, that’s constructing the JankStats object occasion from an incoming Exercise.

How can we go an Exercise to a definition? We use injected parameters to declare that we’ll use an Exercise as a parameter:

manufacturing unit { (exercise: Exercise) -> JankStats.createAndTrack(exercise.window, createOnFrameListener()) }

We use a operate (lambda block behind manufacturing unit key phrase), to jot down a Kotlin operate that may construct our part.

From our MainActivity, we merely must declare JankStats with a name to injectoperate as observe:

val lazyStats: JankStats by inject { parametersOf(this) }

The parametersOf expression passes arguments to your Koin definition.

Lazy inject in Exercise with Koin & injected parameters

The lazyStats property is a actual Kotlin lazy kind and can be utilized straight used:

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles