Dagger 2 for Dummies in Kotlin (with 20 lines of code)

Elye
Elye
Feb 8, 2018 · 10 min read
Image for post
Image for post

Let’s go to (very) basic programming (… in Android)

class MainActivity : AppCompatActivity() {

val info = Info()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

text_view.text = info.text

}
}

class Info {
val text = "Hello Dagger 2"
}

Moving Info out of Activity…

Image for post
Image for post
class MainActivity(val info:Info) : AppCompatActivity()

Based on a true story…

Making Info appear magically?

class MainActivity : AppCompatActivity() {

lateinit var info: Info
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

text_view.text = info.text

}
}

class Info {
val text = "Hello Dagger 2"
}

Adding Dagger 2 libraries

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// ... other dependencies ...
implementation 'com.google.dagger:dagger:2.13'
kapt 'com.google.dagger:dagger-compiler:2.13'
}

Back to real code… Introducing @Component

@Component
interface MagicBox
@Component
interface MagicBox {
fun poke(app: MainActivity)
}
Image for post
Image for post
Image for post
Image for post

Putting `Info` into @Component… Intro to @Inject

class Info  {
val text = "Hello Dagger 2"
}
class Info @Inject constructor() {
val text = "Hello Dagger 2"
}

Building the MagicBox… Another Dagger 2’s magic

DaggerMagicBox.create()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
DaggerMagicBox.create().poke(this)
text_view.text = info.text
}

@Inject again!!

lateinit var info: Info
@Inject lateinit var info: Info

World’s simplest Android App with Dagger 2 code

class MainActivity : AppCompatActivity() {

@Inject lateinit var info: Info

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
DaggerMagicBox.create().poke(this)
text_view.text = info.text
}
}

class Info @Inject constructor() {
val text = "Hello Dagger 2"
}

@Component
interface MagicBox {
fun poke(app: MainActivity)
}

Is this the end of the show?


This blog is tribute to keith smyth; the person who first told me about Dagger 2.

Mobile App Development Publication

Sharing Mobile App Development and Learning

Elye

Written by

Elye

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

Mobile App Development Publication

Sharing iOS, Android and relevant Mobile App Development Technology and Learning

Elye

Written by

Elye

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

Mobile App Development Publication

Sharing iOS, Android and relevant Mobile App Development Technology and Learning

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store