Android依賴注入-Hilt

目錄

1、基本說明

1.1、依賴

1.2、依賴注入(DI)

1.3、依賴注入方式

1.4、依賴注入優勢

2、使用Hilt實現依賴注入

2.1、添加依賴項

2.2、Hilt & Dagger註解介紹

2.3、Hilt註解特別說明

2.4、Hilt官方示例


1、基本說明

1.1、依賴

根據需求方和提供方的作用關係分爲類依賴(直接依賴或強依賴)、方法依賴(間接依賴)、成員依賴(待定類型依賴)

不使用依賴項注入的 Car 類                                     使用依賴項注入的 Car 類
              圖1.直接依賴                                                                        圖2.間接依賴

 

1.2、依賴注入(DI)

將依賴關係傳遞給其他對象或框架,依賴注入方式分別是構造函數注入(constructor injection)字段注入(setter injection)、接口注入(interface injection),在Android中前兩種比較常用。

// Constructor
Client(Service service) {
    // Save the reference to the passed-in service inside this client
    this.service = service;
}
// Setter method
public void setService(Service service) {
    // Save the reference to the passed-in service inside this client.
    this.service = service;
}
// Service setter interface.
public interface ServiceSetter {
    public void setService(Service service);
}

// Client class
public class Client implements ServiceSetter {
    // Internal reference to the service used by this client.
    private Service service;

    // Set the service that this client is to use.
    @Override
    public void setService(Service service) {
        this.service = service;
    }
}

1.3、依賴注入方式

  • 人工依賴注入:人工編寫大量樣板代碼
  • 自動依賴注入:通過庫自動執行創建和提供依賴,它們歸來兩類分別是基於反射解決方案(運行時處理)和靜態解決方案(編譯時處理)

1.4、依賴注入優勢

  • 重用類以及分離依賴項:更容易替換依賴項的實現。由於控制反轉,代碼重用得以改進,並且類不再控制其依賴項的創建方式,而是支持任何配置。
  • 易於重構:依賴項成爲API Surface的可驗證部分,因此可以在創建對象時或編譯時進行檢查,而不是作爲實現詳情隱藏。
  • 易於測試:類不管理其依賴項,因此在測試時,可以傳入不同的實現以測試所有不同用例。

2、使用Hilt實現依賴注入

Hilt在依賴庫Dagger的基礎上構建而成,是Android中實現依賴注入的Jetpack庫。

2.1、添加依賴項

首先,將hilt-android-gradle-plugin插件添加到項目的根級build.gradle文件中:

buildscript {
    ...
    dependencies {
        ...
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.30.1-alpha'
    }
}

然後,應用Gradle插件在app/build.gradle文件中添加以下依賴項:

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

android {
    ...
    //如果需要使用Java8特性則添加
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
  }
}

dependencies {
    implementation 'com.google.dagger:hilt-android:2.30.1-alpha'
    annotationProcessor 'com.google.dagger:hilt-android-compiler:2.30.1-alpha'
}

注意:同時使用 Hilt 和數據綁定的項目需要 Android Studio 4.0 或更高版本。

2.2、Hilt & Dagger註解介紹


圖3.Hilt & Dragger Annotations

2.3、Hilt註解特別說明

  • @Qualifier用途
    當類型定義了多個綁定時,使用它來標識改類型的特定綁定,分爲預定義限定符和自定義限定符。
    預定義限定符:Hilt 提供了一些預定義限定符,例如你可能在不同的情況下需要不同的 ContextApplictionActivity)Hilt 提供了 @ApplicationContext 和 @ActivityContext 兩種限定符。
    自定義限定符
    @Qualifier
    annotation class InMemoryLogger
    
    @Qualifier
    annotation class DatabaseLogger
    
    
    @InstallIn(SingletonComponent::class)
    @Module
    abstract class LoggingDatabaseModule {
        @DatabaseLogger
        @Singleton
        @Binds
        abstract fun bindDatabaseLogger(impl:LoggerLocalDataSource):LoggerDataSource
    }
    
    @InstallIn(SingletonComponent::class)
    @Module
    abstract class LoggingInMemoryModule{
    
        @InMemoryLogger
        @Singleton
        @Binds
        abstract fun bindInMemeryLogger(impl: LoggerInMemoryDataSource):LoggerDataSource
    }

     




2.4、Hilt官方示例

git clone -b solution https://github.com/googlecodelabs/android-hilt

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章