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

+13
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

+11
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

+1
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"
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
}
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+
}
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+
}
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+
}
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+
}
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+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.ysfcyln.daggermultimodule.di.main
2+
3+
interface MainComponentProvider {
4+
fun provideMainComponent(): MainComponent
5+
}

app/src/main/res/layout/activity_main.xml

+18-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,28 @@
66
android:layout_height="match_parent"
77
tools:context=".MainActivity">
88

9-
<TextView
9+
<Button
10+
android:id="@+id/btnFeatureOne"
1011
android:layout_width="wrap_content"
1112
android:layout_height="wrap_content"
12-
android:text="Hello World!"
13+
android:text="Open Feature One!"
1314
app:layout_constraintBottom_toBottomOf="parent"
1415
app:layout_constraintLeft_toLeftOf="parent"
1516
app:layout_constraintRight_toRightOf="parent"
16-
app:layout_constraintTop_toTopOf="parent" />
17+
app:layout_constraintTop_toTopOf="parent"
18+
android:textSize="14sp"
19+
android:textColor="@android:color/black"/>
20+
21+
<Button
22+
android:id="@+id/btnFeatureTwo"
23+
android:layout_width="wrap_content"
24+
android:layout_height="wrap_content"
25+
android:text="Open Feature Two!"
26+
app:layout_constraintLeft_toLeftOf="parent"
27+
app:layout_constraintRight_toRightOf="parent"
28+
app:layout_constraintTop_toBottomOf="@+id/btnFeatureOne"
29+
android:textSize="14sp"
30+
android:textColor="@android:color/black"
31+
android:layout_marginTop="16dp"/>
1732

1833
</androidx.constraintlayout.widget.ConstraintLayout>

base/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

base/build.gradle

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
apply plugin: 'com.android.library'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
4+
apply plugin: 'kotlin-kapt'
5+
6+
android {
7+
compileSdkVersion 29
8+
buildToolsVersion "29.0.2"
9+
10+
defaultConfig {
11+
minSdkVersion 21
12+
targetSdkVersion 29
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
consumerProguardFiles 'consumer-rules.pro'
18+
}
19+
20+
buildTypes {
21+
release {
22+
minifyEnabled false
23+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24+
}
25+
}
26+
27+
}
28+
29+
dependencies {
30+
implementation fileTree(dir: 'libs', include: ['*.jar'])
31+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
32+
implementation 'androidx.appcompat:appcompat:1.1.0'
33+
implementation 'androidx.core:core-ktx:1.2.0'
34+
testImplementation 'junit:junit:4.12'
35+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
36+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
37+
38+
// Dagger dependencies
39+
implementation "com.google.dagger:dagger:$daggerVersion"
40+
kapt "com.google.dagger:dagger-compiler:$daggerVersion"
41+
}

base/consumer-rules.pro

Whitespace-only changes.

base/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.ysfcyln.base
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.ysfcyln.base.test", appContext.packageName)
23+
}
24+
}

base/src/main/AndroidManifest.xml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.ysfcyln.base" />

0 commit comments

Comments
 (0)