android– 如何使用DaggerApplication和DaggerAppCompatActivity

android– 如何使用DaggerApplication和DaggerAppCompatActivity,第1张

概述我正试图了解新的Dagger2API并支持Android.我正在使用Dagger2版本2.15:implementation'com.google.dagger:dagger:2.15'implementation'com.google.dagger:dagger-android:2.15'implementation'com.google.dagger:dagger-android-support:2.15'annotatio

我正试图了解新的Dagger2 API并支持Android.我正在使用Dagger2版本2.15:

implementation 'com.Google.dagger:dagger:2.15'implementation 'com.Google.dagger:dagger-androID:2.15'implementation 'com.Google.dagger:dagger-androID-support:2.15'annotationProcessor 'com.Google.dagger:dagger-compiler:2.15'annotationProcessor 'com.Google.dagger:dagger-androID-processor:2.15'

现在在这个版本中有一些类,如DaggerApplication和DaggerAppCompatActivity,但我不知道如何让它们工作.

这是我到目前为止所做的:

我在清单中添加的我的Application类:

class BaseApplication : DaggerApplication() {    overrIDe fun applicationInjector(): AndroIDInjector<out DaggerApplication> {        return DaggerAppComponent.builder().create(this)    }}

我的AppComponent:

@Singleton@Component(modules = [    AndroIDSupportInjectionModule::class])interface AppComponent : AndroIDInjector<BaseApplication> {    @Component.Builder    abstract class Builder : AndroIDInjector.Builder<BaseApplication>()}

我在我创建的任何其他活动中扩展的基本Activity类:

abstract class BaseActivity : DaggerAppCompatActivity() {}

问题是,当我尝试制作或构建项目时,它失败了,Dagger也没有为我生成DaggerAppComponent.
我错过了什么?

@H_419_40@解决方法:

需要更多信息,但试试这个AppComponent

@Singleton@Component(modules = [AndroIDSupportInjectionModule::class])interface ApplicationComponent : AndroIDInjector<YourApplication> {    overrIDe fun inject(application: YourApplication)    @Component.Builder    interface Builder {        @BindsInstance        fun application(application: YourApplication): Builder        fun build(): ApplicationComponent    }}

从您的应用程序类

class YourApplication : DaggerApplication() {    private val applicationInjector = DaggerApplicationComponent.builder()        .application(this)        .build()    overrIDe fun applicationInjector() = applicationInjector}

也可以使用kapt而不是annotationProcessor
来自你的build.gradle:

apply plugin: 'kotlin-kapt'...kapt 'com.Google.dagger:dagger-compiler:2.15'kapt 'com.Google.dagger:dagger-androID-processor:2.15'
总结

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

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存