Hilt 简单使用篇

前言

Hilt 是Google
最新的依赖注入框架,其是基于Dagger研发,但它不同于Dagger。对于Android开发者来说,Hilt可以说专门为Android
打造,提供了一种将Dagger依赖项注入到Android应用程序的标准方法,而且创建了一组标准的组件和作用域,这些组件会自动集成到Android应用程序的各个生命周期中,以简化开发者的上手难度。

引入Hilt

dependencies {
    ...
    // hilt 依赖导入
    implementation "com.google.dagger:hilt-android:2.28-alpha"
    annotationProcessor "com.google.dagger:hilt-android-compiler:2.28-alpha"
    ...
}
 buildscript {
     ...
     dependencies {
         ...
         classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'// 导入gradle-plugin 字节码插庄
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files
     }
 }

注解

  • @HiltAndroidApp: 用于Application ,触发Hilt的代码生成,包括适用于应用程序的基类,可以使用依赖注入,应用程序容器是应用程序的父容器,这意味着其他容器可以访问其提供的依赖项。
  • @AndroidEntryPoint: 其会创建一个依赖容器,该容器遵循Android类的生命周期
  • @Inject: 用来注入的字段,其类型不能为Private,如果要告诉 Hilt 如何提供相应类型的实例,需要将 @Inject 添加到要注入的类的构造函数中。Hilt有关如何提供不同类型的实例的信息也称为绑定。

Hilt的简单用法

具体逻辑还是根据上一篇的送快递逻辑:上一篇

首先Application用到 @HiltAndroidApp

// hilt 基本上都要 用 Application来辅助
@HiltAndroidApp
public class App extends Application {
}

创建两个需要送的物品

//物品1
public class tools {}
//物品2
public class Student {}

在创建两个它们的包裹

@InstallIn(ApplicationComponent.class)
@Module
public class toolsModule {

    @Provides // 暴露对象 
    @Singleton//上方是ApplicationComponent用这个注解,作用是保持全局单例
    public tools getTools(){
        return new tools();
    }
}

ActivityComponent.class 能注入到Activity,不能注入到Application
ApplicationComponent.class 能注入到Activity, 能注入到Application

@InstallIn(ActivityComponent.class)
@Module
public class StudentModel {

    @Provides // 暴露对象 
    @ActivityScoped//上方是ActivityComponent用这个注解,作用是保持局部单例
    public Student getStudent(){
        return new Student();
    }
}

在Activity1中使用

   @Inject
    Student student;

    @Inject
    Student student1;

    @Inject
    tools tools;

    @Inject
    tools tools1;
     @Override
    protected void onCreate(Bundle savedInstanceState) {
      ...
       Log.e(TAG, "tools.hashCode():"+tools.hashCode());
        Log.e(TAG, "tools1.hashCode():"+tools1.hashCode());

        Log.e(TAG, "student.hashCode():"+student.hashCode());
        Log.e(TAG, "student1.hashCode():"+student1.hashCode());
    }
    

打印结果为:
在这里插入图片描述
跳转到Activity2中

@AndroidEntryPoint // 我是被注解的 被注入的
public class MainActivity2 extends BaseActivity {
    @Inject
    Student student2;
    @Inject
    Student student3;
    @Inject
    tools tools2;
    @Inject
    tools tools3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Log.e(TAG, "tools2.hashCode():"+tools2.hashCode());
        Log.e(TAG, "tools3.hashCode():"+tools3.hashCode());

        Log.e(TAG, "student2.hashCode():"+student2.hashCode());
        Log.e(TAG, "student3.hashCode():"+student3.hashCode());

    }
}

打印结果为:
在这里插入图片描述
会发现tool是全局单例,而student是局部单例

注入绑定接口

先定义一个简单的接口

// 接口
public interface TestInterface {
    void method();
}

接口实现类

// 接口的实现类
public class TestClassImpl implements TestInterface {

    @Inject
    TestClassImpl() {}

    @Override
    public void method() {
        Log.e("MainActivity", "恭喜恭喜你,注入成功√");
    }
}

这也得创建一个Module

@InstallIn(ActivityComponent.class)
@Module
public abstract class TestInterfaceModule {
    @Binds //        接口              与         实现类   的注入工作
    public abstract TestInterface bindTestClass(TestClassImpl testClass);
}

在Activity中使用

@AndroidEntryPoint // 我是被注解的 被注入的
public class MainActivity extends BaseActivity {

    @Inject
    TestInterface testInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
        ...
        testInterface.method();

    }
}

打印结果:
在这里插入图片描述

如果您使用的是 Hilt 2.28及以上版本,则可以不需要在 AndroidManifest.xml 文件中的 Application 标签添加 AndroidEntryPoint 注解,而是在 HiltApplication 中添加 @HiltAndroidApp 注解即可。 至于如何在代码中使用 Hilt 进行依赖注入,您可以使用 @Inject 注解来标记需要注入的依赖,同时在需要使用该依赖的地方使用 @Inject 注解进行注入。此外,您还可以通过 @Module 和 @Provides 注解来自定义依赖的注入行为。 例如,以下是一个使用 Hilt 进行依赖注入的示例代码: ```java @AndroidEntryPoint public class MyActivity extends AppCompatActivity { @Inject MyDependency myDependency; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 使用注入的依赖 myDependency.doSomething(); } } @Module @InstallIn(ActivityComponent.class) public class MyModule { @Provides MyDependency provideMyDependency() { return new MyDependencyImpl(); } } ``` 在上述代码中,我们使用 @AndroidEntryPoint 标注了 MyActivity,表示该 Activity 会使用 Hilt 进行依赖注入。通过 @Inject 注解标记了 MyDependency 字段,表示需要注入 MyDependency 类型的依赖。在 MyModule 中使用 @Provides 注解提供了 MyDependency 的实例。在 MyActivity 中,我们直接使用注入的 MyDependency 实例来调用其方法。 需要注意的是,上述代码是基于 Hilt 2.28及以上版本进行的示例。如果您使用的是旧版本的 Hilt,可能需要做出一些调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值