Android 依赖注入 hilt 库的使用

hilt官网

1-什么是控制反转和依赖注入?

IOC(控制反转):全称是 Inverse of Control , 是一种思想.指的是让第3方去控制去创建对象.

DI(依赖注入):全称是 Dependency Injection , 对象的创建是通过注入的方式实现. 是IOC的一种具体实现.

2- 为啥要用依赖注入?

很多时候都是使用反射泛型实现代码的解耦, 不够灵活,比如在使用MVVM模式进行网络请求时,我们通常在ViewModel定义Repository层,然后把Api传递给Repository层.

// 定义网络接口
interface MainApi {
    default void requestList() {}
}

// 仓库抽象类
abstract class BaseRepo{}

// 首页仓库
class MainRepo extends BaseRepo {
    private MainApi api;
    public MainRepo(MainApi api) {
        this.api = api;
    }
    void requestList() {
        // 具体调用接口
        api.requestList();
    }
}

// 抽象ViewModel层
abstract class BaseViewModel {}

// ViewModel层
class MainViewModel extends BaseViewModel {
    MainRepo repo = new MainRepo(new MainApi() {});

    void requestList(){
        // 通过repo请求接口
        repo.requestList();
    }
}
复制代码

问题: 每次都要在Model层创建Repository对象和Api对象,这是重复且冗余的.

解决方案: 通过在ViewModel层和Repo层指定泛型,然后反射创建

// 定义网络接口
interface MainApi {
    default void requestList() {
    }
}

// 仓库抽象类
abstract class BaseRepo<Api> {
    private Api api;

    public Api getApi() {
        return api;
    }

    public void setApi(Api api) {
        this.api = api;
    }
}

// 首页仓库
class MainRepo extends BaseRepo<MainApi> {
    void requestList() {
        // 具体调用接口
        getApi().requestList();
    }
}

// 抽象ViewModel层
abstract class BaseViewModel<R extends BaseRepo> {
    private R repo;

    public BaseViewModel() {
        try {
            repo = crateRepoAndApi(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public R getRepo() {
        return repo;
    }
    // 反射创建Repo和Api
    public R crateRepoAndApi(BaseViewModel<R> model) throws Exception {
        Type repoType = ((ParameterizedType) model.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        R repo = (R) repoType.getClass().newInstance();
        Type apiType = ((ParameterizedType) repoType.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        String apiClassPath = apiType.getClass().toString().replace("class ", "").replace("interface ", "");
        repo.setApi(Class.forName(apiClassPath));
        return repo;
    }
}

// ViewModel层
class MainViewModel extends BaseViewModel<MainRepo> {
    void requestList() {
        // 通过repo请求接口
        getRepo().requestList();
    }
}
复制代码

通过反射可以避免在ViewModel里写创建Repo和api的代码.有没有更好的实现方式呢?

image.png

3-jectpack 中 hilt库的使用方法

1-引入包

1-在项目最外层build.gralde引入
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.37'

2-在app模块顶部
plugin "dagger.hilt.android.plugin"
plugin "kotlin-kapt"

3-在app模块内,最外层添加纠正错误类型
kapt {
    correctErrorTypes true
}

4-添加依赖
 implementation 'com.google.dagger:hilt-android:2.37'
 kapt 'com.google.dagger:hilt-compiler:2.37'
复制代码

2-必须在Application子类上添加注解@HiltAndroidApp

@HiltAndroidApp
class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
    }
}
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享