Skip to content

Instantly share code, notes, and snippets.

@baconator
Created October 9, 2016 17:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baconator/88777c371e8b7a8469e39268414e4f13 to your computer and use it in GitHub Desktop.
Save baconator/88777c371e8b7a8469e39268414e4f13 to your computer and use it in GitHub Desktop.
Minimal Kotlin + Dagger 2 in IntelliJ Example
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/")
}
}
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
}
@Kolyall
Copy link

Kolyall commented Jan 23, 2020

Doesnt work on new versions

@Kolyall
Copy link

Kolyall commented Jan 23, 2020

kotlin_version = '1.3.61'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment