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

rmakiyama

May 19, 2021
Tweet

More Decks by rmakiyama

Other Decks in Technology

Transcript

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

    DIʹ͍ͭͯͷઆ໌ • Daggerͷجຊ • Hilt΁ͷҠߦํ๏ ࿩͞ͳ͍͜ͱ
  2. ґଘؔ܎Λ௥Ճ // 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" }
  3. όΠϯσΟϯάΛΦϒδΣΫτάϥϑʹొ࿥ class Hoge @Inject constructor() interface Piyo class PiyoImpl :

    Piyo @Module @InstallIn(SingletonComponent :: class) object PiyoModule { @Provides fun providePiyo(): Piyo { return PiyoImpl() } }
  4. ੜ੒͞Εͨίʔυ܈ • Hilt_SampleApp • DaggerSampleApp_HiltComponents_SingletonC • SampleApp_HiltComponents • SampleApp_GeneratedInjector •

    Hilt_MainActivity • MainActivity_GeneratedInjector • MainActivity_MembersInjector • Hoge_Factory • PiyoModule_ProvidePiyoFactory ˞લड़ͨ͠)JMUͷجૅͰॻ͍ͨίʔυʹΑΔੜ੒Λϕʔεͱ͍ͯ͠·͢ɻ
  5. 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(); } }
  6. 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(); } }
  7. 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ϨϕϧͷίϯϙʔωϯτΛੜ੒ɾอ࣋
  8. 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Ϩϕϧͷ ίϯϙʔωϯτͷґଘؔ܎ΛఏڙͰ͖ΔΑ͏ʹ͍ͯ͠Δ
  9. 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Ϩϕϧͷίϯϙʔωϯτ
  10. 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 {…} }
  11. 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ͷ࣮૷ͱ ͦͷϏϧμʔΫϥε
  12. 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 {…} }
  13. 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 {…} … }
  14. 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ͷ࣮૷ͱ ͦͷϏϧμʔΫϥε
  15. 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
  16. 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 {…} }
  17. 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ͷఆ͕ٛ·ͱ·͍ͬͯΔΫϥε
  18. 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() } }
  19. 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
  20. ੜ੒͞Εͨίʔυ܈ • Hilt_SampleApp • DaggerSampleApp_HiltComponents_SingletonC • SampleApp_HiltComponents • SampleApp_GeneratedInjector •

    Hilt_MainActivity • MainActivity_GeneratedInjector • MainActivity_MembersInjector • Hoge_Factory • PiyoModule_ProvidePiyoFactory ˞લड़ͨ͠)JMUͷجૅͰॻ͍ͨίʔυʹΑΔੜ੒Λϕʔεͱ͍ͯ͠·͢ɻ
  21. 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()); } }
  22. 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()); } }
  23. 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; } … }
  24. 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Ϩϕϧͷ ίϯϙʔωϯτͷґଘؔ܎ΛఏڙͰ͖ΔΑ͏ʹ͍ͯ͠Δ
  25. 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; } … } }
  26. 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); }
  27. 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; } … } }
  28. 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()); } … }
  29. 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() } }
  30. 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΁ϑΟʔϧυΠϯδΣΫγϣϯ
  31. 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ͰΠϯελϯεΛ؅ཧ
  32. -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