没有匕首科特林中的@Inject构造函数或@Provides注释方法无法提供

时间:2019-08-25 04:44:12

标签: android kotlin dagger

我正在将我的一个项目转换为kotlin源代码。 在制作匕首时,我遇到了这个问题

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
}
} 

和组件接口

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

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

1 个答案:

答案 0 :(得分:1)

似乎我传递了错误的应用程序类。我真傻