More Magical Android Dagger 2

Elye
Elye
Jan 10 · 2 min read

Dagger 2 seems magical to those who don’t know how it works. However to those who use it, we know it requires inject to get the needed dependencies injected in.

However for those wanted an even more magical feel, try out Android Dagger.

If you don’t know what is Dagger 2, refers to this blog.

The magical part of Android Dagger

Be ready to be impressed.

Imagine you have an Activity, you just need to first of all make it inherit from DaggerAppCompatActivity (or DaggerActivity if you’re not using Support Library).

Then don’t forget to have your Dependent object declared.

Code as below

class MainActivity : DaggerAppCompatActivity() {    @Inject lateinit var dependent : Dependent    override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

Then make a module for that will Provides your Dependent

@Module
class MainActivityModule {
@Provides fun getDependent() = Dependent()
}

That’s it!! Isn’t that magical!! 🎇 No Inject at all!! 😱

Sorry, I lied a bit… there’s some setup 😜

The above is really what’s needed in all your Activity’s code (or Fragment’s code).

But in order to make your Activity code so simple, a little setup needed on the Application side of code. Not too much, just three simple steps.

1. Define the AndroidInjectBuilder

For each of your Activity and Fragment, you need to define its ContributeAndroidInjector that link up your Activity and it’s Module.

@Module
abstract class AndroidInjectBuilder {
@ContributesAndroidInjector(
modules = [MainActivityModule::class])
abstract fun bindMainActivity(): MainActivity
}

2. Define your Application level Component

Now do your normal component definition with it’s Builder.

What’s different is, aside from aside from add your AndroidInjectBuilder::class to your component, you’ll need to add AndroidSupportInjectionModule::class to it (use AndroidInjectionModule::class if you’re not using Support Library).

@Component(modules = [
AndroidSupportInjectionModule::class,
AndroidInjectBuilder::class])
interface AppComponent: AndroidInjector<MainApplication> {
@Component.Factory
interface Factory: AndroidInjector.Factory<MainApplication>
}

3. Inherit Application from DaggerApplication

Finally, then extends your Application from DaggerApplication, and override the applicationInjector() function by returning the Component.

class MainApplication: DaggerApplication() {
override fun applicationInjector():
AndroidInjector<MainApplication> {
return DaggerAppComponent.factory().create(this)
}
}

That’s it!! Literally no inject at all!

Get the code from below. It has also example of Fragment as well as scoping within.

If you like to understand further Android Injector of how (from a more complex form) it comes to this simplified approach, do check out the 3 parts of Mert Şimşek’s Blog.

I hope this post is helpful to you. You could check out my other interesting topics here.

Follow me on medium, Twitter or Facebook for little tips and learning on Android, Kotlin etc related topics. ~Elye~

Elye

Written by

Elye

Learning and Sharing Android and iOS Development

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade