dagger2 is throwing an error: cannot be provided without an @Provides-annotated method. in my android project's build
dagger2 is throwing an error: cannot be provided without an @Provides-annotated method. in my android project's build
大家好,请帮我调试这个 dagger2 问题,我是 dagger2 的新手,刚刚开始一个项目。
public abstract interface ApplicationComponent {
^
java.util.Map<java.lang.Class<?
>,javax.inject.Provider<dagger.android.AndroidInjector.Factory<?>>> is injected at
dagger.android.DispatchingAndroidInjector(injectorFactoriesWithClassKeys, �)
dagger.android.DispatchingAndroidInjector<java.lang.Object> is injected at
com.example.dictionary.App.dispatchingServiceInjector
com.example.dictionary.App is injected at
com.example.dictionary.di.components.ApplicationComponent.inject(com.example.dictionary.App)
error: [Dagger/MissingBinding]
java.util.Map<java.lang.String,javax.inject.Provider<dagger.android.AndroidInjector.Factory<?
>>> cannot be provided without an @Provides-annotated method.
我的实现是:
interface ApiService {
companion object {
const val BASE_URL= "https://od-api.oxforddictionaries.com/"
}
@GET("api/v2/entries/en-gb/")
fun getMeaning(@Query("word") word: String): Observable<Response>
}
}
open class RemoteDataSource @Inject constructor(private val apiService: ApiService) {
fun getWordMeaning(word: String) = apiService.getMeaning(word)
}
@ApplicationScope
@Component(modules = [ApplicationModule::class, NetworkModule::class])
interface ApplicationComponent {
fun inject(app: App)
fun inject(mainActivity: MainActivity)
}
@Module
class ApplicationModule(private val application: Application) {
@ApplicationScope
@Provides
fun provideApplicationContext(): Context = application.applicationContext
@ApplicationScope
@Provides
fun provideSharedPreferences(): SharedPreferences =
PreferenceManager.getDefaultSharedPreferences(application)
@ApplicationScope
@Provides
fun providerDisplayMetrics(): DisplayMetrics = application.resources.displayMetrics
}
@Module
class NetworkModule(
private val baseUrl: String,
private val application: Application
) {
companion object {
const val CACHE_SIZE = 10 * 1024 * 1024L // 10 MiB
const val TIME_OUT = 10L // time in minutes to get the response from server
}
@ApplicationScope
@Provides
fun provideGson(): Gson = GsonBuilder().create()
@ApplicationScope
@Provides
fun provideOkHttpCache() = Cache(application.cacheDir, CACHE_SIZE)
@ApplicationScope
@Provides
fun provideOkHttpClient(cache: Cache): OkHttpClient = with(OkHttpClient.Builder()) {
writeTimeout(3, TimeUnit.MINUTES)
.connectTimeout(3, TimeUnit.MINUTES)
.readTimeout(TIME_OUT, TimeUnit.MINUTES)
cache(cache)
addInterceptor(headersInterceptor())
build()
}
@ApplicationScope
@Provides
fun provideRetrofit(gson: Gson, okHttpClient: OkHttpClient): Retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
@ApplicationScope
@Provides
fun provideApiService(retrofit: Retrofit): ApiService =
retrofit.create(ApiService::class.java)
private var authRequest: String? = null
private fun headersInterceptor() = Interceptor { chain ->
val requestBuilder = chain.request().newBuilder()
requestBuilder.addHeader("Accept", "application/json")
requestBuilder.addHeader("app_id", "SOME APP ID")
requestBuilder.addHeader("app_key", "Some API KEY")
chain.proceed(
requestBuilder.build()
)
}
}
class App : Application(), HasAndroidInjector {
@Inject
lateinit var dispatchingServiceInjector:
DispatchingAndroidInjector<Any>
override fun onCreate() {
super.onCreate()
applicationComponent.inject(this)
RxJavaPlugins.setErrorHandler {
it.printStackTrace()
}
}
override fun androidInjector(): AndroidInjector<Any> {
return dispatchingServiceInjector
}
val applicationComponent: ApplicationComponent by
lazy<ApplicationComponent>(mode = LazyThreadSafetyMode.NONE) {
DaggerApplicationComponent
.builder()
.applicationModule(ApplicationModule(this))
.networkModule(NetworkModule(ApiService.BASE_URL, this))
.build()
}
}
我尝试重新构建代码,还尝试使缓存无效并重新启动以重新安装依赖项,因为 dagger2 每次实施新的依赖项注入后都需要重新构建项目,按照 YouTube 教程来实现这个到目前为止,但无法检测到问题,虽然在构建崩溃时它指示提供@Provides,但在我的 NetworkModule、ApplicationModule 中我设置了这个 @Provides 注释。
请帮忙想出解决办法。
按照 this 答案中的建议,只需将您的应用程序 class 添加到应用程序标签下的 android 清单中。
<application ...
android:name = ".App" ...>
...
大家好,请帮我调试这个 dagger2 问题,我是 dagger2 的新手,刚刚开始一个项目。
public abstract interface ApplicationComponent {
^
java.util.Map<java.lang.Class<?
>,javax.inject.Provider<dagger.android.AndroidInjector.Factory<?>>> is injected at
dagger.android.DispatchingAndroidInjector(injectorFactoriesWithClassKeys, �)
dagger.android.DispatchingAndroidInjector<java.lang.Object> is injected at
com.example.dictionary.App.dispatchingServiceInjector
com.example.dictionary.App is injected at
com.example.dictionary.di.components.ApplicationComponent.inject(com.example.dictionary.App)
error: [Dagger/MissingBinding]
java.util.Map<java.lang.String,javax.inject.Provider<dagger.android.AndroidInjector.Factory<?
>>> cannot be provided without an @Provides-annotated method.
我的实现是:
interface ApiService {
companion object {
const val BASE_URL= "https://od-api.oxforddictionaries.com/"
}
@GET("api/v2/entries/en-gb/")
fun getMeaning(@Query("word") word: String): Observable<Response>
}
}
open class RemoteDataSource @Inject constructor(private val apiService: ApiService) {
fun getWordMeaning(word: String) = apiService.getMeaning(word)
}
@ApplicationScope
@Component(modules = [ApplicationModule::class, NetworkModule::class])
interface ApplicationComponent {
fun inject(app: App)
fun inject(mainActivity: MainActivity)
}
@Module
class ApplicationModule(private val application: Application) {
@ApplicationScope
@Provides
fun provideApplicationContext(): Context = application.applicationContext
@ApplicationScope
@Provides
fun provideSharedPreferences(): SharedPreferences =
PreferenceManager.getDefaultSharedPreferences(application)
@ApplicationScope
@Provides
fun providerDisplayMetrics(): DisplayMetrics = application.resources.displayMetrics
}
@Module
class NetworkModule(
private val baseUrl: String,
private val application: Application
) {
companion object {
const val CACHE_SIZE = 10 * 1024 * 1024L // 10 MiB
const val TIME_OUT = 10L // time in minutes to get the response from server
}
@ApplicationScope
@Provides
fun provideGson(): Gson = GsonBuilder().create()
@ApplicationScope
@Provides
fun provideOkHttpCache() = Cache(application.cacheDir, CACHE_SIZE)
@ApplicationScope
@Provides
fun provideOkHttpClient(cache: Cache): OkHttpClient = with(OkHttpClient.Builder()) {
writeTimeout(3, TimeUnit.MINUTES)
.connectTimeout(3, TimeUnit.MINUTES)
.readTimeout(TIME_OUT, TimeUnit.MINUTES)
cache(cache)
addInterceptor(headersInterceptor())
build()
}
@ApplicationScope
@Provides
fun provideRetrofit(gson: Gson, okHttpClient: OkHttpClient): Retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
@ApplicationScope
@Provides
fun provideApiService(retrofit: Retrofit): ApiService =
retrofit.create(ApiService::class.java)
private var authRequest: String? = null
private fun headersInterceptor() = Interceptor { chain ->
val requestBuilder = chain.request().newBuilder()
requestBuilder.addHeader("Accept", "application/json")
requestBuilder.addHeader("app_id", "SOME APP ID")
requestBuilder.addHeader("app_key", "Some API KEY")
chain.proceed(
requestBuilder.build()
)
}
}
class App : Application(), HasAndroidInjector {
@Inject
lateinit var dispatchingServiceInjector:
DispatchingAndroidInjector<Any>
override fun onCreate() {
super.onCreate()
applicationComponent.inject(this)
RxJavaPlugins.setErrorHandler {
it.printStackTrace()
}
}
override fun androidInjector(): AndroidInjector<Any> {
return dispatchingServiceInjector
}
val applicationComponent: ApplicationComponent by
lazy<ApplicationComponent>(mode = LazyThreadSafetyMode.NONE) {
DaggerApplicationComponent
.builder()
.applicationModule(ApplicationModule(this))
.networkModule(NetworkModule(ApiService.BASE_URL, this))
.build()
}
}
我尝试重新构建代码,还尝试使缓存无效并重新启动以重新安装依赖项,因为 dagger2 每次实施新的依赖项注入后都需要重新构建项目,按照 YouTube 教程来实现这个到目前为止,但无法检测到问题,虽然在构建崩溃时它指示提供@Provides,但在我的 NetworkModule、ApplicationModule 中我设置了这个 @Provides 注释。
请帮忙想出解决办法。
按照 this 答案中的建议,只需将您的应用程序 class 添加到应用程序标签下的 android 清单中。
<application ...
android:name = ".App" ...>
...