记hilt的使用

记hilt的使用,第1张

概述注意注意注意!!! 自己是初学的记下来自用的可能会有些错误把 若有错误麻烦大佬提醒下谢谢大佬!!2.1是先来个代理模式 3.1然后转成hilt    首先是配置下环境(因为在网上看许多文章都没配置 )1.1(配置)步骤項目的build文件plugins{-------------------- 注意注意注意!!!  自己是初学的 记下来自用的 可能会有些错误把  若有错误麻烦大佬提醒下 谢谢大佬!!2.1是先来个代理模式  3.1然后转成hilt   

 

 


首先是配置下环境(因为在网上看许多文章都没配置  )

@H_404_19@

1. 1 (配置)步骤 項目的build文件
plugins {   ---------------------************---------    ID 'dagger.hilt.androID.plugin'    ID 'kotlin-kapt'}androID {/********/compileOptions {        sourceCompatibility JavaVersion.VERSION_1_8        targetCompatibility JavaVersion.VERSION_1_8    }    kotlinoptions {        jvmTarget = '1.8'    }}dependencIEs { implementation 'com.Google.dagger:hilt-androID:2.28-Alpha'    kapt 'com.Google.dagger:hilt-androID-compiler:2.28-Alpha'}
1.2 根目錄的bulID文件
dependencIEs {        classpath 'com.androID.tools.build:gradle:3.6.1'        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"        // hilt的        classpath 'com.Google.dagger:hilt-android-gradle-plugin:2.28-Alpha'          }

好了  同步下就完事了 

2.1(定义个接口)
interface IProcessor {    fun setshowtext()}
2.2 (需要代理的东西)我只弄俩
import androID.util.Logclass showtextCai :IProcessor {    companion object{        private const val TAG = "shuaiyu"    }    overrIDe fun setshowtext() {        Log.e(TAG, "setshowtext: ", )    }}
import androID.util.Logclass showtextHaha :IProcessor {    companion object{        private const val TAG = "shuaiyu"    }    overrIDe fun setshowtext() {        Log.e(TAG, "haha" )    }}
2.3 来个类管理这俩
object  TextHelper :IProcessor{    private lateinit var iProcessor: IProcessor     fun init(iProcessor: IProcessor ){        this.iProcessor=iProcessor    }         fun getIProcessor():IProcessor=iProcessor    overrIDe fun setshowtext() {        iProcessor.setshowtext()    }}
2.4  使用(可以放自定义的application 上 提供一个出来)
TextHelper.INSTANCE.init(new showtextHaha());
  接下来是转成hilt (先来个图)

 

 

3.1  将需要代理的构造方法放上   @Inject  另一个也这样
class showtextHaha :IProcessor {    companion object{        private const val TAG = "shuaiyu"    }    @Inject    constructor(){    }    overrIDe fun setshowtext() {        Log.e(TAG, "haha" )    }}

 

3.2   然后在自己定义的Appliction 上放上 @HiltAndroIDApp  别忘了在清单文件注册
@HiltAndroIDAppclass MyApplication : Application() {   }
3.3 在想要使用的activity上  也放上  @AndroIDEntryPoint
@AndroIDEntryPointclass SecActivity : AppCompatActivity() {    overrIDe fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentVIEw(R.layout.activity_sec)    }}
3.4 因为需要区分  所以我们要自定义注解 
@QualifIEr@Retention annotation class BindHaha()
@QualifIEr@Retentionannotation class BindCai()

因为他默认是runtime  所以我就不写

3.5   基本已经完事了  就差使用了 3.5.1 在自定义appliction 提供出来
@HiltAndroIDAppclass MyApplication : Application() {    @Inject    @BindCai    lateinit var iProcessor: IProcessor    overrIDe fun onCreate() {        super.onCreate()    }    fun getProccessor() = iProcessor}

 

 其中@BindCai  就是我门要代理的类 想要用另一个  可以直接用@BindHaha  换一下就可以了

3.5.2  在Activity使用
 (application as MyApplication).getProccessor().setshowtext()

完事了  

 写的不好 多多包涵 不要喷我 再发个图把 

 

 

 

                                                                                                                                        2021,要更努力,更充实,更清醒。

 

 

 

 

 

总结

以上是内存溢出为你收集整理的记hilt的使用全部内容,希望文章能够帮你解决记hilt的使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1052005.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存