Created
October 9, 2016 17:25
-
-
Save baconator/88777c371e8b7a8469e39268414e4f13 to your computer and use it in GitHub Desktop.
Minimal Kotlin + Dagger 2 in IntelliJ Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version 'unspecified' | |
buildscript { | |
ext.kotlin_version = '1.1-M01' | |
ext.gradle_version = '2.7' | |
repositories { | |
mavenCentral() | |
maven { url "http://dl.bintray.com/kotlin/kotlin-eap-1.1" } | |
} | |
dependencies { | |
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" | |
} | |
} | |
apply plugin: 'idea' | |
apply plugin: 'kotlin' | |
repositories { | |
mavenCentral() | |
maven { url "http://dl.bintray.com/kotlin/kotlin-eap-1.1" } | |
maven { url "https://plugins.gradle.org/m2/" } | |
} | |
dependencies { | |
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | |
compile "com.google.dagger:dagger:$gradle_version" | |
kapt "com.google.dagger:dagger-compiler:$gradle_version" | |
} | |
kapt { | |
generateStubs = true | |
} | |
sourceSets.main.java.srcDir file("$buildDir/generated/source/kapt/") | |
idea { | |
module { | |
generatedSourceDirs += file("$buildDir/generated/source/kapt/") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dagger.Component | |
import dagger.Module | |
import dagger.Provides | |
import javax.inject.Inject | |
fun main(args: Array<String>) { | |
val shop = DaggerCoffeeShop.builder().dripCoffeeModule(DripCoffeeModule()).build() | |
shop.maker().makeCoffee() | |
} | |
class CoffeeMaker @Inject constructor(val heater: Heater, val pump: Pump) { | |
fun makeCoffee() { | |
heater.heatThings() | |
pump.pumpThings() | |
println("Making coffee ...") | |
} | |
} | |
interface Pump { | |
fun pumpThings() | |
} | |
interface Heater { | |
fun heatThings() | |
} | |
class Thermosiphon @Inject constructor(val heater: Heater) : Pump { | |
override fun pumpThings() { | |
heater.heatThings() | |
println("Thermosiphon pump action!") | |
} | |
} | |
class ElectricHeater : Heater { | |
override fun heatThings() = println("Electrically heat things!") | |
} | |
@Module | |
class DripCoffeeModule { | |
@Provides fun heater(): Heater = ElectricHeater() | |
@Provides fun pump(p: Thermosiphon): Pump = p | |
} | |
@Component(modules = arrayOf(DripCoffeeModule::class)) | |
interface CoffeeShop { | |
fun maker(): CoffeeMaker | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doesnt work on new versions