Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HiltはDIをどうやってやっているのか

 HiltはDIをどうやってやっているのか

2021/05/19のRadiotalk Tech Talkの発表資料です。

https://zenn.dev/rmakiyama/articles/ed007c13e498fc

E6d26a51159a7863cac28e9d12ccd389?s=128

rmakiyama

May 19, 2021
Tweet

Transcript

  1. )JMU͸%*ΛͲ͏΍ͬͯ΍͍ͬͯΔͷ͔ Radiotalk Tech Talk #2 Android 2021/05/19 Ryo Makiyama

  2. w ຀ࢁྎ w 3BEJPUBMLגࣜձࣾ w "OESPJEΤϯδχΞ w!@SNBLJZBNB wSNBLJZBNB ࣗݾ঺հ

  3. ࿩͢͜ͱ • Hiltͷ؆୯ͳઆ໌ • Hiltͷੜ੒ίʔυ • Ͳ͏΍ͬͯDI͍ͯ͠Δ͔ • جૅ෦෼ͷΈ •

    DIʹ͍ͭͯͷઆ໌ • Daggerͷجຊ • Hilt΁ͷҠߦํ๏ ࿩͞ͳ͍͜ͱ
  4. 🎉 

  5. 💪

  6. )JMUͷجૅ

  7. ґଘؔ܎Λ௥Ճ // build.gradle buildscript { … dependencies { … classpath

    "com.google.dagger:hilt-android-gradle-plugin:2.35.1" } } // app/build.gradle apply plugin: 'kotlin-kapt' apply plugin: 'dagger.hilt.android.plugin' android { … } dependencies { … implementation "com.google.dagger:hilt-android:2.35.1" kapt “com.google.dagger:hilt-android-compiler:2.35.1" }
  8. )JMUΞϓϦέʔγϣϯ @HiltAndroidApp class SampleApp : Application() {

  9. όΠϯσΟϯάΛΦϒδΣΫτάϥϑʹొ࿥ class Hoge @Inject constructor() interface Piyo class PiyoImpl :

    Piyo @Module @InstallIn(SingletonComponent :: class) object PiyoModule { @Provides fun providePiyo(): Piyo { return PiyoImpl() } }
  10. "DUJWJUZʹґଘؔ܎Λ஫ೖ @AndroidEntryPoint class MainActivity : AppCompatActivity() { @Inject lateinit var

    hoge: Hoge @Inject lateinit var piyo: Piyo … }
  11. ͨͬͨ͜Ε͚ͩʜ🎉

  12. )JMUҎલʢ%BHHFSʣͷ՝୊ • ίϯϙʔωϯτ/αϒίϯϙʔωϯτͷઃܭ • ͦΕͧΕͷείʔϓͷׂΓ౰ͯ • ϓϩδΣΫτຖͷઃܭͷֶशίετ

  13. )JMU͕ղܾͨ͜͠ͱ • ίϯϙʔωϯτ/είʔϓͷඪ४Խ • ϘΠϥʔϓϨʔτͷ࡟ݮ • ઃఆͷ؆ૉԽʹΑΔֶशίετͷ࡟ݮ

  14. )JMU͕ղܾͨ͜͠ͱ • ίϯϙʔωϯτ/είʔϓͷඪ४Խ • ϘΠϥʔϓϨʔτͷ࡟ݮ • ֶशίετͷ࡟ݮ ͦͷҰํͰʜ

  15. )JMU͕ղܾͨ͜͠ͱ • ίϯϙʔωϯτ/είʔϓͷඪ४Խ • ϘΠϥʔϓϨʔτͷ࡟ݮ • ֶशίετͷ࡟ݮ Ͳ͏΍ͬͯ΍͍ͬͯΔͷ🤔

  16. ੜ੒͞ΕͨίʔυΛݟͯΈΔ👀

  17. ੜ੒͞Εͨίʔυ܈ • Hilt_SampleApp • DaggerSampleApp_HiltComponents_SingletonC • SampleApp_HiltComponents • SampleApp_GeneratedInjector •

    Hilt_MainActivity • MainActivity_GeneratedInjector • MainActivity_MembersInjector • Hoge_Factory • PiyoModule_ProvidePiyoFactory ˞લड़ͨ͠)JMUͷجૅͰॻ͍ͨίʔυʹΑΔੜ੒Λϕʔεͱ͍ͯ͠·͢ɻ
  18. public abstract class Hilt_SampleApp extends Application implements GeneratedComponentManagerHolder { private

    final ApplicationComponentManager componentManager = new ApplicationComponentManager(new ComponentSupplier() { @Override public Object get() { return DaggerSampleApp_HiltComponents_SingletonC.builder() .applicationContextModule(new ApplicationContextModule(Hilt_SampleApp.this)) .build(); } }); @Override public final ApplicationComponentManager componentManager() { return componentManager; } @Override public final Object generatedComponent() { return this.componentManager().generatedComponent(); } @CallSuper @Override public void onCreate() { ((SampleApp_GeneratedInjector) generatedComponent()).injectSampleApp(UnsafeCasts.<SampleApp>unsafeCast(this)); super.onCreate(); } }
  19. public abstract class Hilt_SampleApp extends Application implements GeneratedComponentManagerHolder { private

    final ApplicationComponentManager componentManager = new ApplicationComponentManager(new ComponentSupplier() { @Override public Object get() { return DaggerSampleApp_HiltComponents_SingletonC.builder() .applicationContextModule(new ApplicationContextModule(Hilt_SampleApp.this)) .build(); } }); @Override public final ApplicationComponentManager componentManager() { return componentManager; } @Override public final Object generatedComponent() { return this.componentManager().generatedComponent(); } @CallSuper @Override public void onCreate() { ((SampleApp_GeneratedInjector) generatedComponent()).injectSampleApp(UnsafeCasts.<SampleApp>unsafeCast(this)); super.onCreate(); } }
  20. public abstract class Hilt_SampleApp extends Application implements GeneratedComponentManagerHolder { private

    final ApplicationComponentManager componentManager = new ApplicationComponentManager(new ComponentSupplier() { @Override public Object get() { return DaggerSampleApp_HiltComponents_SingletonC.builder() .applicationContextModule(new ApplicationContextModule(Hilt_SampleApp.this)) .build(); } }); @Override public final ApplicationComponentManager componentManager() { return componentManager; } @Override public final Object generatedComponent() { return this.componentManager().generatedComponent(); } @CallSuper @Override public void onCreate() { ((SampleApp_GeneratedInjector) generatedComponent()).injectSampleApp(UnsafeCasts.<SampleApp>unsafeCast(this)); super.onCreate(); } } "QQMJDBUJPOϨϕϧͷίϯϙʔωϯτΛੜ੒ɾอ࣋
  21. public abstract class Hilt_SampleApp extends Application implements GeneratedComponentManagerHolder { private

    final ApplicationComponentManager componentManager = new ApplicationComponentManager(new ComponentSupplier() { @Override public Object get() { return DaggerSampleApp_HiltComponents_SingletonC.builder() .applicationContextModule(new ApplicationContextModule(Hilt_SampleApp.this)) .build(); } }); @Override public final ApplicationComponentManager componentManager() { return componentManager; } @Override public final Object generatedComponent() { return this.componentManager().generatedComponent(); } @CallSuper @Override public void onCreate() { ((SampleApp_GeneratedInjector) generatedComponent()).injectSampleApp(UnsafeCasts.<SampleApp>unsafeCast(this)); super.onCreate(); } } )JMU͕4BNQMF"QQʹ"QQMJDBUJPOϨϕϧͷ ίϯϙʔωϯτͷґଘؔ܎ΛఏڙͰ͖ΔΑ͏ʹ͍ͯ͠Δ
  22. public abstract class Hilt_SampleApp extends Application implements GeneratedComponentManagerHolder { private

    final ApplicationComponentManager componentManager = new ApplicationComponentManager(new ComponentSupplier() { @Override public Object get() { return DaggerSampleApp_HiltComponents_SingletonC.builder() .applicationContextModule(new ApplicationContextModule(Hilt_SampleApp.this)) .build(); } }); @Override public final ApplicationComponentManager componentManager() { return componentManager; } @Override public final Object generatedComponent() { return this.componentManager().generatedComponent(); } @CallSuper @Override public void onCreate() { ((SampleApp_GeneratedInjector) generatedComponent()).injectSampleApp(UnsafeCasts.<SampleApp>unsafeCast(this)); super.onCreate(); } } "QQMJDBUJPOϨϕϧͷίϯϙʔωϯτ
  23. public final class DaggerSampleApp_HiltComponents_SingletonC extends SampleApp_HiltComponents.SingletonC { … public static

    Builder builder() {…} @Override public void injectSampleApp(SampleApp sampleApp) {…} @Override public ActivityRetainedComponentBuilder retainedComponentBuilder() {…} @Override public ServiceComponentBuilder serviceComponentBuilder() {…} public static final class Builder {…} private final class ActivityRetainedCBuilder implements SampleApp_HiltComponents.ActivityRetainedC.Builder {…} private final class ActivityRetainedCImpl extends SampleApp_HiltComponents.ActivityRetainedC {…} private final class ServiceCBuilder implements SampleApp_HiltComponents.ServiceC.Builder {…} private final class ServiceCImpl extends SampleApp_HiltComponents.ServiceC {…} }
  24. public final class DaggerSampleApp_HiltComponents_SingletonC extends SampleApp_HiltComponents.SingletonC { … public static

    Builder builder() {…} @Override public void injectSampleApp(SampleApp sampleApp) {…} @Override public ActivityRetainedComponentBuilder retainedComponentBuilder() {…} @Override public ServiceComponentBuilder serviceComponentBuilder() {…} public static final class Builder {…} private final class ActivityRetainedCBuilder implements SampleApp_HiltComponents.ActivityRetainedC.Builder {…} private final class ActivityRetainedCImpl extends SampleApp_HiltComponents.ActivityRetainedC {…} private final class ServiceCBuilder implements SampleApp_HiltComponents.ServiceC.Builder {…} private final class ServiceCImpl extends SampleApp_HiltComponents.ServiceC {…} } ࣗ਎ͷϏϧμʔΫϥε "DUJWJUZ3FUBJOFE$PNQPOFOUͷ࣮૷ͱ ͦͷϏϧμʔΫϥε 4FSWJDF$PNQPOFOUͷ࣮૷ͱ ͦͷϏϧμʔΫϥε
  25. public final class DaggerSampleApp_HiltComponents_SingletonC extends SampleApp_HiltComponents.SingletonC { … public static

    Builder builder() {…} @Override public void injectSampleApp(SampleApp sampleApp) {…} @Override public ActivityRetainedComponentBuilder retainedComponentBuilder() {…} @Override public ServiceComponentBuilder serviceComponentBuilder() {…} public static final class Builder {…} private final class ActivityRetainedCBuilder implements SampleApp_HiltComponents.ActivityRetainedC.Builder {…} private final class ActivityRetainedCImpl extends SampleApp_HiltComponents.ActivityRetainedC {…} private final class ServiceCBuilder implements SampleApp_HiltComponents.ServiceC.Builder {…} private final class ServiceCImpl extends SampleApp_HiltComponents.ServiceC {…} }
  26. public final class DaggerSampleApp_HiltComponents_SingletonC extends SampleApp_HiltComponents.SingletonC { … private final

    class ActivityRetainedCImpl extends SampleApp_HiltComponents.ActivityRetainedC { … @Override public ActivityComponentBuilder activityComponentBuilder() {…} @Override public ActivityRetainedLifecycle getActivityRetainedLifecycle() {…} private final class ActivityCBuilder implements SampleApp_HiltComponents.ActivityC.Builder {…} private final class ActivityCImpl extends SampleApp_HiltComponents.ActivityC {…} private final class ViewModelCBuilder implements SampleApp_HiltComponents.ViewModelC.Builder {…} private final class ViewModelCImpl extends SampleApp_HiltComponents.ViewModelC {…} … }
  27. public final class DaggerSampleApp_HiltComponents_SingletonC extends SampleApp_HiltComponents.SingletonC { … private final

    class ActivityRetainedCImpl extends SampleApp_HiltComponents.ActivityRetainedC { … @Override public ActivityComponentBuilder activityComponentBuilder() {…} @Override public ActivityRetainedLifecycle getActivityRetainedLifecycle() {…} private final class ActivityCBuilder implements SampleApp_HiltComponents.ActivityC.Builder {…} private final class ActivityCImpl extends SampleApp_HiltComponents.ActivityC {…} private final class ViewModelCBuilder implements SampleApp_HiltComponents.ViewModelC.Builder {…} private final class ViewModelCImpl extends SampleApp_HiltComponents.ViewModelC {…} … } "DUJWJUZ$PNQPOFOUͷ࣮૷ͱ ͦͷϏϧμʔΫϥε 7JFX.PEFM$PNQPOFOUͷ࣮૷ͱ ͦͷϏϧμʔΫϥε
  28. public final class DaggerSampleApp_HiltComponents_SingletonC extends SampleApp_HiltComponents.SingletonC { … private final

    class ActivityRetainedCImpl extends SampleApp_HiltComponents.ActivityRetainedC { … @Override public ActivityComponentBuilder activityComponentBuilder() {…} @Override public ActivityRetainedLifecycle getActivityRetainedLifecycle() {…} private final class ActivityCBuilder implements SampleApp_HiltComponents.ActivityC.Builder {…} private final class ActivityCImpl extends SampleApp_HiltComponents.ActivityC {…} private final class ViewModelCBuilder implements SampleApp_HiltComponents.ViewModelC.Builder {…} private final class ViewModelCImpl extends SampleApp_HiltComponents.ViewModelC {…} … } IUUQTEFWFMPQFSBOESPJEDPNUSBJOJOHEFQFOEFODZJOKFDUJPOIJMUBOESPJE
  29. public final class DaggerSampleApp_HiltComponents_SingletonC extends SampleApp_HiltComponents.SingletonC { … public static

    Builder builder() {…} @Override public void injectSampleApp(SampleApp sampleApp) {…} @Override public ActivityRetainedComponentBuilder retainedComponentBuilder() {…} @Override public ServiceComponentBuilder serviceComponentBuilder() {…} public static final class Builder {…} private final class ActivityRetainedCBuilder implements SampleApp_HiltComponents.ActivityRetainedC.Builder {…} private final class ActivityRetainedCImpl extends SampleApp_HiltComponents.ActivityRetainedC {…} private final class ServiceCBuilder implements SampleApp_HiltComponents.ServiceC.Builder {…} private final class ServiceCImpl extends SampleApp_HiltComponents.ServiceC {…} }
  30. public final class SampleApp_HiltComponents { … @Component( modules = {

    ApplicationContextModule.class, PiyoModule.class, ActivityRetainedCBuilderModule.class, ServiceCBuilderModule.class } ) @Singleton public abstract static class SingletonC implements SampleApp_GeneratedInjector, HiltWrapper_ActivityRetainedComponentManager_ActivityRetainedComponentBuilderEntryPoint, ServiceComponentManager.ServiceComponentBuilderEntryPoint, SingletonComponent, GeneratedComponent { } … } $PNQPOFOUͷఆ͕ٛ·ͱ·͍ͬͯΔΫϥε
  31. public final class SampleApp_HiltComponents { … @Component( modules = {

    ApplicationContextModule.class, PiyoModule.class, ActivityRetainedCBuilderModule.class, ServiceCBuilderModule.class } ) @Singleton public abstract static class SingletonC implements SampleApp_GeneratedInjector, HiltWrapper_ActivityRetainedComponentManager_ActivityRetainedComponentBuilderEntryPoint, ServiceComponentManager.ServiceComponentBuilderEntryPoint, SingletonComponent, GeneratedComponent { } … } །Ұͷ$PNQPOFOU @Module @InstallIn(SingletonComponent :: class) object PiyoModule { @Provides fun providePiyo(): Piyo { return PiyoImpl() } }
  32. public final class SampleApp_HiltComponents { … @Subcomponent( modules = {

    HiltWrapper_ActivityRetainedComponentManager_LifecycleModule.class, ActivityCBuilderModule.class, ViewModelCBuilderModule.class } ) @ActivityRetainedScoped public abstract static class ActivityRetainedC implements ActivityRetainedComponent, ActivityComponentManager.ActivityComponentBuilderEntryPoint, HiltWrapper_ActivityRetainedComponentManager_ActivityRetainedLifecycleEntryPoint, GeneratedComponent { @Subcomponent.Builder abstract interface Builder extends ActivityRetainedComponentBuilder { } } … } 4JOHMFUPO$Ҏ֎͸4VCDPNQPOFOU IUUQTEFWFMPQFSBOESPJEDPNUSBJOJOHEFQFOEFODZJOKFDUJPOIJMUBOESPJE
  33. ͜͜·ͰΛ੔ཧ🍵 • Hilt_AppName͕ى఺ʹͳΓґଘؔ܎ΛఏڙͰ͖ΔΑ͏ʹͳΔ • AppName_HiltComponents͸ඪ४Խ͞Εͨ 
 DaggerͷίϯϙʔωϯτΛఆٛ • DaggerAppName_HiltComponents_SingletonC͸ 


    ίϯϙʔωϯτ܈ͷ࣮૷Ϋϥε
  34. ੜ੒͞Εͨίʔυ܈ • Hilt_SampleApp • DaggerSampleApp_HiltComponents_SingletonC • SampleApp_HiltComponents • SampleApp_GeneratedInjector •

    Hilt_MainActivity • MainActivity_GeneratedInjector • MainActivity_MembersInjector • Hoge_Factory • PiyoModule_ProvidePiyoFactory ˞લड़ͨ͠)JMUͷجૅͰॻ͍ͨίʔυʹΑΔੜ੒Λϕʔεͱ͍ͯ͠·͢ɻ
  35. "DUJWJUZ΁ͷґଘؔ܎ͷ஫ೖ·Ͱ

  36. public abstract class Hilt_MainActivity extends AppCompatActivity implements GeneratedComponentManagerHolder { private

    volatile ActivityComponentManager componentManager; … @Override public final Object generatedComponent() { return this.componentManager().generatedComponent(); } … protected void inject() { if (!injected) { injected = true; ((MainActivity_GeneratedInjector) this.generatedComponent()).injectMainActivity(UnsafeCasts.<MainActivity>unsafeCast(this)); } } @Override public ViewModelProvider.Factory getDefaultViewModelProviderFactory() { return DefaultViewModelFactories.getActivityFactory(this, super.getDefaultViewModelProviderFactory()); } }
  37. public abstract class Hilt_MainActivity extends AppCompatActivity implements GeneratedComponentManagerHolder { private

    volatile ActivityComponentManager componentManager; … @Override public final Object generatedComponent() { return this.componentManager().generatedComponent(); } … protected void inject() { if (!injected) { injected = true; ((MainActivity_GeneratedInjector) this.generatedComponent()).injectMainActivity(UnsafeCasts.<MainActivity>unsafeCast(this)); } } @Override public ViewModelProvider.Factory getDefaultViewModelProviderFactory() { return DefaultViewModelFactories.getActivityFactory(this, super.getDefaultViewModelProviderFactory()); } }
  38. public abstract class Hilt_MainActivity extends AppCompatActivity implements GeneratedComponentManagerHolder { private

    volatile ActivityComponentManager componentManager; … @Override public final Object generatedComponent() { return this.componentManager().generatedComponent(); } … protected void inject() { if (!injected) { injected = true; ((MainActivity_GeneratedInjector) this.generatedComponent()).injectMainActivity(UnsafeCasts.<MainActivity>unsafeCast(this)); } } @Override public ViewModelProvider.Factory getDefaultViewModelProviderFactory() { return DefaultViewModelFactories.getActivityFactory(this, super.getDefaultViewModelProviderFactory()); } } // package dagger.hilt.android.internal.managers public class ActivityComponentManager implements GeneratedComponentManager<Object> { … @Override public Object generatedComponent() { if (component == null) { synchronized (componentLock) { if (component == null) { component = createComponent(); } } } return component; } … }
  39. public abstract class Hilt_MainActivity extends AppCompatActivity implements GeneratedComponentManagerHolder { private

    volatile ActivityComponentManager componentManager; … @Override public final Object generatedComponent() { return this.componentManager().generatedComponent(); } … protected void inject() { if (!injected) { injected = true; ((MainActivity_GeneratedInjector) this.generatedComponent()).injectMainActivity(UnsafeCasts.<MainActivity>unsafeCast(this)); } } @Override public ViewModelProvider.Factory getDefaultViewModelProviderFactory() { return DefaultViewModelFactories.getActivityFactory(this, super.getDefaultViewModelProviderFactory()); } } )JMU͕.BJO"DUJWJUZʹ"DUJWJUZϨϕϧͷ ίϯϙʔωϯτͷґଘؔ܎ΛఏڙͰ͖ΔΑ͏ʹ͍ͯ͠Δ
  40. public final class DaggerSampleApp_HiltComponents_SingletonC extends SampleApp_HiltComponents.SingletonC { … private final

    class ActivityCImpl extends SampleApp_HiltComponents.ActivityC { … @Override public void injectMainActivity(MainActivity mainActivity) { injectMainActivity2(mainActivity); } … private MainActivity injectMainActivity2(MainActivity instance) { MainActivity_MembersInjector.injectHoge(instance, new Hoge()); MainActivity_MembersInjector.injectPiyo(instance, PiyoModule_ProvidePiyoFactory.providePiyo()); return instance; } … } }
  41. public final class DaggerSampleApp_HiltComponents_SingletonC extends SampleApp_HiltComponents.SingletonC { … private final

    class ActivityCImpl extends SampleApp_HiltComponents.ActivityC { … @Override public void injectMainActivity(MainActivity mainActivity) { injectMainActivity2(mainActivity); } … private MainActivity injectMainActivity2(MainActivity instance) { MainActivity_MembersInjector.injectHoge(instance, new Hoge()); MainActivity_MembersInjector.injectPiyo(instance, PiyoModule_ProvidePiyoFactory.providePiyo()); return instance; } … } } @OriginatingElement( topLevelClass = MainActivity.class ) @GeneratedEntryPoint @InstallIn(ActivityComponent.class) public interface MainActivity_GeneratedInjector { void injectMainActivity(MainActivity mainActivity); }
  42. public final class DaggerSampleApp_HiltComponents_SingletonC extends SampleApp_HiltComponents.SingletonC { … private final

    class ActivityCImpl extends SampleApp_HiltComponents.ActivityC { … @Override public void injectMainActivity(MainActivity mainActivity) { injectMainActivity2(mainActivity); } … private MainActivity injectMainActivity2(MainActivity instance) { MainActivity_MembersInjector.injectHoge(instance, new Hoge()); MainActivity_MembersInjector.injectPiyo(instance, PiyoModule_ProvidePiyoFactory.providePiyo()); return instance; } … } }
  43. public final class DaggerSampleApp_HiltComponents_SingletonC extends SampleApp_HiltComponents.SingletonC { … private final

    class ActivityCImpl extends SampleApp_HiltComponents.ActivityC { … @Override public void injectMainActivity(MainActivity mainActivity) { injectMainActivity2(mainActivity); } … private MainActivity injectMainActivity2(MainActivity instance) { MainActivity_MembersInjector.injectHoge(instance, new Hoge()); MainActivity_MembersInjector.injectPiyo(instance, PiyoModule_ProvidePiyoFactory.providePiyo()); return instance; } … } } public final class PiyoModule_ProvidePiyoFactory implements Factory<Piyo> { … public static Piyo providePiyo() { return Preconditions.checkNotNullFromProvides(PiyoModule.INSTANCE.providePiyo()); } … }
  44. public final class DaggerSampleApp_HiltComponents_SingletonC extends SampleApp_HiltComponents.SingletonC { … private final

    class ActivityCImpl extends SampleApp_HiltComponents.ActivityC { … @Override public void injectMainActivity(MainActivity mainActivity) { injectMainActivity2(mainActivity); } … private MainActivity injectMainActivity2(MainActivity instance) { MainActivity_MembersInjector.injectHoge(instance, new Hoge()); MainActivity_MembersInjector.injectPiyo(instance, PiyoModule_ProvidePiyoFactory.providePiyo()); return instance; } … } } public final class PiyoModule_ProvidePiyoFactory implements Factory<Piyo> { … public static Piyo providePiyo() { return Preconditions.checkNotNullFromProvides(PiyoModule.INSTANCE.providePiyo()); } … } @Module @InstallIn(SingletonComponent :: class) object PiyoModule { @Provides fun providePiyo(): Piyo { return PiyoImpl() } }
  45. public final class MainActivity_MembersInjector implements MembersInjector<MainActivity> { … @InjectedFieldSignature("com.rmakiyama.daggerhiltstudy.MainActivity.hoge") public

    static void injectHoge(MainActivity instance, Hoge hoge) { instance.hoge = hoge; } @InjectedFieldSignature("com.rmakiyama.daggerhiltstudy.MainActivity.piyo") public static void injectPiyo(MainActivity instance, Piyo piyo) { instance.piyo = piyo; } } .BJO"DUJWJUZ΁ϑΟʔϧυΠϯδΣΫγϣϯ
  46. "DUJWJUZʹґଘؔ܎͕஫ೖ͞Εͨ🎉 @AndroidEntryPoint class MainActivity : AppCompatActivity() { @Inject lateinit var

    hoge: Hoge @Inject lateinit var piyo: Piyo … }
  47. #POVTUSBDL🚀

  48. 1JZPΛ4JOHMFUPOείʔϓʹͯ͠ΈΔ @Module @InstallIn(SingletonComponent :: class) object PiyoModule { @Provides @Singleton

    fun providePiyo(): Piyo { return PiyoImpl() } }
  49. public final class DaggerSampleApp_HiltComponents_SingletonC extends SampleApp_HiltComponents.SingletonC { … private volatile

    Object piyo = new MemoizedSentinel(); private Piyo piyo() { Object local = piyo; if (local instanceof MemoizedSentinel) { synchronized (local) { local = piyo; if (local instanceof MemoizedSentinel) { local = PiyoModule_ProvidePiyoFactory.providePiyo(); piyo = DoubleCheck.reentrantCheck(piyo, local); } } } return (Piyo) local; } … private final class ActivityCImpl extends SampleApp_HiltComponents.ActivityC { … private MainActivity injectMainActivity2(MainActivity instance) { MainActivity_MembersInjector.injectHoge(instance, new Hoge()); MainActivity_MembersInjector.injectPiyo(instance, DaggerSampleApp_HiltComponents_SingletonC.this.piyo()); return instance; } … } } 4JOHMFUPO$PNQPOFOUͰΠϯελϯεΛ؅ཧ
  50. ·ͱΊ • Hilt͸ίϯϙʔωϯτ֊૚Λඪ४Խͨ͠ • ಺෦Ͱͷॲཧ͸Dagger2ͱ͋·ΓมΘΒͳ͍ • ίϯϙʔωϯτઃܭͰ೰·ͳ͍ͷͰཧղ͠΍͍͢ • ͜ΕͰ͋ͳͨ΋ੜ੒ίʔυ͕ಡΊΔʂ •

    Ͳ͏΍ͬͯ΍͍ͬͯΔ͔Λཧղ͢Δͱ҆৺
  51. "QQFOEJY

  52. -JOLT • https: // developer.android.com/training/dependency-injection/ hilt-android • https: // developer.android.com/training/dependency-injection/

    dagger-android • https: // dagger.dev/hilt/ • https: // zenn.dev/rmakiyama/articles/ed007c13e498fc • https: // github.com/rmakiyama/DaggerHiltStudy • https: // qiita.com/takahirom/items/46053e031041405e2a9e