Dagger Hilt 初探

介紹

Dagger Hilt (這名字起的溜...........)

官方描述其設(shè)計(jì)目的:

  • To simplify Dagger-related infrastructure for Android apps.
  • To create a standard set of components and scopes to ease setup, readability/understanding, and code sharing between apps.
  • To provide an easy way to provision different bindings to various build types (e.g. testing, debug, or release).

簡(jiǎn)單說(shuō)就是Dagger Android的瘦身包,使依賴注入在Android開發(fā)中標(biāo)準(zhǔn)化、簡(jiǎn)單化。

集成

首先在項(xiàng)目級(jí)別的build.gradle文件中添加以下內(nèi)容,這將使我們能夠訪問(wèn)hilt gradle插件:

classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'

然后在應(yīng)用程序級(jí)別的build.gradle文件并應(yīng)用此插件:

apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'

最后,在應(yīng)用程序級(jí)別build.gradle文件中添加所需的hilt依賴項(xiàng):

implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01'
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'

這樣Hilt就可以使用了。

Hilt Application

按照官方要求,首先需要在自定義的Application類中添加@HiltAndroidApp注解:

@HiltAndroidApp
class APP:Application()

這有什么作用?以下來(lái)自官方描述:

All apps using Hilt must contain an Application class annotated with @HiltAndroidApp. @HiltAndroidApp kicks off the code generation of the Hilt components and also generates a base class for your application that uses those generated components. Because the code generation needs access to all of your modules, the target that compiles your Application class also needs to have all of your Dagger modules in its transitive dependencies.

Just like other Hilt Android entry points, Applications are members injected as well. This means you can use injected fields in the Application after super.onCreate() has been called.

Daager2中,需要Application繼承DaggerApplication,并且還需要?jiǎng)?chuàng)建Application的Module。
這里只需要使用@HiltAndroidApp的注解就可以完成對(duì)Application的依賴注入,由Hilt gradle插件生成對(duì)應(yīng)的文件

構(gòu)建后我們看到在 app/build/generated/source/kapt/debug/目錄下生成了一個(gè)Hilt_APP的抽象類:

/**
 * A generated base class to be extended by the @dagger.hilt.android.HiltAndroidApp annotated class. If using the Gradle plugin, this is swapped as the base class via bytecode transformation. */
@Generated("dagger.hilt.android.processor.internal.androidentrypoint.ApplicationGenerator")
public abstract class Hilt_APP extends Application implements GeneratedComponentManager<Object> {
  private final ApplicationComponentManager componentManager = new ApplicationComponentManager(new ComponentSupplier() {
    @Override
    public Object get() {
      return DaggerAPP_HiltComponents_ApplicationC.builder()
          .applicationContextModule(new ApplicationContextModule(Hilt_APP.this))
          .build();
    }
  });

  protected final ApplicationComponentManager componentManager() {
    return componentManager;
  }

  @Override
  public final Object generatedComponent() {
    return componentManager().generatedComponent();
  }

  @CallSuper
  @Override
  public void onCreate() {
    // This is a known unsafe cast, but is safe in the only correct use case:
    // APP extends Hilt_APP
    ((APP_GeneratedInjector) generatedComponent()).injectAPP(UnsafeCasts.<APP>unsafeCast(this));
    super.onCreate();
  }
}

  • ApplicationComponentManager的聲明
  • onCreate函數(shù)中注入Application類

看下里面涉及到的~

ApplicationComponentManager

public final class ApplicationComponentManager implements GeneratedComponentManager<Object> {
  private volatile Object component;
  private final Object componentLock = new Object();
  private final ComponentSupplier componentCreator;

  public ApplicationComponentManager(ComponentSupplier componentCreator) {
    this.componentCreator = componentCreator;
  }

  @Override
  public Object generatedComponent() {
    if (component == null) {
      synchronized (componentLock) {
        if (component == null) {
          component = componentCreator.get();
        }
      }
    }
    return component;
  }
}

主要用于管理應(yīng)用程序中的Hilt Component的創(chuàng)建

  • 構(gòu)造中創(chuàng)建ComponentSupplier實(shí)例。

  • generatedComponent():通過(guò)對(duì)象鎖獲取ComponentSupplier類中的Object,并負(fù)責(zé)在onCreate函數(shù)中將依賴項(xiàng)注入到我們的應(yīng)用程序類中。

ComponentSupplier

提供component的接口

/**
 * Interface for supplying a component. This is separate from the Supplier interface so that
 * optimizers can strip this method (and therefore all the Dagger code) from the main dex even if a
 * Supplier is referenced in code kept in the main dex.
 */
public interface ComponentSupplier {
  Object get();
}

ApplicationC

代碼比較長(zhǎng)就不貼出來(lái)了,看下builder:

import dagger.hilt.android.internal.builders.ActivityComponentBuilder;
import dagger.hilt.android.internal.builders.ActivityRetainedComponentBuilder;
import dagger.hilt.android.internal.builders.FragmentComponentBuilder;
import dagger.hilt.android.internal.builders.ServiceComponentBuilder;
import dagger.hilt.android.internal.builders.ViewComponentBuilder;
import dagger.hilt.android.internal.builders.ViewWithFragmentComponentBuilder;

ComponentSupplier實(shí)現(xiàn)的內(nèi)部,我們可以看到對(duì)ApplicationC(Application組件)類的引用。DaggerAPP_HiltComponents_ApplicationC是生成的應(yīng)用程序組件,它充當(dāng)Hilt在我們的應(yīng)用程序中使用的(Activity,F(xiàn)ragment,Service,View等組件)的全局容器。

GeneratedInjector

@OriginatingElement(
    topLevelClass = APP.class
)
@GeneratedEntryPoint
@InstallIn(ApplicationComponent.class)
@Generated("dagger.hilt.android.processor.internal.androidentrypoint.InjectorEntryPointGenerator")
public interface APP_GeneratedInjector {
  void injectAPP(APP aPP);
}

接口類提供injectAPP方法為外部類提供了一個(gè)訪問(wèn)點(diǎn),以觸發(fā)應(yīng)用程序Component的注入。

ApplicationContextModule

@Module
@InstallIn(ApplicationComponent.class)
public final class ApplicationContextModule {
  private final Context applicationContext;

  public ApplicationContextModule(Context applicationContext) {
    this.applicationContext = applicationContext;
  }

  @Provides
  @ApplicationContext
  Context provideContext() {
    return applicationContext;
  }

  @Provides
  Application provideApplication() {
    return (Application) applicationContext.getApplicationContext();
  }

}

主要是提供ApplicationContext,通過(guò)@InstalIIn注入到 ApplicationComponent便于后續(xù)使用

這里有個(gè)@ApplicationContext 這是個(gè)qualifers 限定符,Hilt還提供了一個(gè)@ActivityContext,

例如:

class AnalyticsAdapter @Inject constructor(
    @ActivityContext private val context: Context,
    private val service: AnalyticsService
) { ... }
@Singleton
class NetWorkUtils @Inject constructor(@ApplicationContext private val context: Context) {
    fun isNetworkConnected(): Boolean {
        .....
    }
}

可以直接作為@Provides方法或@Inject構(gòu)造的參數(shù)使用。

流程

image

Hilt Components

介紹

在之前Dagger-Android中,我們必須創(chuàng)建諸如ActivityScope,F(xiàn)ragmentScope之類的范圍注釋,以管理對(duì)象的生命周期,

而這里只要使用@InstallIn的注解,就可以委托Hilt幫我們管理

組件的生存期:

組件 范圍 創(chuàng)建 銷毀
ApplicationComponent @Singleton Application#onCreate() Application#onDestroy()
ActivityRetainedComponent @ActivityRetainedScope Activity#onCreate()鏈接 Activity#onDestroy()鏈接
ActivityComponent @ActivityScoped Activity#onCreate() Activity#onDestroy()
FragmentComponent @FragmentScoped Fragment#onAttach() Fragment#onDestroy()
ViewComponent @ViewScoped View#super() View 被毀
ViewWithFragmentComponent @ViewScoped View#super() View 被毀
ServiceComponent @ServiceScoped Service#onCreate() Service#onDestroy()

@InstallIn模塊中確定綁定范圍時(shí),綁定上的范圍必須與component范圍匹配。例如,@InstallIn(ActivityComponent.class)模塊內(nèi)的綁定只能用限制范圍@ActivityScoped

例如我們需要在App中共享OkHttp的配置:

@Module
@InstallIn(ApplicationComponent::class)
class ApplicationModule {

    @Provides
    fun provideBaseUrl() = "...."

    @Provides
    @Singleton
    fun provideOkHttpClient() = if (BuildConfig.DEBUG) {
        val loggingInterceptor = HttpLoggingInterceptor()
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
        OkHttpClient.Builder()
            .addInterceptor(loggingInterceptor)
            .build()
    } else OkHttpClient
        .Builder()
        .build()


    @Provides
    @Singleton
    fun provideRetrofit(
        okHttpClient: OkHttpClient,
        BASE_URL: String
    ): Retrofit =
        Retrofit.Builder()
            .addConverterFactory(MoshiConverterFactory.create())
            .baseUrl(BASE_URL)
            .client(okHttpClient)
            .build()

    @Provides
    @Singleton
    fun provideApiService(retrofit: Retrofit): ApiService = retrofit.create(ApiService::class.java)

}

類似Dagger中:

@Module
class NetworkModule {
        // Hypothetical dependency on LoginRetrofitService
        @Provides
        fun provideLoginRetrofitService(
            okHttpClient: OkHttpClient
        ): LoginRetrofitService { ... }
}

@Component(modules = [NetworkModule::class])
interface ApplicationComponent {
        ...
}

這里通過(guò)@InstallIn(ApplicationComponent::class)Hilt幫我們管理ApplicationModule的生命周期

Module

module的使用基本和dagger中一樣, 用來(lái)提供一些無(wú)法用構(gòu)造@Inject的依賴, 比如接口, 第三方庫(kù)類型, Builder模式構(gòu)造的對(duì)象等.

  • @Module: 標(biāo)記一個(gè)module, 可以是一個(gè)object.
  • @Provides: 標(biāo)記方法, 提供返回值類型的依賴.這里就不需要手動(dòng)添加到@Component(modules = ...)
  • @Binds: 標(biāo)記抽象方法, 返回接口類型, 接口實(shí)現(xiàn)是方法的唯一參數(shù).
interface AnalyticsService {
  fun analyticsMethods()
}

// Constructor-injected, because Hilt needs to know how to
// provide instances of AnalyticsServiceImpl, too.
class AnalyticsServiceImpl @Inject constructor(
  ...
) : AnalyticsService { ... }

@Module
@InstallIn(ActivityComponent::class)
abstract class AnalyticsModule {

  @Binds
  abstract fun bindAnalyticsService(
    analyticsServiceImpl: AnalyticsServiceImpl
  ): AnalyticsService
}

@Provides@Binds的區(qū)別:

按照官方說(shuō)@Binds需要module是一個(gè)abstract class,@Provides需要module是一個(gè)object.而且@Binds需要在方法參數(shù)里面明確指明接口的實(shí)現(xiàn)類

但是@Provides這么用也是可以的。

Qualifier

如果要提供同一個(gè)接口的不同實(shí)現(xiàn), 可以用不同的注解來(lái)標(biāo)記. (類似于dagger中是@Named).

什么意思? 比如我們緩存接口有內(nèi)存和磁盤兩種實(shí)現(xiàn):

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class CacheInMemory

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class CacheInDisk

module中提供的時(shí)候用來(lái)標(biāo)記相應(yīng)的依賴:

@InstallIn(ApplicationComponent::class)
@Module
object CacheModule {

    @CacheInMemory
    @Singleton
    @Provides
    fun getCacheInMemory(memoryImpl: CacheSourceMemoryImpl): CacheSource = memoryImpl
    
    @CacheInDisk
    @Singleton
    @Provides
    fun getCacheInDisk(diskImpl: CacheSourceDiskImpl): CacheSource = diskImpl
}

Android types

@AndroidEntryPoint

Dagger2中,對(duì)Activity和Fragment的注入依賴的使用比較麻煩。

@Module
abstract class ActivityModule {

  @ActivityScope
  @ContributesAndroidInjector(modules = [MainActivityFragmentModule::class])
  internal abstract fun contributeMainActivity(): MainActivity

  @ActivityScope
  @ContributesAndroidInjector
  internal abstract fun contributeMovieDetailActivity(): MovieDetailActivity

  @ActivityScope
  @ContributesAndroidInjector
  internal abstract fun contributeTvDetailActivity(): TvDetailActivity

  @ActivityScope
  @ContributesAndroidInjector
  internal abstract fun contributePersonDetailActivity(): PersonDetailActivity
}

Hilt中就比較簡(jiǎn)單了,只需要@AndroidEntryPoint的注解。相當(dāng)于上面的@ContributesAndroidInjector。

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    //
    private val mJokesViewModel: JokesViewModel by viewModels()
     private val mBinding: ActivityMainBinding by viewBinding {
        ActivityMainBinding.inflate(layoutInflater)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(mBinding.root)
        mJokesViewModel.jokes.observe(this, Observer {
                ........
        }

    }
}

我們不需要編寫AndroidInjection.inject(this)或擴(kuò)展DaggerAppCompatActivity類。

這里官方給了限定范圍:

  • Activity
  • Fragment
  • View
  • Service
  • BroadcastReceiver

Hilt currently only supports activities that extend ComponentActivity and fragments that extend androidx library Fragment, not the (now deprecated) Fragment in the Android platform.

Hilt目前不直接支持 content providers.

@EntryPoint

Hilt支持最常用的Android組件, 對(duì)于默認(rèn)不支持的類型, 如果要做字段注入, 需要用@EntryPoint.

這里只是限制了字段注入的情況, 對(duì)于自定義類型我們一般習(xí)慣于用構(gòu)造注入。

必須與@InstallIn搭配使用,將interface標(biāo)記為入口點(diǎn),這樣就可以使用Hilt容器提供的依賴對(duì)象們.

如果要content provider使用Hilt

class ExampleContentProvider : ContentProvider() {

  @EntryPoint
  @InstallIn(ApplicationComponent::class)
  interface ExampleContentProviderEntryPoint {
    fun analyticsService(): AnalyticsService
  }

  ...
}

要訪問(wèn)@EntryPoint,使用靜態(tài)方法 EntryPointAccessors

class ExampleContentProvider: ContentProvider() {
    ...

  override fun query(...): Cursor {
    val appContext = context?.applicationContext ?: throw IllegalStateException()
    val hiltEntryPoint =
      EntryPointAccessors.fromApplication(appContext, ExampleContentProviderEntryPoint::class.java)

    val analyticsService = hiltEntryPoint.analyticsService()
    ...
  }
}

appContext參數(shù)要與@InstallIn(ApplicationComponent::class)保持一致。

@EntryPoint除了解決上述字段注入的 問(wèn)題,還有什么場(chǎng)景可以發(fā)揮用處?

生命周期匹配

我們利用FragmentFactoryActivityFragment之間用構(gòu)造函數(shù)傳遞數(shù)據(jù) :

class ContainerActivity : AppCompatActivity() {
    
    private var fragmentDataTest = FragmentDataTest()
    private val mBinding: ActivityContainerBinding by viewBinding {
        ActivityContainerBinding.inflate(layoutInflater)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        supportFragmentManager.fragmentFactory = EntryPointFragmentFactory(fragmentDataTest)
        super.onCreate(savedInstanceState)
        setContentView(mBinding.root)
        setSupportActionBar(mBinding.toolbar)
    }
}

看上述代碼,在沒(méi)有用Hilt之前fragmentFactory的設(shè)置應(yīng)該是在 super.onCreate()之前,但是如果用Hilt就不能這么寫了,因?yàn)樵?/p>

之前Hilt Application中已經(jīng)說(shuō)過(guò),Hilt是在super.onCreate()中進(jìn)行依賴注入的,在Hilt_ContainerActivity類中:

  @CallSuper
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    inject();
    super.onCreate(savedInstanceState);
  }

<u>Injection happens in super.onCreate().</u>

所以,如果我們要在ContainerActivity中使用FragmentFactory就該在super.onCreate()之后,那么問(wèn)題來(lái)了...我們知道FragmentFactory 負(fù)責(zé)在 Activity 和 parent Fragment 初始化 Fragment,應(yīng)該在 super.onCreate() 之前關(guān)聯(lián) FragmentFactory 和 FragmentManager設(shè)置。如果在使用Hilt注入之后還是放在編譯會(huì)報(bào)錯(cuò):

UninitializedPropertyAccessException: lateinit property mFragmentFactory has not been initialized

所以我們將FragmentManager綁定FragmentFactory的動(dòng)作放在super.onCreate()之后:

@AndroidEntryPoint
class ContainerActivity : AppCompatActivity() {

    @Inject lateinit var mFragmentFactory: EntryPointFragmentFactory

    private val mBinding: ActivityContainerBinding by viewBinding {
        ActivityContainerBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportFragmentManager.fragmentFactory = mFragmentFactory
        setContentView(mBinding.root)
        mFragmentFactory.fragmentDataTest.setData("xxx")
    }
}

這樣就可以了,但是要注意了:

<u>如果上面的ContainerActivity被意外終止而觸發(fā)重建的話是會(huì)報(bào)錯(cuò)的 :</u>

 java.lang.RuntimeException: Unable to start activity ComponentInfo{tt.reducto.daggerhiltsample/tt.reducto.daggerhiltsample.ui.entry.ContainerActivity}: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment tt.reducto.daggerhiltsample.ui.entry.EntryPointFragment: could not find Fragment constructor
 ...
Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment tt.reducto.daggerhiltsample.ui.entry.EntryPointFragment: could not find Fragment constructor
  

可以定位 Hilt_ContainerActivity中的super.onCreate(savedInstanceState);這里就涉及到FragmentManager的狀態(tài)保存與恢復(fù)

Fragment依附于Activity,而Fragment的狀態(tài)保存與恢復(fù)機(jī)制也是由Activity的相關(guān)方法觸發(fā)。Activity的方法onSaveInstanceState(Bundle outState)的參數(shù)outState是系統(tǒng)在狀態(tài)需要保存時(shí)用來(lái)提供存放持久化狀態(tài)的容器,當(dāng)系統(tǒng)觸發(fā)狀態(tài)保存時(shí),Activity下的Fragment的所有狀態(tài)便通過(guò)mFragments的saveAllState方法保存在了 FRAGMENTS_TAG 鍵中,在Activity重建 的 時(shí)候通過(guò)mFragments.restoreAllState入口將狀態(tài)恢復(fù)

在此期間Fragment的都會(huì)交由FragmentManager管理,包括我們需要注意的如何新建一個(gè)Fragment對(duì)象:

Fragment.instantiate(…)方法會(huì)根據(jù)所給的class name加載對(duì)應(yīng)的Class類,調(diào)用clazz.newInstance()新建一個(gè)全新的Fragment對(duì)象:

public static Fragment instantiate(Context context, String fname, @Nullable Bundle args) {
    ...
    Fragment f = (Fragment)clazz.newInstance();
    if (args != null) {
        args.setClassLoader(f.getClass().getClassLoader());
        f.mArguments = args;
    }
    ...
}

綜上,Activity重建后無(wú)法利用FragmentFactory重新創(chuàng)建Fragment,所以官方FragmentFactory文檔才有這么一句話:

  • Before the system restores the Fragment, if the Fragment is being recreated after a configuration change or the app’s process restart.

了解了FragmentManager綁定FragmentFactory的動(dòng)作在super.onCreate()之前執(zhí)行的必要性后,我們?cè)賮?lái)利用Hilt提供的特性解決聲明周期不匹配的問(wèn)題

@EntryPoint
@InstallIn(ActivityComponent::class)
interface ContainerActivityEntryPoint {
    fun getFragmentManager(): FragmentManager
    fun getFragmentFactory(): EntryPointFragmentFactory
}

我們利用@EntryPointContainerActivityEntryPoint對(duì)象中獲取FragmentManager和EntryPointFragmentFactory的引用:

@AndroidEntryPoint
class ContainerActivity : AppCompatActivity() {

    private val mBinding: ActivityContainerBinding by viewBinding {
        ActivityContainerBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        val entryPoint  = EntryPointAccessors.fromActivity(this,ContainerActivityEntryPoint::class.java)
        val mFragmentFactory = entryPoint.getFragmentFactory()
        entryPoint.getFragmentManager().fragmentFactory = mFragmentFactory
        super.onCreate(savedInstanceState)
        setContentView(mBinding.root)
        mFragmentFactory.fragmentDataTest.setData("xxxxxxxxxxx")
    }
        ......
}

從靜態(tài)類EntryPointAccessors中獲取定義的實(shí)例,有點(diǎn)SOLID中接口隔離原則的意思。

再次模擬Activity重建狀態(tài),一切正常。

ViewModel

之前Dagger注入ViewModel時(shí)比較麻煩,在構(gòu)造函數(shù)中創(chuàng)建帶有參數(shù)的ViewModel實(shí)例,每個(gè)ViewModel必須實(shí)現(xiàn)一個(gè)全局或每個(gè)ViewModelFactory,并實(shí)現(xiàn)ViewModelModule來(lái)綁定ViewModel

@Module
internal abstract class ViewModelModule {

  @Binds
  @IntoMap
  @ViewModelKey(MainActivityViewModel::class)
  internal abstract fun bindMainActivityViewModels(mainActivityViewModel: MainActivityViewModel): ViewModel
    ......
  }

Hilt更簡(jiǎn)單:

class JokesViewModel @ViewModelInject constructor(
    private val jokesRepository: JokesRepository,
    private val netWorkUtils: NetWorkUtils,
    @Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel() {
        ........
}

使用@ViewModelInject即可,JokesRepositoryNetWorkUtils都是由Hilt注入的。

Hilt將在后臺(tái)生成相應(yīng)的工廠類和東西。

這里有個(gè)@Assisted需要注意下:

因?yàn)樵谶@之前ViewModel中注入SavedStateHandle是比較麻煩的,由于@AssistedInject.Factory修飾接口再通過(guò)@AssistedInject注入ViewModel,最后還要通過(guò)@AssistedModule`中添加.....太太太麻煩了

看下Hilt@Assisted描述:

/**
 * Marks a parameter in a {@link androidx.hilt.lifecycle.ViewModelInject}-annotated constructor
 * or a {@link androidx.hilt.work.WorkerInject}-annotated constructor to be assisted
 * injected at runtime via a factory.
 */
// TODO(danysantiago): Remove and replace with dagger.assisted.
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface Assisted {
}

意思是Worker通過(guò)@WorkerInject構(gòu)造函數(shù)注入時(shí)要通過(guò)@Assisted修飾ContextWorkerParameters

類似:

class ExampleWorker @WorkerInject constructor(
  @Assisted appContext: Context,
  @Assisted workerParams: WorkerParameters,
  workerDependency: WorkerDependency
) : Worker(appContext, workerParams) { ... }

簡(jiǎn)單例子

利用Hilt寫個(gè)超簡(jiǎn)單的請(qǐng)求列表的小例子:Github

image

總結(jié)

  • 不用手動(dòng)創(chuàng)建Component.
  • 不用手動(dòng)調(diào)用inject()進(jìn)行字段注入.
  • 不用在Application中保存component.
  • 提供一些Scope管理他們的生命周期,只能在對(duì)應(yīng)的范圍內(nèi)進(jìn)行使用。
  • 提供了一些默認(rèn)依賴, 比如Context.

以上就是Dagger Hilt簡(jiǎn)單上手,

目前Hilt還處于alpha狀態(tài),依賴kapt,等KSP成熟之后預(yù)計(jì)效率會(huì)有進(jìn)一步提升。

當(dāng)然koin玩起來(lái)更舒心。

參考

https://developer.android.com/training/dependency-injection/hilt-android#not-supported

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

http://joebirch.co/android/exploring-dagger-hilt-application-level-code-generation/

https://www.techyourchance.com/dagger-hilt-entry-point/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容