Skip to content

Commit 538d221

Browse files
authored
Merge pull request #1 from yusufceylan/development
development
2 parents c764335 + bed9906 commit 538d221

File tree

67 files changed

+1059
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1059
-4
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Dagger Multi Module Android
2+
Plain Dagger implementation in multi module project
3+
### Articles
4+
5+
- [Offical android dagger documentation](https://developer.android.com/training/dependency-injection/dagger-multi-module)
6+
- [Mindorks Multi-Module-Dagger article](https://blog.mindorks.com/dagger-in-a-multi-module-project)
7+
- [Step by step multi module dagger implementation](https://proandroiddev.com/android-multi-module-dagger-a-real-use-case-step-by-step-bbc03500f2f9)
8+
### Reference projects that I get inspired
9+
- [https://github.com/MindorksOpenSource/Dagger-Multi-Module-Android](https://github.com/MindorksOpenSource/Dagger-Multi-Module-Android)
10+
- [https://github.com/elye/demo_android_dagger_modules_setup](https://github.com/elye/demo_android_dagger_modules_setup)
11+
- [https://github.com/Ladgertha/Dagger-Multi-Modules](https://github.com/Ladgertha/Dagger-Multi-Modules)
12+
13+
Feel free to contribute

app/build.gradle

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
apply plugin: 'com.android.application'
22
apply plugin: 'kotlin-android'
33
apply plugin: 'kotlin-android-extensions'
4+
apply plugin: 'kotlin-kapt'
45

56
android {
67
compileSdkVersion 29
@@ -27,11 +28,21 @@ android {
2728

2829
dependencies {
2930
implementation fileTree(dir: 'libs', include: ['*.jar'])
31+
32+
implementation project(":base")
33+
implementation project(":feature-one")
34+
implementation project(":feature-two")
35+
implementation project(":feature-three")
36+
3037
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
3138
implementation 'androidx.appcompat:appcompat:1.1.0'
3239
implementation 'androidx.core:core-ktx:1.2.0'
3340
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
3441
testImplementation 'junit:junit:4.12'
3542
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
3643
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
44+
45+
// Dagger dependencies
46+
implementation "com.google.dagger:dagger:$daggerVersion"
47+
kapt "com.google.dagger:dagger-compiler:$daggerVersion"
3748
}

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package="com.ysfcyln.daggermultimodule">
44

55
<application
6+
android:name=".MyApp"
67
android:allowBackup="true"
78
android:icon="@mipmap/ic_launcher"
89
android:label="@string/app_name"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,55 @@
11
package com.ysfcyln.daggermultimodule
22

3+
import android.content.Intent
34
import androidx.appcompat.app.AppCompatActivity
45
import android.os.Bundle
6+
import android.util.Log
7+
import com.ysfcyln.base.DatabaseService
8+
import com.ysfcyln.base.NetworkService
9+
import com.ysfcyln.daggermultimodule.R
10+
import com.ysfcyln.daggermultimodule.di.main.MainComponentProvider
11+
import com.ysfcyln.feature_one.FeatureOneActivity
12+
import com.ysfcyln.feature_two.FeatureTwoActivity
13+
import kotlinx.android.synthetic.main.activity_main.*
14+
import javax.inject.Inject
515

616
class MainActivity : AppCompatActivity() {
717

18+
@Inject
19+
lateinit var databaseService: DatabaseService
20+
21+
@Inject
22+
lateinit var networkService: NetworkService
23+
24+
/**
25+
* Create component and inject
26+
*/
27+
private fun inject() {
28+
// When rotation happens component and its dependencies recreated :(
29+
val mainComponent = (application as MainComponentProvider).provideMainComponent()
30+
mainComponent.inject(this)
31+
}
32+
833
override fun onCreate(savedInstanceState: Bundle?) {
34+
inject()
935
super.onCreate(savedInstanceState)
1036
setContentView(R.layout.activity_main)
37+
Log.d("MainActivity", databaseService.toString())
38+
Log.d("MainActivity", networkService.toString())
39+
40+
clickListeners()
41+
}
42+
43+
/**
44+
* View click listeners
45+
*/
46+
private fun clickListeners() {
47+
btnFeatureOne.setOnClickListener {
48+
startActivity(Intent(this, FeatureOneActivity::class.java))
49+
}
50+
51+
btnFeatureTwo.setOnClickListener {
52+
startActivity(Intent(this, FeatureTwoActivity::class.java))
53+
}
1154
}
1255
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.ysfcyln.daggermultimodule
2+
3+
import android.app.Application
4+
import com.ysfcyln.daggermultimodule.di.DiProvider
5+
import com.ysfcyln.daggermultimodule.di.SubComponents
6+
7+
class MyApp : Application(), SubComponents {
8+
9+
override fun onCreate() {
10+
super.onCreate()
11+
DiProvider.buildDi(this)
12+
}
13+
14+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.ysfcyln.daggermultimodule.di
2+
3+
import android.app.Application
4+
import com.ysfcyln.base.BaseModule
5+
import com.ysfcyln.daggermultimodule.di.main.MainComponent
6+
import com.ysfcyln.feature_one.di.FeatureOneComponent
7+
import com.ysfcyln.feature_three.di.FeatureThreeComponent
8+
import com.ysfcyln.feature_two.di.FeatureTwoComponent
9+
import dagger.BindsInstance
10+
import dagger.Component
11+
import javax.inject.Singleton
12+
13+
@Singleton
14+
@Component(
15+
modules = [
16+
BaseModule::class,
17+
SubComponentsModule::class
18+
]
19+
)
20+
interface AppComponent {
21+
22+
/**
23+
* A {@see [Component.Factory]} that initializes necessary implementations
24+
*/
25+
@Component.Factory
26+
interface Factory {
27+
fun create(@BindsInstance application: Application): AppComponent
28+
}
29+
30+
// Save the reference of factories in the app component for creating sub components
31+
fun mainComponent() : MainComponent.Factory
32+
33+
// Save the reference of factories in the app component for creating sub components
34+
fun featureOneComponent() : FeatureOneComponent.Factory
35+
36+
// Save the reference of factories in the app component for creating sub components
37+
fun featureTwoComponent() : FeatureTwoComponent.Factory
38+
39+
// Save the reference of factories in the app component for creating sub components
40+
fun featureThreeComponent() : FeatureThreeComponent.Factory
41+
42+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.ysfcyln.daggermultimodule.di
2+
3+
import android.app.Application
4+
5+
object DiProvider {
6+
private lateinit var appComponent: AppComponent
7+
8+
@JvmStatic
9+
fun appComponent() = appComponent
10+
11+
fun buildDi(application: Application) {
12+
appComponent = DaggerAppComponent.factory().create(application)
13+
}
14+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.ysfcyln.daggermultimodule.di
2+
3+
import com.ysfcyln.daggermultimodule.di.main.MainComponent
4+
import com.ysfcyln.daggermultimodule.di.main.MainComponentProvider
5+
import com.ysfcyln.feature_one.di.FeatureOneComponent
6+
import com.ysfcyln.feature_one.di.FeatureOneComponentProvider
7+
import com.ysfcyln.feature_three.di.FeatureThreeComponent
8+
import com.ysfcyln.feature_three.di.FeatureThreeComponentProvider
9+
import com.ysfcyln.feature_two.di.FeatureTwoComponent
10+
import com.ysfcyln.feature_two.di.FeatureTwoComponentProvider
11+
12+
interface SubComponents: MainComponentProvider, FeatureOneComponentProvider, FeatureTwoComponentProvider,
13+
FeatureThreeComponentProvider {
14+
15+
override fun provideMainComponent(): MainComponent {
16+
return DiProvider.appComponent().mainComponent().create()
17+
}
18+
19+
override fun provideFeatureOneComponent(): FeatureOneComponent {
20+
return DiProvider.appComponent().featureOneComponent().create()
21+
}
22+
23+
override fun provideFeatureTwoComponent(): FeatureTwoComponent {
24+
return DiProvider.appComponent().featureTwoComponent().create()
25+
}
26+
27+
override fun provideFeatureThreeComponent(): FeatureThreeComponent {
28+
return DiProvider.appComponent().featureThreeComponent().create()
29+
}
30+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.ysfcyln.daggermultimodule.di
2+
3+
import com.ysfcyln.daggermultimodule.di.main.MainComponent
4+
import com.ysfcyln.feature_one.di.FeatureOneComponent
5+
import com.ysfcyln.feature_three.di.FeatureThreeComponent
6+
import com.ysfcyln.feature_two.di.FeatureTwoComponent
7+
import dagger.Module
8+
9+
/**
10+
* Associate SubComponents with AppComponent
11+
*/
12+
@Module(
13+
subcomponents = [
14+
MainComponent::class,
15+
FeatureOneComponent::class,
16+
FeatureTwoComponent::class,
17+
FeatureThreeComponent::class
18+
]
19+
)
20+
class SubComponentsModule {
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.ysfcyln.daggermultimodule.di.main
2+
3+
import com.ysfcyln.daggermultimodule.MainActivity
4+
import dagger.Subcomponent
5+
6+
@Subcomponent(
7+
modules = [
8+
// Bounded main activity necessary modules comes here
9+
]
10+
)
11+
interface MainComponent {
12+
13+
@Subcomponent.Factory
14+
interface Factory {
15+
fun create() : MainComponent
16+
}
17+
18+
fun inject(mainActivity: MainActivity) // Add main activity to Dagger graph
19+
20+
}

0 commit comments

Comments
 (0)