Android Hilt 基本使用及详解

·  阅读 547

Hilt 初探

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

Hilt 做的优化包括

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

Hilt 组件介绍

Hilt 组件介绍

Hilt 组件生命周期

Hilt 组件生命周期

Hilt 组件作用域

Hilt 组件作用域

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
标签: