【问题标题】:Can not provide context for dagger injection无法为匕首注入提供上下文
【发布时间】:2023-04-06 00:06:01
【问题描述】:

我是 dagger 的新手,我想在我的课程中注入上下文和网络(使用改造)。

这是我目前的代码:

   @Module
// Safe here as we are dealing with a Dagger 2 module
@Suppress("unused")
object NetworkModule {
    @Provides
    @Reusable
    @JvmStatic
    internal fun provideMainApi(retrofit: Retrofit): MainApi {
        return retrofit.create(MainApi::class.java)
    }

    @Provides
    @Reusable
    @JvmStatic
    internal fun provideRetrofitInterface(): Retrofit {
        val interceptor = HttpLoggingInterceptor()
        interceptor.level = HttpLoggingInterceptor.Level.BODY
        val client = OkHttpClient.Builder().addInterceptor(interceptor).build()

        return Retrofit.Builder()
            .baseUrl(Constants.baseUrl)
            .addConverterFactory(MoshiConverterFactory.create())
            .addCallAdapterFactory(CoroutineCallAdapterFactory())
            .client(client)
            .build()
    }
}

@Module
class AppModule(private val app: Application) {
    @Provides
    @Singleton
    fun provideApplication() = app
}

这是我的组件:

    @Singleton
@dagger.Component(modules = arrayOf(AppModule::class, NetworkModule::class))
interface AppComponent {

    ///for injecting retrofit network
    fun injectMain(mainRepository: MainRepository)

    @dagger.Component.Builder
    interface Builder {
        fun build(): AppComponent

        fun networkModule(networkModule: NetworkModule): Builder
        fun appModule(appModule: AppModule):Builder
    }

}

我想在我的存储库中使用它,我有一个 baseRepository:

    open class BaseRepository {
    private val injector: AppComponent = DaggerAppComponent
        .builder()
        .networkModule(NetworkModule)
        .build()

    init {
        inject()
    }

    private fun inject() {
        when (this) {
            is MainRepository -> injector.injectMain(this)
        }
    }

}

当我运行应用程序时,我收到此错误“module.AppModule must be set”

我理解错误,我应该在我的基础存储库中提供 appMOdule,但问题是我在基础存储库中没有任何应用程序或上下文

我应该如何解决这个问题?

我遇到的第二个问题是这个,我听说我应该制作一次匕首并在我的整个应用程序中使用它,我不应该每次都制作它,这意味着我应该为此使用应用程序。

但是如何在应用程序类中使用注入器,这没有意义

【问题讨论】:

    标签: android dagger-2 androidinjector


    【解决方案1】:

    如果我是你,我会通过这个项目,看看那里的一切是如何完成的。

    https://github.com/google/iosched

    这里解释了这个项目是关于什么的:

    https://medium.com/androiddevelopers/google-i-o-2018-app-architecture-and-testing-f546e37fc7eb

    你也可以检查这个项目的 Dagger 分支:

    https://github.com/android/architecture-samples

    这一切都是由 Jose Alcerreca 和其他一些人完成的。这些是 Google 关于 Android 的指南,您的大部分问题都会得到解答。

    但是如何在应用程序类中使用注入器,这没有意义有一个 DaggerApplication 类,您可以根据需要扩展和注入您的 App 类。

    我要告诉你所有这些,因为我认为你是从旧资源中学习 Dagger 的。我会在你的代码中改变很多东西。

    另外,我使用 Dagger 已经 1.5 年了,而且我从来没有写过像 BaseRepository 这样的代码。我只使用并且只使用@Inject 注释,这都是通过我的与 Dagger 无关的类。在存储库中创建组件? -> 从不!

    这就是你的类应该在任何地方结束的方式。

    class Repostory @Inject constructor(
    private val dependency1: Dependency1 
    ) {}
    
    class Activity {
    @Inject lateinit var dependency2: Dependency2
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-18
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      相关资源
      最近更新 更多