Android Hilt 基本使用及详解

Hilt 初探

Hilt就是Android团队联系了Dagger2团队,一起开发出来的一个专门面向Android的依赖注入框架。
相比于Dagger2,Hilt最明显的特征就是:1. 简单。2. 提供了Android专属的API。

Hilt 做的优化包括

  • 无需编写大量的Component代码
  • Scope也会与Component自动绑定
  • 预定义绑定,例如 Application与Activity
  • 预定义的限定符,例如@ApplicationContext与@ActivityContext

Hilt 组件介绍

Android Hilt 基本使用及详解_第1张图片

Hilt 组件生命周期

Android Hilt 基本使用及详解_第2张图片

Hilt 组件作用域

Android Hilt 基本使用及详解_第3张图片

Hilt 集成

1、在项目结构的 build.gradle里面添加以下代码

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

2、在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"

3、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 对象的使用

1、提供一个对象 HttpObject

public class HttpObject {
}

2、编写对应Module

//详见上面的 hilt 对应组件 
@InstallIn(ApplicationComponent.class)
@Module
public class HttpModule {

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

3、注入到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()+"");
    }
}

4、在Application中注册

@HiltAndroidApp
public class MyApplication extends Application {
}

5、项目运行打印

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 接口的使用

1、提供一个待实现的接口

public interface TestInterface {

    void method();
}

2、提供一个实现接口的对象

import android.util.Log;

import javax.inject.Inject;

public class TestClass implements TestInterface {


    @Inject
    TestClass() {
    }

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

3、编写对应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);
}

4、最终使用

@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地址:点我下载

你可能感兴趣的:(Android,架构师之路,android)