fun start() { engine.start() } } fun main(args: Array) { val engine = Engine() // construct val car = Car(engine) car.start() } Car With DI (Constructor Injection) Engine
start() { engine.start() } } fun main(args: Array) { val car = Car() car.engine = Engine() // construct car.start() } With DI (Field Injection) Car Engine
object ServiceLocator { fun getEngine() : Engine = Engine() } fun main(args: Array) { val car = Car() car.start() } Service Locator Engine Car Service Locator
, flavor . :data :app :buildcon fi g :buildcon fi g-stub compileOnly implementation = :others … Link: h tt ps://developer.android.com/build/dependencies#dependency_con fi gurations
import com.myapp.resources.R val bar = R.string.welcome_text // :library1 module - import com.myapp.library1.R + import com.myapp.resources.R val foo = R.string.welcome_text val bar = R.string.goodbye impo r ܳ ҙܻೞӝ ಞೞ. Link: h tt ps://medium.com/androiddevelopers/5-ways-to-prepare-your-app-build-for-android-studio- fl amingo-release-da34616bb946
Hilt public final class ExampleApplication_HiltComponents { @Component(modules = { ActivityRetainedCBuilderModule.class, ... }) @Singleton public abstract static class SingletonC implements SingletonComponent { } ... } // -------------------------------------------------------------------------------- // Generated by Dagger public final class DaggerExampleApplication_HiltComponents_SingletonC { // ઓࢿ ஶపց private static final class SingletonCImpl extends ExampleApplication_HiltComponents.SingletonC { public void injectExampleApplication(ExampleApplication exampleApplication) {} } ... } @HiltAndroidApp - ઓࢿ ஶపցܳ ࢤࢿೠ.
fun provideBar(): Bar = Bar() } // -------------------------------------------------------------------------------- public final class ExampleApplication_HiltComponents { @Component( modules = { ApplicationContextModule.class, ActivityRetainedCBuilderModule.class, FooModule.class, } ) @Singleton public abstract static class SingletonC implements HiltWrapper_ActivityRetainedComponentManager_ActivityRetainedComponentBuilderEntryPoint, SingletonComponent, GeneratedComponent { } }
{ @Provides fun provideBar(): Bar = Bar() } // -------------------------------------------------------------------------------- public final class FooModule_ProvideBarFactory implements Factory<Bar> { public static Bar provideBar() { return Preconditions.checkNotNullFromProvides(FooModule.INSTANCE.provideBar()); } }
@Inject lateinit var bar: Bar } @Module @InstallIn(SingletonComponent::class) object FooModule { @Provides fun provideBar(): Bar = Bar() } Hilt_ExampleApplication.java ExampleApplication_GeneratedInjector.java ExampleApplication_MembersInjector.java
Bar } = @HiltAndroidApp class ExampleApplication : Hilt_ExampleApplication() { @Inject lateinit var bar: Bar } ߄٘ܳ ߸ജೞৈ, ࠗݽ ېझܳ ߸҃ೠ. (Gradle Plugin)
} // -------------------------------------------------------------------------------- public final class ExampleApplication_HiltComponents { @Component(modules = { ... }) @Singleton public abstract static class SingletonC implements HiltWrapper_ActivityRetainedComponentManager_ActivityRetainedComponentBuilderEntryPoint, SingletonComponent, GeneratedComponent, ExampleApplication_GeneratedInjector { } } // -------------------------------------------------------------------------------- public final class DaggerExampleApplication_HiltComponents_SingletonC { private static final class SingletonCImpl extends SingletonC { public void injectExampleApplication(ExampleApplication instance) { } }
bar: Bar } // -------------------------------------------------------------------------------- public final class ExampleApplication_MembersInjector implements MembersInjector<ExampleApplication> { public static void injectBar(ExampleApplication instance, Bar bar) { instance.bar = bar; } } // -------------------------------------------------------------------------------- public final class DaggerExampleApplication_HiltComponents_SingletonC { private static final class SingletonCImpl extends SingletonC { public void injectExampleApplication(ExampleApplication instance) { ExampleApplication_MembersInjector.injectBar( instance, FooModule_ProvideBarFactory.provideBar()); } } ࢤࢿػ Injector ېझܳ ా೧, উ٘۽٘ ېझী ઓࢿ ੑػ.
val io_github_fornewid_feature_foo_impl_FooImpl_scope0: KClass<AppScope> = AppScope::class // Factory ېझ public object FooImpl_Factory : Factory<FooImpl> { override fun `get`(): FooImpl = newInstance() @JvmStatic public fun create(): FooImpl_Factory = this @JvmStatic public fun newInstance(): FooImpl = FooImpl() } Hint ੌҗ Factoryܳ ࢤࢿೠ.
Bar } → Hilt_HomeActivity.java ࢤࢿ → HomeActivity_GeneratedInjector.java ࢤࢿ → HomeActivity_MembersInjector.java ࢤࢿ // -------------------------------------------------------------------------------- class HomeActivity : ComponentActivity() { private val appGraph by lazy { appGraph<AppGraph>() } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val bar: Bar = appGraph.bar } } inline fun <reified T> Context.appGraph(): T { return (applicationContext as ExampleApplication).appGraph as T } Metroח ٘ ੑ হҊ, ઓࢿ Ӓېী Ӕ೧ঠ ೠ.
:app :feature:search-impl :feature-common :feature:detail-impl :data-api :feature:search-api :feature:detail-api Metro .class (KCP) Metro .class (KCP) (KCP) Metro .class Metro .class (KCP)
Factory<FooImpl> { public static final FooImpl$MetroFactory INSTANCE; // यӖఢ public final FooImpl newInstance() { return new FooImpl(); // ࢤࢿ ഐ } }
{ createGraph<AppGraph>() } // Metro ղࠗ ٘ public inline fun <reified T : Any> createGraph(): T { throw UnsupportedOperationException("Implemented by the compiler") } // IR ജ റ private static final AppGraph appGraph_delegate$lambda$0() { return new AppGraph$Impl(); // createGraphо ࢤࢿ ഐ۽ ജؽ }