程序与技术分享:Android使用Dagger注入的方式初始化对象的简单使用

简介: 程序与技术分享:Android使用Dagger注入的方式初始化对象的简单使用

一. Dagger简介


Dagger 2 是 Google 开源的一款依靠注入结构,它的前身是 square 的 Dagger 1,Dagger 2 在 Android 中有着较为广泛的运用。


Dagger 2 根据 Java 注解,采用 annotationProcessor(注解处理器) 在项目编译时动态生成依靠注入需求的 Java 代码,然后咱们在合适的位置手动完结终究的依靠注入,而不是 Dagger 1 中根据反射的解决方案,所以在性能上是有保障的。


Android官方dagger介绍://代码效果参考:http://hnjlyzjd.com/xl/wz_24269.html


二. Dagger的依赖


// kapt is for implement annotation


apply plugin: 'kotlin-kapt'


// implementation dependences for dagger


implementation "com.google.dagger:dagger:2.21"


implementation "com.google.dagger:dagger-android-support:2.21"


kapt "com.google.dagger-compiler:2.21"


kapt "com.google.dagger-android-processor:2.21"


二. Dagger的配置


1. 初始化对象配置


1.1 使用@Inject注解初始化对象


注意:使用@Inject注解初始化对象时,构造函数所需的参数也必须已经使用dagger初始化,否则编译时会报错:com.record.sample.** cannot be provided without an @Inject constructor or an @Provides-annotated method


1 @Singleton


2 class RecordContractImple @Inject constructor(): RecordContract{


3   // empty body


4 }


1 @Singleton


2 class PicruteAdapter @Inject constructor(


3 context: Context


4 ): RecyclerView.Adapter{


5   // empty body


6 }


1.2 使用provide注解初始化对象


@Module


class RecordModule {


  @Provides


  @Singleton


  fun provideRecordAdapter(


    context: Context


  ): RecordAdapter = RecordAdapter(context)


  @Provides


  @Singleton


  fun provideRecordContract(


    recordContractImpl: RecordContractImpl  


  ): RecordContract = recordContractImpl


  @Provides


  @Singleton


  fun provideApplication(application: RecordApplication): RecordApplication = application


  @Provides


  @Singleton


  fun provideContext(application: RecordApplication): Context = application


}


使用@Named注解可以为同一个类初始化不同的对象,在页面中可以使用@Inject+@Named注解类使用该对象,但使用时一直报错: cannot be provided without an @Inject constructor or an @Provides-annotated method,原因未知。


// use @Named annotation to provide different instance for the same class


@Provides


@Singleton


@Named("Okhttp_client")


fun provideOkHttpClient(): OkHttpClient


@Provides


@Singleton


fun provideRetrofit(


  // use the @Named annotation to use different instance for same class


  @Named("Okhttp_client") okHttpClient: OkHttpClient


): Retrofit {


  return RetrofitProvide(...)


}


可以初始化基本类型


@Provider


fun provideAppVersion(): String = "2.21.0"


初始化Set,需要使用@ElementsIntoSet注解,网传dagger2.25以上版本有时候不需要显示提供@ElementsIntoSet注解。


@IntoSet注解告诉Dagger将返回的对象添加到Set依赖项中。


@Singleton


@Provides


@ElementsIntoSet


fun provideSet(): Set {


  val set = mutableSetOf()


  set.add("a")


  set.add("b")


  set.add("c")


  return set


}


@Singleton


@Provides


@ElementsIntoSet


fun provideSet2(): Set {


  val set = mutableSetOf()


  set.add("a")


  set.add("b")


  set.add("c")


  return set


}


@Provides


@IntoSet


fun provideInstanceForSet(): Name {


  return Name()


}


2. 绑定页面与使用Dagger初始化的对象


设置哪些页面可以使用@Inject方式获取Dagger初始化的对象。(ContributesAndroidInjector参数设置待确认)


@Module


interface BindRecordModule {


  @ContributesAndroidInjector


  fun bindMainActivity(): MainActivity


  @ContributesAndroidInjector


  fun bindHomeFragment(): HomeFragment


}


3. 定义Component配置moudle


将1和2中的配置配置到Component中,配置完点击make module后,Dagger会自动生成一个DaggerRecordComponent。


@Singleton


@Component(


  modules = 【


    // default module, must add


    AndroidSupportInjectionModule::class,  


    RecordModule::class,


    BindRecordModule::class,


  】


)


interface RecordComponent {


  @Component.Builder


  interface Builder {


    @BindsInstance


    fun application(application: Application): Builder


    fun build(): RecordComponent


  }


  fun inject(app: RecordApplication)


完成后点击Make Module,Dagger会自动生成类DaggerRecordComponent


4. 重写Application,配置Component


class RecordApplication: Application(), HasActivityInjector, HasSupportFragmentInjector {


  override fun onCreate() {


    DaggerRecordComponent.builder().


      .application(this)


      .build()


      .inject(this)


      super.onCreate()


  }


  @Inject


  internal lateinit var activityDispatchingAndroidInjector: DispatchingAndroidInjector


  override fun activityInjector(): AndroidInjector {


    return activityDispatchingAndroidInjector


  }


  @Inject


  internal lateinit var fragmentDispatchingAndroidInjector: DispatchingAndroidInjector


  override fun supportFragmentInjector(): AndroidInjector {


    return fragmentDispatchingAndroidInjector


  }


}


三. 使用


1. Activity中使用


AndroidInjection.inject(this)可以放在onAttach或者onCreate方法中初始化,只要在使用dagger初始化的地方之前调用即可。


HasSupportFragmentInjector可要可不要,原因未知。


class MainActivity: AppCompatActivity, HasSupportFragmentInjector {


  // use inject anotation to init instance, do not use private, otherwise the param will not init


  @Inject


  lateinit var recordContract: RecordContract


  override fun onCreate(savedInstanceState: Bundle?) {


    super.onCreate(savedInstanceState)


    // pass activity instance to dagger for init the param


    AndroidInjection.inject(this)


    // use the instance function directily


    val name = recordContract.getName()


  }


  @Inject


  internal lateinit var fragmentDispatchingAndroidInjector: DispatchingAndroidInjector


  override fun supportFragmentInjector(): AndroidInjector {


    return fragmentDispatchingAndroidInjector


  }


}


2. Fragment中使用


AndroidSupportInjection.inject(this)可以放在onAttach或者onCreate方法中初始化,只要在使用dagger初始化的地方之前调用即可。


class HomeFragment: Fragment() {


  // use inject anotation to init instance, do not use private, otherwise the param will not init


  @Inject


  lateinit var recordContract: RecordContract    


  override fun onAttach(context: Context) {


    super.onAttach(context)


    // pass activity instance to dagger for init the param


    AndroidSupportInjection.inject(this)


  }


}


不断更新中,技术讨论可以留言,谢谢。


end

相关文章
|
3月前
|
安全 Android开发 Kotlin
Android经典面试题之Kotlin延迟初始化的by lazy和lateinit有什么区别?
**Kotlin中的`by lazy`和`lateinit`都是延迟初始化技术。`by lazy`用于只读属性,线程安全,首次访问时初始化;`lateinit`用于可变属性,需手动初始化,非线程安全。`by lazy`支持线程安全模式选择,而`lateinit`适用于构造函数后初始化。选择依赖于属性特性和使用场景。**
107 5
Android经典面试题之Kotlin延迟初始化的by lazy和lateinit有什么区别?
|
3月前
|
Oracle Java 关系型数据库
Android studio 安装以及第一个程序
Android studio 安装以及第一个程序
86 0
|
3月前
|
Web App开发 JavaScript 前端开发
Android端使用WebView注入一段js代码实现js调用android
Android端使用WebView注入一段js代码实现js调用android
49 0
|
4月前
|
安全 网络协议 网络安全
程序与技术分享:Android应用安全之数据传输安全
程序与技术分享:Android应用安全之数据传输安全
|
4月前
|
安全 网络安全 API
kotlin安卓开发JetPack Compose 如何使用webview 打开网页时给webview注入cookie
在Jetpack Compose中使用WebView需借助AndroidView。要注入Cookie,首先在`build.gradle`添加WebView依赖,如`androidx.webkit:webkit:1.4.0`。接着创建自定义`ComposableWebView`,通过`CookieManager`设置接受第三方Cookie并注入Cookie字符串。最后在Compose界面使用这个自定义组件加载URL。注意Android 9及以上版本可能需要在网络安全配置中允许第三方Cookie。
|
5天前
|
IDE Android开发 iOS开发
探索Android与iOS开发的差异:平台选择对项目成功的影响
【9月更文挑战第27天】在移动应用开发的世界中,Android和iOS是两个主要的操作系统平台。每个系统都有其独特的开发环境、工具和用户群体。本文将深入探讨这两个平台的关键差异点,并分析这些差异如何影响应用的性能、用户体验和最终的市场表现。通过对比分析,我们将揭示选择正确的开发平台对于确保项目成功的重要作用。
|
18天前
|
Android开发 开发者 Kotlin
探索安卓开发中的新特性
【9月更文挑战第14天】本文将引导你深入理解安卓开发领域的一些最新特性,并为你提供实用的代码示例。无论你是初学者还是经验丰富的开发者,这篇文章都会给你带来新的启示和灵感。让我们一起探索吧!
|
2天前
|
开发框架 移动开发 Android开发
安卓与iOS开发中的跨平台解决方案:Flutter入门
【9月更文挑战第30天】在移动应用开发的广阔舞台上,安卓和iOS两大操作系统各自占据半壁江山。开发者们常常面临着选择:是专注于单一平台深耕细作,还是寻找一种能够横跨两大系统的开发方案?Flutter,作为一种新兴的跨平台UI工具包,正以其现代、响应式的特点赢得开发者的青睐。本文将带你一探究竟,从Flutter的基础概念到实战应用,深入浅出地介绍这一技术的魅力所在。
18 7
|
5天前
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台解决方案
【9月更文挑战第27天】在移动应用开发的广阔天地中,安卓和iOS两大操作系统如同双子星座般耀眼。开发者们在这两大平台上追逐着创新的梦想,却也面临着选择的难题。如何在保持高效的同时,实现跨平台的开发?本文将带你探索跨平台开发的魅力所在,揭示其背后的技术原理,并通过实际案例展示其应用场景。无论你是安卓的忠实拥趸,还是iOS的狂热粉丝,这篇文章都将为你打开一扇通往跨平台开发新世界的大门。
|
2天前
|
缓存 Java Linux
探索安卓开发:从新手到专家的旅程
【9月更文挑战第30天】在这篇文章中,我们将一起踏上一段激动人心的旅程,探索安卓开发的广阔世界。无论你是刚入门的新手,还是希望提升技能的开发者,本文都将为你提供宝贵的知识和指导。我们将深入探讨安卓开发的基础知识、关键概念、实用工具和最佳实践,帮助你在安卓开发领域取得更大的成功。让我们一起开启这段精彩的旅程吧!
下一篇
无影云桌面