Dagger 错误:使用作用域绑定或声明重复模块:

问题描述 投票:0回答:1

我一直在努力解决这个错误。我不知道该怎么办。我有 3 个子组件(天文学、当前天气和天气预报),我还为每个子组件创建了范围和模块。具有 App 和 AppComponent 类。

天文子:

package com.ghostdev.skycast.presentation.di

import com.ghostdev.skycast.MainActivity
import dagger.Subcomponent

@AstronomyScope
@Subcomponent(modules = [AstronomyModule::class])
interface AstronomySubComponent {

    fun inject(weatherActivity: MainActivity)

    @Subcomponent.Factory
    interface Factory {
        fun create(): AstronomySubComponent
    }
}

当前天气子:

package com.ghostdev.skycast.presentation.di

import com.ghostdev.skycast.MainActivity
import dagger.Subcomponent

@CurrentWeatherScope
@Subcomponent(modules = [CurrentWeatherModule::class])
interface CurrentWeatherSubComponent {

    fun inject(currentWeatherActivity: MainActivity)

    @Subcomponent.Factory
    interface Factory {
        fun create(): CurrentWeatherSubComponent
    }
}

天气预报子:

package com.ghostdev.skycast.presentation.di

import com.ghostdev.skycast.MainActivity
import dagger.Subcomponent

@WeatherForecastScope
@Subcomponent(modules = [WeatherForecastModule::class])
interface WeatherForecastSubComponent {

    fun inject(weatherForecastActivity: MainActivity)

    @Subcomponent.Factory
    interface Factory {
        fun create(): WeatherForecastSubComponent
    }
}

应用程序类:

package com.ghostdev.skycast

import android.app.Application
import com.ghostdev.skycast.presentation.di.AppComponent
import com.ghostdev.skycast.presentation.di.AstronomyModule
import com.ghostdev.skycast.presentation.di.AstronomySubComponent
import com.ghostdev.skycast.presentation.di.CurrentWeatherModule
import com.ghostdev.skycast.presentation.di.CurrentWeatherSubComponent
import com.ghostdev.skycast.presentation.di.DaggerAppComponent
import com.ghostdev.skycast.presentation.di.Injector
import com.ghostdev.skycast.presentation.di.WeatherDataModule
import com.ghostdev.skycast.presentation.di.WeatherForecastModule
import com.ghostdev.skycast.presentation.di.WeatherForecastSubComponent

class App: Application(), Injector {
    private lateinit var appComponent: AppComponent
    private val location = LocationProvider(applicationContext).getCurrentLocation()

    override fun onCreate() {
        super.onCreate()

        appComponent = DaggerAppComponent.builder()
            .currentWeatherModule(CurrentWeatherModule())
            .weatherForecastModule(WeatherForecastModule())
            .astronomyModule(AstronomyModule())
            .weatherDataModule(WeatherDataModule(BuildConfig.API_KEY, location))
            .build()

    }

    override fun createCurrentWeatherSubComponent(): CurrentWeatherSubComponent {
        return appComponent.currentWeatherComponent().create()
    }

    override fun createWeatherForecastSubComponent(): WeatherForecastSubComponent {
        return appComponent.weatherForecastComponent().create()
    }

    override fun createAstronomySubComponent(): AstronomySubComponent {
        return appComponent.astronomyComponent().create()
    }
}

应用程序组件:

package com.ghostdev.skycast.presentation.di

import dagger.Component
import javax.inject.Singleton

@Singleton
@Component(modules = [AppModule::class, CurrentWeatherModule::class, WeatherForecastModule::class,  AstronomyModule::class, WeatherDataModule::class])
interface AppComponent {

    fun currentWeatherComponent(): CurrentWeatherSubComponent.Factory

    fun weatherForecastComponent(): WeatherForecastSubComponent.Factory

    fun astronomyComponent(): AstronomySubComponent.Factory

}

天文学模块:

package com.ghostdev.skycast.presentation.di

import com.ghostdev.skycast.presentation.WeatherViewModelFactory
import com.ghostdev.skycast.repo.WeatherRepo
import dagger.Module
import dagger.Provides

@Module
class AstronomyModule {

    @AstronomyScope
    @Provides
    fun provideWeatherViewModelFactory(weatherRepo: WeatherRepo): WeatherViewModelFactory {
        return WeatherViewModelFactory((weatherRepo))
    }
}

当前天气模块:

package com.ghostdev.skycast.presentation.di

import com.ghostdev.skycast.presentation.WeatherViewModelFactory
import com.ghostdev.skycast.repo.WeatherRepo
import dagger.Module
import dagger.Provides


@Module
class CurrentWeatherModule {

    @CurrentWeatherScope
    @Provides
    fun provideWeatherViewModelFactory(weatherRepo: WeatherRepo): WeatherViewModelFactory {
        return WeatherViewModelFactory((weatherRepo))
    }
}

天气预报模块:

package com.ghostdev.skycast.presentation.di

import com.ghostdev.skycast.presentation.WeatherViewModelFactory
import com.ghostdev.skycast.repo.WeatherRepo
import dagger.Module
import dagger.Provides

@Module
class WeatherForecastModule {

    @WeatherForecastScope
    @Provides
    fun provideWeatherViewModelFactory(weatherRepo: WeatherRepo): WeatherViewModelFactory {
        return WeatherViewModelFactory((weatherRepo))
    }
}

天文范围:

package com.ghostdev.skycast.presentation.di

import javax.inject.Scope

@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class AstronomyScope

当前天气范围:

package com.ghostdev.skycast.presentation.di

import javax.inject.Scope

@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class CurrentWeatherScope

天气预报范围:

package com.ghostdev.skycast.presentation.di

import javax.inject.Scope

@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class WeatherForecastScope

注射器:

package com.ghostdev.skycast.presentation.di

interface Injector {

    fun createCurrentWeatherSubComponent(): CurrentWeatherSubComponent

    fun createWeatherForecastSubComponent(): WeatherForecastSubComponent

    fun createAstronomySubComponent(): AstronomySubComponent
}
android kotlin dagger-2 dagger dagger-hilt
1个回答
0
投票

您的 AppComponent 不能(也不应该)列出三个模块 AstronomyModule、CurrentWeatherModule 和 WeatherForecastModule。这些包含在各个子组件中,Dagger 知道它们是相关的子组件,因为您已将它们适当地列在 AppComponent 上。一旦您从 AppComponent 的定义中删除这些模块,一切都应该正常工作。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.