Android Hilt 基本使用及詳解

Hilt 初探

Hilt就是Android團隊聯繫了Dagger2團隊,一塊兒開發出來的一個專門面向Android的依賴注入框架。 相比於Dagger2,Hilt最明顯的特徵就是:1. 簡單。2. 提供了Android專屬的API。java

Hilt 作的優化包括

  • 無需編寫大量的Component代碼
  • Scope也會與Component自動綁定
  • 預約義綁定,例如 Application與Activity
  • 預約義的限定符,例如@ApplicationContext與@ActivityContext

Hilt 組件介紹

Hilt 組件介紹

Hilt 組件生命週期

Hilt 組件生命週期

Hilt 組件做用域

Hilt 組件做用域

Hilt 集成

一、在項目結構的 build.gradle裏面添加如下代碼

classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'
複製代碼

二、在App結構的 build.gradle裏面添加

apply plugin: 'dagger.hilt.android.plugin'
複製代碼
compileOptions {	//支持 java8
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
複製代碼
implementation "com.google.dagger:hilt-android:2.28-alpha"
   annotationProcessor "com.google.dagger:hilt-android-compiler:2.28-alpha"
複製代碼

三、App完整gradle代碼

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

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.hqk.hiltdemo"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation "com.google.dagger:hilt-android:2.28-alpha"
    annotationProcessor "com.google.dagger:hilt-android-compiler:2.28-alpha"
}
複製代碼

Hilt 對象的使用

一、提供一個對象 HttpObject

public class HttpObject {
}

複製代碼

二、編寫對應Module

//詳見上面的 hilt 對應組件 
@InstallIn(ApplicationComponent.class)
@Module
public class HttpModule {

    @Singleton
    @Provides 
    public HttpObject getHttpObject(){
        return new HttpObject();
    }
}

複製代碼

三、注入到Activity

@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
 
    @Inject
    HttpObject httpObject;

    @Inject
    HttpObject httpObject2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("hqk_MainActivity",httpObject.hashCode()+"");
        Log.i("hqk_MainActivity",httpObject2.hashCode()+"");
    }
}

複製代碼

四、在Application中註冊

@HiltAndroidApp
public class MyApplication extends Application {
}

複製代碼

五、項目運行打印

2021-07-12 16:03:31.319 3509-3509/com.hqk.hiltdemo I/hqk_MainActivity: 59372848
2021-07-12 16:03:31.319 3509-3509/com.hqk.hiltdemo I/hqk_MainActivity: 59372848

複製代碼

Hilt 接口的使用

一、提供一個待實現的接口

public interface TestInterface {

    void method();
}
複製代碼

二、提供一個實現接口的對象

import android.util.Log;

import javax.inject.Inject;

public class TestClass implements TestInterface {


    @Inject
    TestClass() {
    }

    @Override
    public void method() {
        Log.i("TestClass", "TestClass注入成功!");
    }
}

複製代碼

三、編寫對應Module

import dagger.Binds;
import dagger.Module;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ActivityComponent;

@Module
@InstallIn(ActivityComponent.class)
public abstract class TestInterfaceModule {

// @Binds
// public abstract TestInterface bindTestClass(TestClassTwo testClass //實現不一樣的 功能);

    @Binds
    public abstract TestInterface bindTestClass(TestClass testClass);
}
複製代碼

四、最終使用

@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
    @Inject
    HttpObject httpObject;

    @Inject
    HttpObject httpObject2;
    @Inject
    public TestInterface testInterface;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("hqk_MainActivity",httpObject.hashCode()+"");
        Log.i("hqk_MainActivity",httpObject2.hashCode()+"");
        testInterface.method();
    }

}
複製代碼

Demo地址:點我下載

相關文章
相關標籤/搜索