错误:[Dagger/MissingBinding] SwipeRepository 不能在没有 @Provides 注释的方法的情况下提供

时间:2021-03-06 11:11:28

标签: android kotlin dagger-hilt

我有一个关于 Dagger/MissingBinding 的问题。我在 stackoverflow 上把所有相关的答案都写成了红色。他们没有解决我的问题。

错误看起来像:

error: [Dagger/MissingBinding] SwipeRepository cannot be provided without an @Provides-annotated method.

还有我的消息来源:

AppModule.kt

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    @Provides
    @Singleton
    fun provideOkHttpClient(
        clientRequestInfo: ClientRequestInfo,
        sessionLocalDataSource: SessionLocalDataSource,
        loginLocalDataSource: LoginLocalDataSource,
        keyCheckerInterceptor: KeyCheckerInterceptor,
    ): OkHttpClient =
        OkHttpClient.Builder()
            .addInterceptor(
                ClientAuthInterceptor(
                    clientRequestInfo,
                    sessionLocalDataSource,
                    loginLocalDataSource
                )
            )
            .addInterceptor(keyCheckerInterceptor)
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()

    @Provides
    @Singleton
    fun provideClientRequestInfo(): ClientRequestInfo = ClientRequestInfo.Builder()
        .appName("twit")
        .versionCode("3")
        .rotalume(false)
        .Build()

    @Provides
    @Singleton
    fun provideRetrofit(
        client: OkHttpClient, gson: Gson,
    ): Retrofit {
        return Retrofit.Builder()
            .baseUrl(LoginRetrofitService.ENDPOINT)
            .callFactory { client.newCall(it) }
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build()
    }
}

SwipeModule.kt

@Module
@InstallIn(ActivityComponent::class)
abstract class SwipeModule {
    companion object {
        @Provides
        fun provideSwipeRetrofitService(retrofit: Retrofit): SwipeRetrofitService {
            return retrofit.create(SwipeRetrofitService::class.java)
        }

        @Provides
        fun provideSwipeRemoteDataSource(SwipeRetrofitService: SwipeRetrofitService): SwipeRemoteDataSource {
            return SwipeRemoteDataSource(SwipeRetrofitService)
        }

        @Provides
        fun provideSwipeRepository(
            SwipeRemoteDataSource: SwipeRemoteDataSource
        ): SwipeRepository {
            return SwipeRepositoryImpl(SwipeRemoteDataSource)
        }

        @Provides
        fun provideGetMatchUsersUsecase(SwipeRepository:SwipeRepositoryImpl): GetMatchUsersUsecase {
            return GetMatchUsersUsecase(SwipeRepository, Dispatchers.Default)
        }
    }

    @Binds
    abstract fun bindRepository(SwipeRepository: SwipeRepositoryImpl): SwipeRepository

}

GetMatchUsersUsecase.kt

class GetMatchUsersUsecase @Inject constructor(
    private val SwipeRepository: SwipeRepository,
    private val dispatcher: CoroutineDispatcher,
) : FlowUseCase<String, SwipeResult>(dispatcher) {

    override fun execute(parameters: String): Flow<Result<SwipeResult>> {
        return flow {
            SwipeRepository.getMatchUsers().collect { result ->
                emit(result)
            }
        }
    }
}

SwipeRepository.kt

interface SwipeRepository {
    fun getMatchUsers(): Flow<Result<SwipeResult>>
}

最后,我的 build.gradle 文件:

build.gradle(项目)

buildscript {
    ext.kotlin_version = "1.4.21-2"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.0-alpha08'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.33-beta'
        classpath 'com.google.gms:google-services:4.3.4'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle(app)

plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}
android {
    compileSdkVersion 30
    defaultConfig {
        applicationId "app.twit"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 2
        versionName "0.1"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
        freeCompilerArgs += ["-Xallow-jvm-ir-dependencies", "-Xskip-prerelease-check"]
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerVersion "1.4.21-2"
        kotlinCompilerExtensionVersion "1.0.0-alpha11"
    }
}
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2"
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation "com.google.dagger:hilt-android:2.33-beta"
    kapt "com.google.dagger:hilt-android-compiler:2.33-beta"
    kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha03'
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
}

那么,请告诉我如何解决它的任何想法?

1 个答案:

答案 0 :(得分:2)

这里的问题是绑定和提供一起使用。

来自此处的文档:https://dagger.dev/dev-guide/faq.html#:~:text=Because%20%40Binds%20methods%20are%20just,implementation%20and%20will%20be%20invoked

<块引用>

为什么@Binds 和实例@Provides 方法不能放在同一个模块中?

因为@Binds 方法只是一个方法声明,所以它们是 表示为抽象方法——从未创建过任何实现,并且 没有任何东西被调用。另一方面,@Provides 方法确实 有一个实现并将被调用。

因为@Binds 方法从未实现,所以没有具体的类 创建实现这些方法。但是,实例@Provides 方法需要一个具体的类来构造一个实例 可以调用哪个方法。

解决方案:

如果你想在同一个模块中使用两者。在接口内使用绑定。如下图。

@Module(includes = [SwipeModule.Test::class])
@InstallIn(SingletonComponent::class)
object SwipeModule {


    @Provides
    @Singleton
    fun providesRepo(): SwipeRepository {
        return SwipeRepositoryImpl()
    }

    @Module
    @InstallIn(SingletonComponent::class)
    interface Test{
        @Binds
        fun bindRepository(SwipeRepository: SwipeRepositoryImpl): SwipeRepository
    }

}

试试这个方法让我知道。