Skip to content

Commit

Permalink
Migrate Google Dagger-Hilt
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedeltaher committed Jan 31, 2021
2 parents 9c11eb0 + 3c0711a commit ae48815
Show file tree
Hide file tree
Showing 35 changed files with 162 additions and 428 deletions.
116 changes: 40 additions & 76 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-platform-android'
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'kotlin-kapt'
id 'kotlin-platform-android'
id 'dagger.hilt.android.plugin'
}


android {
compileSdkVersion rootProject.compileSdkVersion
buildToolsVersion '29.0.3'
buildToolsVersion rootProject.buildToolsVersion
defaultConfig {
applicationId 'com.eltaher.task'
minSdkVersion rootProject.minSdkVersion
targetSdkVersion rootProject.targetSdkVersion
versionCode 1
versionName "1.0"
multiDexEnabled true
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "com.task.CustomTestRunner"
}
buildFeatures{
buildFeatures {
viewBinding = true
}
buildTypes {
Expand All @@ -30,7 +33,6 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

flavorDimensions "default"
productFlavors {
dev {
Expand All @@ -45,24 +47,21 @@ android {
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_8
}
productFlavors {
}
defaultConfig {
testInstrumentationRunnerArguments clearPackageData: 'true'
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
}
packagingOptions {
pickFirst 'META-INF/kotlinx-io.kotlin_module'
pickFirst 'META-INF/atomicfu.kotlin_module'
pickFirst 'META-INF/kotlinx-coroutines-core.kotlin_module'
pickFirst 'META-INF/kotlinx-coroutines-io.kotlin_module'
configurations.all {
resolutionStrategy {
exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-debug"

}
}
defaultConfig {
testInstrumentationRunnerArguments clearPackageData: 'true'
}
testOptions {
unitTests.returnDefaultValues = true
}

android {
sourceSets {
String sharedTestDir = 'src/sharedTest/resources'
Expand All @@ -79,77 +78,42 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//kotlin
//implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "androidx.annotation:annotation:$annotationVersion"

implementation "com.squareup.picasso:picasso:$picassoVersion"
androidTestImplementation "androidx.test.ext:junit-ktx:$androidXJunitVersion"
/**-------------------testing libs------------------------------------------**/
//UI Testing , AndroidJUnitRunner and JUnit Rules &Espresso dependencies
//junit 5
testImplementation "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"

testImplementation "org.junit.platform:junit-platform-runner:$junitRunnerVersion"
androidTestImplementation "androidx.test.ext:junit:$androidXJunitVersion"
testImplementation "io.mockk:mockk:$mockVersion"
androidTestImplementation "org.assertj:assertj-core:$assertjVersion"
androidTestImplementation "androidx.arch.core:core-testing:$coreTestingVersion"
androidTestImplementation "androidx.test.espresso:espresso-intents:$espressoIntentsVersion"
androidTestImplementation ("androidx.test.espresso:espresso-core:$espressoCoreVersion",{
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation "androidx.test:rules:$testRulesVersion"
androidTestImplementation "androidx.test:runner:$runnerVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesTestVersion"
implementation "androidx.arch.core:core-testing:$coreTestingVersion"
implementation "androidx.test.espresso:espresso-idling-resource:$espressoVersion"
implementation "androidx.test.espresso:espresso-contrib:$espressoVersion"
implementation "androidx.arch.core:core-testing:$coreTestingVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesTestVersion"

/**-------------------------------------------------------------**/

//android support & recyclerview
implementation "androidx.recyclerview:recyclerview:$recyclerviewVersion"
//android support
implementation "androidx.appcompat:appcompat:$appcompatVersion"
implementation "com.google.android.material:material:$materialVersion"
implementation "androidx.core:core-ktx:$coreKtxVersion"
implementation "androidx.coordinatorlayout:coordinatorlayout:$coordinatorLayoutVersion"
implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"
implementation "androidx.cardview:cardview:$cardViewVersion"
implementation "androidx.activity:activity:$activityVersion"
implementation "androidx.activity:activity-ktx:$activityKtxVersion"

//Dagger
implementation "com.google.dagger:dagger:$daggerVersion"
kapt "com.google.dagger:dagger-compiler:$daggerVersion"
implementation "com.google.dagger:dagger-android:$daggerVersion"
implementation "com.google.dagger:dagger-android-support:$daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$daggerVersion"
kaptAndroidTest "com.google.dagger:dagger-compiler:$daggerVersion"

//Logging
implementation "com.squareup.okhttp3:logging-interceptor:$okhttpInterceptorVersion"

// retrofit
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
implementation "com.squareup.retrofit2:converter-moshi:$retrofitVersion"
implementation "com.squareup.moshi:moshi:$moshiVersion"
kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshiVersion"
implementation "com.squareup.okhttp3:okhttp:$okhttpVersion"

//picasso
implementation "com.squareup.picasso:picasso:$picassoVersion"

//MultiDex
implementation "androidx.multidex:multidex:$multiDexVersion"

//Mockk
// androidTestImplementation "io.mockk:mockk-android:$mockVersion"
testImplementation "io.mockk:mockk:$mockVersion"

//coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesCoreVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutinesCommonVersion"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycleExtensionsVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleKTXVersion"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleKTXVersion"
implementation "com.squareup.okhttp3:logging-interceptor:$okhttpInterceptorVersion"
implementation("com.github.bumptech.glide:glide:$glid") {
exclude group: "com.android.support"
}
implementation "androidx.activity:activity-ktx:$activityKtxVersion"
// Hilt
implementation "com.google.dagger:hilt-android:$hilt_version"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hilt_viewmodels"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
androidTestImplementation "com.google.dagger:hilt-android-testing:$hilt_version"
kaptAndroidTest "com.google.dagger:hilt-android-compiler:$hilt_version"
}
kapt {
correctErrorTypes true
}
8 changes: 0 additions & 8 deletions app/src/androidTest/java/com/task/AppTest.kt

This file was deleted.

4 changes: 2 additions & 2 deletions app/src/androidTest/java/com/task/CustomTestRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package com.task
import android.app.Application
import android.content.Context
import androidx.test.runner.AndroidJUnitRunner
import dagger.hilt.android.testing.HiltTestApplication

/**
* A custom [AndroidJUnitRunner] used to replace the application used in tests with a
* [CustomTestRunner].
*/
class CustomTestRunner : AndroidJUnitRunner() {

override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
return super.newApplication(cl, AppTest::class.java.name, context)
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
}
27 changes: 0 additions & 27 deletions app/src/androidTest/java/com/task/di/TestAppComponent.kt

This file was deleted.

7 changes: 7 additions & 0 deletions app/src/androidTest/java/com/task/di/TestDataModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import com.task.TestDataRepository
import com.task.data.DataRepositorySource
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import dagger.hilt.testing.TestInstallIn
import javax.inject.Singleton

@Module
@TestInstallIn(
components = [SingletonComponent::class],
replaces = [DataModule::class]
)
abstract class TestDataModule {
@Binds
@Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,29 @@ import androidx.test.espresso.IdlingResource
import androidx.test.espresso.action.ViewActions.scrollTo
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.ActivityTestRule
import com.task.R
import com.task.RECIPE_ITEM_KEY
import com.task.TestUtil.initData
import com.task.TestUtil.recipes
import com.task.RECIPE_ITEM_KEY
import com.task.utils.EspressoIdlingResource
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* Created by AhmedEltaher
*/

@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class DetailsActivityTest {

@get:Rule
var hiltRule = HiltAndroidRule(this)

@get:Rule
var mActivityTestRule = ActivityTestRule(DetailsActivity::class.java, true, false)
private var mIdlingResource: IdlingResource? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,27 @@ import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
import androidx.test.espresso.intent.rule.IntentsTestRule
import androidx.test.espresso.matcher.RootMatchers.withDecorView
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import com.task.R
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.CoreMatchers.not
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.lang.Thread.sleep


/**
* Created by AhmedEltaher
*/
@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class LoginActivityTest {

@get:Rule
var hiltRule = HiltAndroidRule(this)

@get:Rule
var mActivityTestRule = IntentsTestRule(LoginActivity::class.java, true, false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,28 @@ import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.matcher.RootMatchers
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.ActivityTestRule
import com.task.DataStatus
import com.task.R
import com.task.TestUtil.dataStatus
import com.task.TestUtil.recipes
import com.task.utils.EspressoIdlingResource
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.hamcrest.CoreMatchers
import org.hamcrest.Matchers.not
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.lang.Thread.sleep

@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class RecipesListActivityTest {

@get:Rule
var hiltRule = HiltAndroidRule(this)

@get:Rule
var mActivityTestRule = ActivityTestRule(RecipesListActivity::class.java, false, false)
private var mIdlingResource: IdlingResource? = null
Expand Down
32 changes: 4 additions & 28 deletions app/src/main/java/com/task/App.kt
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
package com.task

import android.app.Application
import android.content.Context
import androidx.multidex.MultiDexApplication
import com.task.di.DaggerAppComponent
import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.HasAndroidInjector
import javax.inject.Inject
import dagger.hilt.android.HiltAndroidApp

/**
* Created by AhmedEltaher
*/

open class App : MultiDexApplication(), HasAndroidInjector {

@Inject
lateinit var androidInjector: DispatchingAndroidInjector<Any>

override fun androidInjector(): AndroidInjector<Any> = androidInjector

override fun onCreate() {
super.onCreate()
context = applicationContext
initDagger()
}

open fun initDagger() {
DaggerAppComponent.builder().build().inject(this)
}

companion object {
lateinit var context: Context
}
}
@HiltAndroidApp
open class App : Application()
Loading

0 comments on commit ae48815

Please sign in to comment.