Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate the Dagger app to Hilt #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions GithubBrowserSample/app/build.gradle
Expand Up @@ -21,6 +21,7 @@ apply plugin: 'kotlin-kapt'

apply plugin: 'kotlin-allopen'
apply plugin: 'androidx.navigation.safeargs.kotlin'
apply plugin: 'dagger.hilt.android.plugin'

allOpen {
// allows mocking for classes w/o directly opening them for release builds
Expand All @@ -46,8 +47,8 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
buildFeatures {
dataBinding = true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand Down Expand Up @@ -86,16 +87,11 @@ dependencies {
implementation deps.retrofit.gson
implementation deps.glide.runtime

implementation deps.dagger.runtime
implementation deps.dagger.android
implementation deps.dagger.android_support
implementation deps.constraint_layout
implementation deps.kotlin.stdlib

implementation deps.timber

kapt deps.dagger.android_support_compiler
kapt deps.dagger.compiler
kapt deps.room.compiler
kapt deps.lifecycle.compiler

Expand All @@ -120,6 +116,11 @@ dependencies {
androidTestImplementation deps.arch_core.testing
androidTestImplementation deps.mockito.core
androidTestImplementation deps.mockito.android

implementation deps.hilt.android
implementation deps.hilt.lifecycle_viewmodel
kapt deps.hilt.compiler
kapt deps.hilt.android_compiler
}

// we need all open to run tests which we enable only for debug builds.
Expand Down
Expand Up @@ -16,26 +16,16 @@

package com.android.example.github

import android.app.Activity
import android.app.Application
import com.android.example.github.di.AppInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.HasActivityInjector
import dagger.hilt.android.HiltAndroidApp
import timber.log.Timber
import javax.inject.Inject


class GithubApp : Application(), HasActivityInjector {
@Inject
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>

@HiltAndroidApp
class GithubApp : Application() {
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
}
AppInjector.init(this)
}

override fun activityInjector() = dispatchingAndroidInjector
}
Expand Up @@ -17,20 +17,13 @@
package com.android.example.github

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.appcompat.app.AppCompatActivity
import dagger.android.DispatchingAndroidInjector
import dagger.android.support.HasSupportFragmentInjector
import javax.inject.Inject

class MainActivity : AppCompatActivity(), HasSupportFragmentInjector {
@Inject
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Fragment>
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
}

override fun supportFragmentInjector() = dispatchingAndroidInjector
}

This file was deleted.

This file was deleted.

Expand Up @@ -16,7 +16,7 @@

package com.android.example.github.di

import android.app.Application
import android.content.Context
import androidx.room.Room
import com.android.example.github.api.GithubService
import com.android.example.github.db.GithubDb
Expand All @@ -25,11 +25,15 @@ import com.android.example.github.db.UserDao
import com.android.example.github.util.LiveDataCallAdapterFactory
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import dagger.hilt.android.qualifiers.ApplicationContext
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton

@Module(includes = [ViewModelModule::class])
@Module
@InstallIn(ApplicationComponent::class)
class AppModule {
@Singleton
@Provides
Expand All @@ -44,9 +48,9 @@ class AppModule {

@Singleton
@Provides
fun provideDb(app: Application): GithubDb {
fun provideDb(@ApplicationContext context: Context): GithubDb {
return Room
.databaseBuilder(app, GithubDb::class.java, "github.db")
.databaseBuilder(context, GithubDb::class.java, "github.db")
.fallbackToDestructiveMigration()
.build()
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.