android - 如果没有 @Inject 构造函数或 dagger kotlin 中的 @Provides-annotated 方法,则无法提供

标签 android kotlin dagger

我正在将我的一个项目转换为 kotlin 源代码。 在构建 Dagger 时我遇到了这个问题

cannot be provided without an @Inject constructor or an @Provides-annotated method.

我有如下两个模块

@Module
class AppClient {
@Provides
@Singleton
fun provideHttpCache(application: Application): Cache {
    val cacheSize = 10 * 1024 * 1024
    return Cache(application.cacheDir, cacheSize.toLong())
}

@Provides
@Singleton
fun provideGson(): Gson {
    val gsonBuilder = GsonBuilder()
   gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
    //        gsonBuilder.excludeFieldsWithoutExposeAnnotation();
    return gsonBuilder.create()
}

@Provides
@Singleton
fun provideOkhttpClient(cache: Cache): OkHttpClient {
    val interceptor = HttpLoggingInterceptor()
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    val client = OkHttpClient.Builder()
    client.cache(cache)
    client.addInterceptor(interceptor)
    return client.build()
}

@Provides
@Singleton
fun provideRetrofit(gson: Gson, okHttpClient: OkHttpClient): Retrofit {
    return Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create(gson))
        .baseUrl(NetworkConstant.BASE_URL)
        .client(okHttpClient)
        .build()
}

@Provides
@Singleton
fun provideApiCall(retrofit: Retrofit): NetworkCall {
    return retrofit.create(NetworkCall::class.java)
}

@Provides
@Singleton
fun provideSharedPreference(application: Application): SharedPreferences {
    return application.applicationContext.getSharedPreferences(
        application.getString(R.string.shared_pref_name),
        MODE_PRIVATE
    )
}

@Provides
@Singleton
fun provideSharedPreferenceEditor(sharedPreferences: SharedPreferences): SharedPreferences.Editor {
    return sharedPreferences.edit()
}

@Provides
@Singleton
fun provideAppPresenter(
    sharedPreferences: SharedPreferences, editor: SharedPreferences.Editor,
    apiCall: NetworkCall): AppPresenter {
    return AppPresenter(sharedPreferences, editor, apiCall)
}

  }

第二个模块

@Module
class AppModule(val application: MyApplication) {

@Provides
@Singleton
internal fun provideApplication(): MyApplication {
    return application
}
}

应用类

class MyApplication : Application() {
lateinit var mApiComponent: AppComponent

override fun onCreate() {
    super.onCreate()
    mApiComponent = DaggerAppComponent.builder()
        .appModule(AppModule(this))
        .appClient(AppClient())
        .build()
}

fun getAppComponent(): AppComponent {
    return mApiComponent
}
} 

和组件接口(interface)

@Singleton
@Component(modules = [AppModule::class, AppClient::class])
interface AppComponent {
 fun inject(activity: LoginActivity)
 }

有人可以帮忙解决这里的问题吗?

最佳答案

看来我传递了错误的应用程序类。我真傻

关于android - 如果没有 @Inject 构造函数或 dagger kotlin 中的 @Provides-annotated 方法,则无法提供,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57643301/

相关文章:

带有 Dagger2 的 Android Kotlin,lateinit var 尚未初始化

c# - 如何将数据从 PC 发送到手机 c#

java - 在嵌套 Flowable 的 onNext 中抛出异常会导致 UndeliverableException

android-studio - Android Studio Kotlin 数据绑定(bind) : Unresolved reference on BR

android - 使用适用于 Android MVP 的 Dagger 2 设置模块和组件 - 具有多个 fragment 的 Activity

android - 是否可以使用 Dagger 2.11 现场注入(inject) Recycler View 适配器

android studio gradle同步错误

java - 将数据从一项 Activity 解析到另一项 Activity

android - CodenameOne APK 未对齐

kotlin - Camel :项目数量小于批量大小时,如何拆分然后汇总