LoginSignup
0
2

More than 3 years have passed since last update.

Android Hilt Componentsを紹介する

Last updated at Posted at 2020-09-17
1 / 21

アジェンダ

  • Hiltとは
  • 導入手順
  • Hilt Application
  • Components
  • Moduleの作り方
  • Activity, Fragmentでinject方法

Hiltとは


背景

  • Dagger - Square社が開発したもの
  • Dagger2 - GoogleがDaggerをforkして通称Dagger2を作っている
  • Dagger2 + Android
    • Dagger Android Supportの複雑性
    • components and scopesが標準ではない
    • マルチモジュールの流れと相性が悪く
  • Android Hilt

最新バージョン


メリット

  • 使い方が簡単になる
  • 標準化になる

導入手順


build dependencies

dependencies {
  implementation 'com.google.dagger:hilt-android:<VERSION>'
  kapt 'com.google.dagger:hilt-android-compiler:<VERSION>'

  // For instrumentation tests
  androidTestImplementation  'com.google.dagger:hilt-android-testing:<VERSION>'
  kaptAndroidTest 'com.google.dagger:hilt-android-compiler:<VERSION>'

  // For local unit tests
  testImplementation 'com.google.dagger:hilt-android-testing:<VERSION>'
  kaptTest 'com.google.dagger:hilt-android-compiler:<VERSION>'
}

kapt {
 correctErrorTypes true
}


Plugin設定

buildscript {
  repositories {
    // other repositories...
    mavenCentral()
  }
  dependencies {
    // other plugins...
    classpath 'com.google.dagger:hilt-android-gradle-plugin:<version>'
  }
}

apply plugin: 'com.android.application'
apply plugin: 'dagger.hilt.android.plugin'

android {
  // ...
}


プラグインを使う理由

  • プラグインを使わないと、親クラスをアノテーションで指定しないといけない
@HiltAndroidApp(MultiDexApplication.class)
public final class MyApplication extends Hilt_MyApplication {}
  • プラグインを使う
@HiltAndroidApp
public final class MyApplication extends MultiDexApplication {}

Hilt Application


before

方法1

public final class MyApplication extends MyBaseApplication {
  @Inject Bar bar;

  @Override public void onCreate() {
    super.onCreate();

    MyComponent myComponent =
        DaggerMyComponent
            .builder()
            ...
            .build();

    myComponent.inject(this);
  }
}

方法2

  • DaggerApplication導入

after

@HiltAndroidApp
class App : Application() {
}

Components


Component lifetimes

Hiltでは
* Application
* Activity
* Fragment
* Service
* View
のライフサイクルがあらかじめ定義されている


Component default bindings

Component Default Bindings
SingletonComponent Application
ActivityComponent Application, Activity
FragmentComponent Application, Activity, Fragment
ViewComponent Application, Activity, View
ServiceComponent Application, Service

Module


  • Moduleにこのライフサイクルを定義するには、@InstallIn annotationを使います。
@Module
@InstallIn(SingletonComponent::class)
object class FooModule {
  @Provides
  @Singleton
  fun provideBar(): Bar {...}
}


Activity, Fragmentでinject方法


@AndroidEntryPoint
class MyActivity : MyBaseActivity() {
  // Bindings in SingletonComponent or ActivityComponent
  @Inject lateinit var bar: Bar

  override fun onCreate(savedInstanceState: Bundle?) {
    // Injection happens in super.onCreate().
    super.onCreate()

    // Do something with bar ...
  }
}


感想

  • 新機能の追加がなし
  • 使いやすくなる
  • injectのやり方はほぼ変わっていないので、実装済み場合はHiltへ変更がいらないはず
0
2
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
2