Skip to content

Commit

Permalink
Implement hilt dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Drjacky committed Aug 4, 2020
1 parent 4edf9ef commit b8af89b
Show file tree
Hide file tree
Showing 30 changed files with 97 additions and 286 deletions.
22 changes: 13 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'kotlin-kapt'

android {
Expand All @@ -17,7 +18,7 @@ android {
versionCode rootProject.ext.versionCode
versionName rootProject.ext.versionName
testInstrumentationRunner rootProject.ext.testInstrumentationRunner
javaCompileOptions.annotationProcessorOptions.arguments =
javaCompileOptions.annotationProcessorOptions.arguments +=
["room.schemaLocation": "$projectDir/schemas".toString()]
}
sourceSets {
Expand Down Expand Up @@ -45,6 +46,9 @@ android {
targetCompatibility 1.8
sourceCompatibility 1.8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
lintOptions.abortOnError = false
testOptions.unitTests.returnDefaultValues = true
packagingOptions {
Expand All @@ -65,15 +69,15 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
implementation "androidx.core:core-ktx:$coreKtxVersion"
implementation "androidx.multidex:multidex:$multidexVersion"
implementation "androidx.fragment:fragment-ktx:$fragmentExtVersion"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycleVersion"
//dependency injection
implementation "com.google.dagger:dagger:$daggerVersion"
implementation "com.google.dagger:dagger-android-support:$daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$daggerVersion"
kapt "com.google.dagger:dagger-compiler:$daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$daggerVersion"
kapt "com.google.dagger:dagger-compiler:$daggerVersion"
api "com.google.dagger:dagger-android:$daggerVersion"
api "com.google.dagger:dagger-android-support:$daggerVersion"
implementation "com.google.dagger:hilt-android:$hiltVersion"
kapt "com.google.dagger:hilt-android-compiler:$hiltVersion"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hiltExtVersion"
kapt "androidx.hilt:hilt-compiler:$hiltExtVersion"
kapt "com.google.dagger:hilt-android-compiler:$hiltVersion"
implementation "javax.inject:javax.inject:$javaxInjectVersion"
//network
implementation "com.squareup.retrofit2:retrofit:$retrofit2Version"
implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofit2Version"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
package app.web.drjackycv.mvvmtemplate.application

import android.content.Context
import androidx.multidex.MultiDex
import app.web.drjackycv.mvvmtemplate.di.component.DaggerAppComponent
import dagger.android.AndroidInjector
import dagger.android.support.DaggerApplication
import androidx.multidex.MultiDexApplication
import dagger.hilt.android.HiltAndroidApp
import timber.log.Timber

class MVVMTemplateApplication : DaggerApplication() {
@HiltAndroidApp
class MVVMTemplateApplication : MultiDexApplication() {

override fun onCreate() {
super.onCreate()
Timber.plant(Timber.DebugTree())
}

override fun applicationInjector(): AndroidInjector<out DaggerApplication> =
DaggerAppComponent.builder().application(this).build()

override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
MultiDex.install(this)
}

}

This file was deleted.

This file was deleted.

3 changes: 3 additions & 0 deletions app/src/main/kotlin/app/web/drjackycv/mvvmtemplate/di/module/ApiModule.kt
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package app.web.drjackycv.mvvmtemplate.di.module
import app.web.drjackycv.data.products.remote.ProductsApi
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import retrofit2.Retrofit

@InstallIn(ApplicationComponent::class)
@Module
class ApiModule {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
package app.web.drjackycv.mvvmtemplate.di.module

import android.app.Application
import android.content.Context
import android.content.res.Resources
import app.web.drjackycv.mvvmtemplate.application.MVVMTemplateApplication
import app.web.drjackycv.data.network.BaseHttpClient
import app.web.drjackycv.data.network.BaseRetrofit
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import javax.inject.Singleton

@InstallIn(ApplicationComponent::class)
@Module
class AppModule {

@Provides
@Singleton
fun provideApplication(application: MVVMTemplateApplication): Application = application
fun resources(application: Application): Resources = application.resources

@Provides
@Singleton
fun provideContext(application: Application): Context = application
fun gson(): Gson = GsonBuilder().create()

@Provides
@Singleton
fun resources(application: Application): Resources = application.resources
fun okHttpClient(baseHttpClient: BaseHttpClient): OkHttpClient = baseHttpClient.okHttpClient

@Provides
@Singleton
fun retrofit(baseRetrofit: BaseRetrofit): Retrofit = baseRetrofit.retrofit

}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import app.web.drjackycv.data.products.repository.ProductsRepositoryImpl
import app.web.drjackycv.domain.products.repository.ProductsRepository
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import java.util.concurrent.Executors

@InstallIn(ApplicationComponent::class)
@Module
class RepositoryModule {

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ buildscript {
ext {
kotlinVersion = '1.3.72'
navigationVersion = '2.3.0-alpha05'
hiltVersion = '2.28.3-alpha'
}
repositories {
google()
Expand All @@ -15,6 +16,7 @@ buildscript {
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlinVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigationVersion"
classpath "com.google.dagger:hilt-android-gradle-plugin:$hiltVersion"
}
}

Expand Down
8 changes: 4 additions & 4 deletions data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ android {
targetCompatibility 1.8
sourceCompatibility 1.8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
test.java.srcDirs += 'src/test/kotlin'
Expand All @@ -48,10 +51,7 @@ dependencies {
implementation "androidx.paging:paging-rxjava2:$pagingVersion"
implementation "androidx.multidex:multidex:$multidexVersion"
//dependency injection
implementation "com.google.dagger:dagger:$daggerVersion"
implementation "com.google.dagger:dagger-android-support:$daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$daggerVersion"
kapt "com.google.dagger:dagger-compiler:$daggerVersion"
implementation "javax.inject:javax.inject:$javaxInjectVersion"
//parser
api "com.squareup.retrofit2:converter-gson:$gsonVersion"
//network
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Singleton

private const val TIMEOUT = 5L

@Singleton
class BaseHttpClient @Inject constructor() {

val okHttpClient = OkHttpClient()
Expand Down
Loading

0 comments on commit b8af89b

Please sign in to comment.