Skip to content

Commit

Permalink
Dagger implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick committed Apr 10, 2019
1 parent f5ec67d commit fda3d1a
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .buckconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
app = //app:bin_debug
4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ dependencies {
implementation(deps.android.androidx.corektx)
implementation(deps.android.androidx.constraintLayout)


implementation(deps.dagger.core)
kapt(deps.kapt.dagger)

// Test
testImplementation(deps.test.junit)
}
22 changes: 22 additions & 0 deletions app/src/main/kotlin/com/nickfirmani/template/AppComponent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.nickfirmani.template

import android.app.Application
import dagger.BindsInstance
import dagger.Component
import javax.inject.Singleton

@Singleton
@Component(
modules = arrayOf(
AppModule::class
)
)
interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application): Builder

fun build(): AppComponent
}
}
17 changes: 17 additions & 0 deletions app/src/main/kotlin/com/nickfirmani/template/AppModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.nickfirmani.template

import android.app.Application
import android.content.Context
import com.nickfirmani.template.annotations.AppContext
import dagger.Binds
import dagger.Module
import javax.inject.Singleton

@Module
abstract class AppModule {

@Binds
@AppContext
@Singleton
abstract fun provideApplicationContext(application: Application): Context
}
10 changes: 5 additions & 5 deletions app/src/main/kotlin/com/nickfirmani/template/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.nickfirmani.template

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.nickfirmani.template.annotations

import javax.inject.Qualifier

@Qualifier
annotation class AppContext
20 changes: 20 additions & 0 deletions app/src/main/kotlin/javax/annotation/Generated.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package javax.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

// TODO figure out why Buck can't seem to find this file.
// https://github.com/google/dagger/issues/1449 ?
@Documented
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.PACKAGE, ElementType.TYPE, ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.LOCAL_VARIABLE, ElementType.PARAMETER})
public @interface Generated {
String[] value();

String date() default "";

String comments() default "";
}
6 changes: 6 additions & 0 deletions apply_okbuck.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ okbuck {
"androidx.*"
]
}
externalDependencies {
downloadInBuck = false
}
wrapper {
watch += ["**/*/build.gradle.kts", "**/dependencies.kt"]
}
}
3 changes: 2 additions & 1 deletion buckw
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ getChanges ( ) {
["type", "f"],
["anyof",
["suffix", "gradle"],
["name", ["gradle-wrapper.properties"]]
["name", ["gradle-wrapper.properties", "dependencies.kt"]],
["match", "**/*/build.gradle.kts", "wholename"]
],
["not",
["dirname", ".okbuck"]
Expand Down
3 changes: 1 addition & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ allprojects {
}
}

apply(from = "apply_okbuck.gradle" )

apply(from = "apply_okbuck.gradle")
9 changes: 9 additions & 0 deletions buildSrc/src/main/kotlin/dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
object deps {
object versions {
const val kotlin = "1.3.21"
const val dagger = "2.22"
}

object android {
Expand Down Expand Up @@ -30,6 +31,14 @@ object deps {
}
}

object dagger {
const val core = "com.google.dagger:dagger:${versions.dagger}"
}

object kapt {
const val dagger = "com.google.dagger:dagger-compiler:${versions.dagger}"
}

object test {
const val junit = "junit:junit:4.12"
}
Expand Down
35 changes: 18 additions & 17 deletions gradle/config-kotlin-sources.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,27 @@
*/

if (project.plugins.hasPlugin("kotlin")
|| project.plugins.hasPlugin("org.jetbrains.kotlin.kapt")
|| project.plugins.hasPlugin("org.jetbrains.kotlin.android")
|| project.plugins.hasPlugin("org.jetbrains.kotlin.jvm")
|| project.plugins.hasPlugin("kotlin-android")
|| project.plugins.hasPlugin("kotlin-kapt")) {
|| project.plugins.hasPlugin("org.jetbrains.kotlin.kapt")
|| project.plugins.hasPlugin("org.jetbrains.kotlin.android")
|| project.plugins.hasPlugin("org.jetbrains.kotlin.jvm")
|| project.plugins.hasPlugin("kotlin-android")
|| project.plugins.hasPlugin("kotlin-kapt")) {
if (project.plugins.hasPlugin("com.android.application")
|| project.plugins.hasPlugin("com.android.library")) {
|| project.plugins.hasPlugin("com.android.library")) {
project.android.sourceSets {
main.java.srcDirs += 'src/main/kotlin'
debug.java.srcDirs += 'src/debug/kotlin'
release.java.srcDirs += 'src/release/kotlin'
test.java.srcDirs += 'src/test/kotlin'

// TODO put this back?
// For kapt stubs
main.java.srcDirs += [file("$buildDir/generated/source/kapt/main")]
debug.java.srcDirs += [file("$buildDir/generated/source/kapt/debug")]
release.java.srcDirs += [file("$buildDir/generated/source/kapt/release")]
test.java.srcDirs += [file("$buildDir/generated/source/kapt/test")]
// main.java.srcDirs += [file("$buildDir/generated/source/kapt/main")]
// debug.java.srcDirs += [file("$buildDir/generated/source/kapt/debug")]
// release.java.srcDirs += [file("$buildDir/generated/source/kapt/release")]
// test.java.srcDirs += [file("$buildDir/generated/source/kapt/test")]

// For kotlin code gen during kapt
// For kotlin code gen during kapt
main.java.srcDirs += [file("$buildDir/generated/source/kaptKotlin/main")]
debug.java.srcDirs += [file("$buildDir/generated/source/kaptKotlin/debug")]
release.java.srcDirs += [file("$buildDir/generated/source/kaptKotlin/release")]
Expand All @@ -46,13 +47,13 @@ if (project.plugins.hasPlugin("kotlin")
idea {
module {
sourceDirs += files(
'build/generated/source/kapt/main',
'build/generated/source/kaptKotlin/main',
'build/tmp/kapt/main/kotlinGenerated')
'build/generated/source/kapt/main',
'build/generated/source/kaptKotlin/main',
'build/tmp/kapt/main/kotlinGenerated')
generatedSourceDirs += files(
'build/generated/source/kapt/main',
'build/generated/source/kaptKotlin/main',
'build/tmp/kapt/main/kotlinGenerated')
'build/generated/source/kapt/main',
'build/generated/source/kaptKotlin/main',
'build/tmp/kapt/main/kotlinGenerated')
}
}
}
Expand Down

0 comments on commit fda3d1a

Please sign in to comment.