2.基于Dagger2.38.1版本源码理解hilt注解-@AndroidEntryPoint或@HiltAndroidApp

本文详细介绍了Dagger2中@AndroidEntryPoint和@HiltAndroidApp注解的使用规则,并探讨了它们在不同组件(如Application、Activity、Service、BroadcastReceiver、Fragment、View)中的代码生成过程,揭示了Hilt如何实现依赖注入。
摘要由CSDN通过智能技术生成

前言

@AndroidEntryPoint用于修饰ComponentActivity、Service、Broadcast、service、Fragment、View。@HiltAndroidApp用于修饰Application;

注解规则

  1. 节点使用@HiltAndroidApp或@AndroidEntryPoint注解修饰有且仅有一个;

  2. @HiltAndroidApp或@AndroidEntryPoint注解只能用于修饰类,而不能用于修饰接口;

  3. @HiltAndroidApp或@AndroidEntryPoint注解修饰的类不能使用泛型;

  4. @HiltAndroidApp或@AndroidEntryPoint注解value值默认使用Void.class,我们最好不要去改动;

  5. 一个项目中@HiltAndroidApp只允许出现一次;

  6. baseElement表示@HiltAndroidApp或@AndroidEntryPoint注解修饰的类的父类,如果baseElement使用了@AndroidEntryPoint注解修饰:

  • (1) @AndroidEntryPoint注解修饰的类 和 其baseElement节点要么都使用@WithFragmentBindings注解修饰,要么都不要使用该注解;

  • (2) @AndroidEntryPoint注解修饰的类不要使用@OptionalInject修饰的注解修饰 || baseElement类型使用@OptionalInject修饰的注解修饰;

  1. @HiltAndroidApp只能用于修饰Application的继承类;

  2. @AndroidEntryPoint可用于修饰Activity、Service、Broadcast_service,Fragment,View:

  • (1)@AndroidEntryPoint用于修饰Activity时,只能修饰androidx.activity.ComponentActivity的继承节点;

  • (2)@AndroidEntryPoint不能用于修饰Application的继承类。

代码生成工作

@HiltAndroidApp或@AndroidEntryPoint修饰的节点生成xx_GeneratedInjector类

只要使用@HiltAndroidApp或@AndroidEntryPoint修饰的节点都会生成xx_GeneratedInjector类。

demo:

@AndroidEntryPoint
class UserListFragment : Fragment() {...}

生成的类:

	@OriginatingElement(
	    topLevelClass = UserListFragment.class
	)
	@GeneratedEntryPoint
	@InstallIn(FragmentComponent.class)
	public interface UserListFragment_GeneratedInjector {
	  void injectUserListFragment(UserListFragment userListFragment);
	}

@HiltAndroidApp修饰的Application实现类生成类

生成Hilt_xx类。这个类的作用是实现依赖注入到@HiltAndroidApp修饰的Application类中。

demo:

@HiltAndroidApp
class MyApplication : Application() {...}

生成Hilt_MyApplication类:

//A generated base class to be extended by the @HiltAndroidApp annotated class. If using the Gradle plugin, this is swapped as the base class via bytecode transformation.
 @Generated("ApplicationGenerator")
 abstract class Hilt_MyApplication extends Application implements GeneratedComponentManagerHolder {

        private final ApplicationComponentManager componentManager =
         new ApplicationComponentManager(
          new ComponentSupplier() {
              @Override
              public Object get() {
			   //_com_aregyan_github_Application_HiltComponents内部类SingletonComponent生成的,该类后面生成。
 
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您使用的是 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、付费专栏及课程。

余额充值