Skip to content

Commit

Permalink
multi-module dagger
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Holgado committed Dec 15, 2018
1 parent 34efd06 commit 9ec7be8
Show file tree
Hide file tree
Showing 29 changed files with 385 additions and 38 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ android {
}

dependencies {
implementation project(path: ':core')
implementation project(path: ':mymodule')

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:name="com.marcosholgado.MyApplication"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
Expand All @@ -16,6 +17,8 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name="com.marcosholgado.mymodule.OtherActivity" />
</application>

</manifest>
20 changes: 20 additions & 0 deletions app/src/main/java/com/marcosholgado/MyApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.marcosholgado

import android.app.Application
import com.marcosholgado.core.di.CoreComponentProvider
import com.marcosholgado.daggerplayground.di.CoreComponent
import com.marcosholgado.daggerplayground.di.DaggerCoreComponent

class MyApplication : Application(), CoreComponentProvider {

private lateinit var coreComponent: CoreComponent

override fun provideCoreComponent(): CoreComponent {
if (!this::coreComponent.isInitialized) {
coreComponent = DaggerCoreComponent
.builder()
.build()
}
return coreComponent
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
package com.marcosholgado.daggerplayground

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.marcosholgado.daggerplayground.di.DaggerMainComponent
import com.marcosholgado.daggerplayground.di.MyModule
import dagger.Lazy
import com.marcosholgado.core.ExpensiveObject
import com.marcosholgado.core.di.CoreInjectHelper
import com.marcosholgado.daggerplayground.di.DaggerFeature2Component
import com.marcosholgado.mymodule.OtherActivity
import kotlinx.android.synthetic.main.activity_main.*
import javax.inject.Inject

class MainActivity : AppCompatActivity() {

@Inject
lateinit var expensiveObject: Lazy<ExpensiveObject>
lateinit var expensiveObject: ExpensiveObject

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

DaggerMainComponent.builder().myModule(MyModule()).build().inject(this)
DaggerFeature2Component
.builder()
.coreComponent(CoreInjectHelper.provideCoreComponent(applicationContext))
.build()
.inject(this)

button.setOnClickListener {
textView.text = "Size is ${expensiveObject.get().intArray?.size}"
startActivity(Intent(this, OtherActivity::class.java))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.marcosholgado.daggerplayground.di

import com.marcosholgado.core.di.FeatureScope
import com.marcosholgado.daggerplayground.MainActivity
import dagger.Component

@Component(modules = [Feature2Module::class], dependencies = [CoreComponent::class])
@FeatureScope
interface Feature2Component {
fun inject(mainActivity: MainActivity)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.marcosholgado.daggerplayground.di

import dagger.Module
import dagger.Provides

@Module
class Feature2Module {
@Provides
fun provideInt() = 1
}

This file was deleted.

1 change: 1 addition & 0 deletions core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
72 changes: 72 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 28

defaultConfig {
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"


}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.core:core-ktx:1.0.0'

// Dagger
implementation "com.google.dagger:dagger:2.19"
implementation "com.google.dagger:dagger-android:2.19"
implementation "com.google.dagger:dagger-android-support:2.19"

kapt "com.google.dagger:dagger-compiler:2.19"
kapt "com.google.dagger:dagger-android-processor:2.19"

testImplementation 'junit:junit:4.12'

// Core library
androidTestImplementation 'androidx.test:core:1.0.0'
androidTestUtil 'androidx.test:orchestrator:1.1.0'

// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'

// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test.ext:truth:1.0.0'
androidTestImplementation 'com.google.truth:truth:0.42'

// Espresso dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-web:3.1.0'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.1.0'
androidTestImplementation 'androidx.test.janktesthelper:janktesthelper:1.0.1'
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'

androidTestImplementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
androidTestImplementation 'io.reactivex.rxjava2:rxjava:2.1.10'
}
21 changes: 21 additions & 0 deletions core/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
2 changes: 2 additions & 0 deletions core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.marcosholgado.core" />
3 changes: 3 additions & 0 deletions core/src/main/java/com/marcosholgado/core/ExpensiveObject.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.marcosholgado.core

class ExpensiveObject
11 changes: 11 additions & 0 deletions core/src/main/java/com/marcosholgado/core/di/CoreComponent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.marcosholgado.daggerplayground.di

import com.marcosholgado.core.ExpensiveObject
import dagger.Component
import javax.inject.Singleton

@Component(modules = [CoreModule::class])
@Singleton
interface CoreComponent {
fun getExpensiveObject(): ExpensiveObject
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.marcosholgado.core.di

import com.marcosholgado.daggerplayground.di.CoreComponent

interface CoreComponentProvider {
fun provideCoreComponent(): CoreComponent
}
14 changes: 14 additions & 0 deletions core/src/main/java/com/marcosholgado/core/di/CoreInjectHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.marcosholgado.core.di

import android.content.Context
import com.marcosholgado.daggerplayground.di.CoreComponent

object CoreInjectHelper {
fun provideCoreComponent(applicationContext: Context): CoreComponent {
return if (applicationContext is CoreComponentProvider) {
(applicationContext as CoreComponentProvider).provideCoreComponent()
} else {
throw IllegalStateException("The application context you have passed does not implement CoreComponentProvider")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.marcosholgado.daggerplayground.di

import com.marcosholgado.daggerplayground.ExpensiveObject
import com.marcosholgado.core.ExpensiveObject
import dagger.Module
import dagger.Provides
import javax.inject.Singleton

@Module
class MyModule {

class CoreModule {
@Provides
@Singleton
fun provideExpensiveObject(): ExpensiveObject = ExpensiveObject()

}
7 changes: 7 additions & 0 deletions core/src/main/java/com/marcosholgado/core/di/FeatureScope.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.marcosholgado.core.di

import javax.inject.Scope

@Scope
@Retention
annotation class FeatureScope
3 changes: 3 additions & 0 deletions core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">core</string>
</resources>
1 change: 1 addition & 0 deletions mymodule/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
73 changes: 73 additions & 0 deletions mymodule/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 28

defaultConfig {
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation project(path: ':core')

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.core:core-ktx:1.0.0'

// Dagger
implementation "com.google.dagger:dagger:2.19"
implementation "com.google.dagger:dagger-android:2.19"
implementation "com.google.dagger:dagger-android-support:2.19"

kapt "com.google.dagger:dagger-compiler:2.19"
kapt "com.google.dagger:dagger-android-processor:2.19"

testImplementation 'junit:junit:4.12'

// Core library
androidTestImplementation 'androidx.test:core:1.0.0'
androidTestUtil 'androidx.test:orchestrator:1.1.0'

// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'

// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test.ext:truth:1.0.0'
androidTestImplementation 'com.google.truth:truth:0.42'

// Espresso dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-web:3.1.0'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.1.0'
androidTestImplementation 'androidx.test.janktesthelper:janktesthelper:1.0.1'
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'

androidTestImplementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
androidTestImplementation 'io.reactivex.rxjava2:rxjava:2.1.10'
}
Loading

0 comments on commit 9ec7be8

Please sign in to comment.