LoginSignup
0
1

More than 1 year has passed since last update.

Dagger Hilt alpha版からの移行

Posted at

いささか気を逸した話題ではありますが......
記録しておきます。

運用中のプロダクトで使用しているDagger Hiltのアップデートを行いました。
2.28-alphaから2.38.1までアップデートしています。
以下、やったことを順番に記載します。

build.gradleの編集

hilt-lifecycleが不要になりましたので依存関係から排除します

before

build.gradle
    implementation "com.google.dagger:hilt-android:$dagger_hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$dagger_hilt_version"
    def hilt_lifecycle_version = "1.0.0-alpha02"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hilt_lifecycle_version"
    kapt "androidx.hilt:hilt-compiler:$hilt_lifecycle_version"

after

build.gradle
    implementation "com.google.dagger:hilt-android:$dagger_hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$dagger_hilt_version"

コンポーネントのクラス名変更①

ApplicationComponentSingletonComponentに変えます

before

ExampleModule.kt
@Module
@InstallIn(ApplicationComponent::class)
object ExampleModule {
}

after

ExampleModule.kt
@Module
@InstallIn(SingletonComponent::class)
object ExampleModule {
}

コンポーネントのクラス名変更②

ActivityComponentを利用しているModuleをActivityRetainedComponentに変更しました。新旧でコンポーネントのスコープが変わっており、あえてActivityComponentにしておく必要がないと判断したためです。
AndroidDevelopersでは以下のような対応となっています。

ActivityRetainedComponentのスコープ

クラス Avtivity ViewModel

before

ExampleModule.kt
@Module
@InstallIn(ActivityComponent::class)
object ExampleModule {
    @ActivityScoped
    @Provides
    fun provideExampleClient(): ExampleClient {
    }
}

after

ExampleModule.kt
@Module
@InstallIn(ActivityRetainedComponent::class)
object ExampleModule {
    @ActivityRetainedScoped
    @Provides
    fun provideExampleClient(): ExampleClient {
    }
}

ViewModelの記法修正

ViewModelInjectがなくなりました

before

ExampleViewModel.kt
class ExampleViewModel @ViewModelInject constructor() : ViewModel() {

after

ExampleViewModel.kt
@HiltViewModel
class ExampleViewModel @Inject constructor() : ViewModel() {
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1