android DaggerAppComponent不通过dagger插件生成

h5qlskok  于 2023-05-05  发布在  Android
关注(0)|答案(1)|浏览(132)

我正在开发一个应用程序,我使用dagger注入我的依赖项,当我尝试配置我的Application类时,我进入了trubble:
我有这个:
应用.kt

@HiltAndroidApp
class Application : Application(){
    private lateinit var appComponent: AppComponent

    override fun onCreate() {
        super.onCreate()
        appComponent = initDagger()
    }

    private fun initDagger() = DaggerAppComponent.Builder.build()
}

所以我唯一的模块是AppModule,它是这样的:
AppModule.kt

@Module
@InstallIn(SingletonComponent::class)
class AppModule {
    private val httpClient = OkHttpClient.Builder()

    //If it needed add interceptor in the instance of httpClient above.
@Provides
@Singleton
fun provideCodigoQRODeBarrasApi(): CodigoQRODeBarrasApi {
    return ApiClient(okHttpClientBuilder = httpClient, converterFactory = GsonConverterFactory.create()).createService(CodigoQRODeBarrasApi::class.java)
    }

    @Provides
    @Singleton
    fun provideFirmaApi(): FirmaApi {
        return ApiClient(okHttpClientBuilder = httpClient, converterFactory = GsonConverterFactory.create()).createService(FirmaApi::class.java)
    }

    @Provides
    @Singleton
    fun provideImpresoraApi(): ImpresoraApi {
        return ApiClient(okHttpClientBuilder = httpClient, converterFactory = GsonConverterFactory.create()).createService(ImpresoraApi::class.java)
    }

    @Provides
    @Singleton
    fun provideLecturaDeTarjetasApi(): LecturaDeTarjetasApi {
        return ApiClient(okHttpClientBuilder = httpClient, converterFactory = GsonConverterFactory.create()).createService(LecturaDeTarjetasApi::class.java)
    }

    @Provides
    @Singleton
    fun provideMiFareApi(): MiFareApi {
        return ApiClient(okHttpClientBuilder = httpClient, converterFactory = GsonConverterFactory.create()).createService(MiFareApi::class.java)
    }

    @Provides
    @Singleton
    fun providePINPADApi(): PINPADApi {
        return ApiClient(okHttpClientBuilder = httpClient, converterFactory = GsonConverterFactory.create()).createService(PINPADApi::class.java)
    }

    @Provides
    @Singleton
    fun provideTransaccinApi(): TransaccinApi {
        return ApiClient(okHttpClientBuilder = httpClient, converterFactory = GsonConverterFactory.create()).createService(TransaccinApi::class.java)
    }
}

我的AppComponent类:
AppComponent.kt

@Singleton
@Component(modules = [
    AppModule::class
])
interface AppComponent {
    @Component.Builder
    interface Builder {
        fun build(): AppComponent

        @BindsInstance
        fun application(application: Application): Builder
    }
}

关于build.grale的配置如下:
build.gradle:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}

android {
    namespace 'es.paytef.hioposconnector'
    compileSdk 33

    defaultConfig {
        applicationId "es.paytef.hioposconnector"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    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'
    }
    buildFeatures {
        compose true
    }
    kapt{
        generateStubs = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.3.2'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.8.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.5.1'
    implementation platform('androidx.compose:compose-bom:2022.10.00')
    implementation 'androidx.compose.ui:ui'
    implementation 'androidx.compose.ui:ui-graphics'
    implementation 'androidx.compose.ui:ui-tooling-preview'
    implementation 'androidx.compose.material3:material3'
    implementation 'androidx.appcompat:appcompat-resources:1.6.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
    debugImplementation 'androidx.compose.ui:ui-tooling'
    debugImplementation 'androidx.compose.ui:ui-test-manifest'

    // Librerías agregadas
    implementation "org.jetbrains.kotlin:kotlin-reflect:1.7.20"
    implementation 'com.google.code.gson:gson:2.9.0'
    implementation("javax.xml.stream:stax-api:1.0-2")

    // retrofit
    implementation "com.squareup.retrofit2:retrofit:2.9.0"
    implementation "com.squareup.retrofit2:converter-gson:2.9.0"
    implementation "com.squareup.okhttp3:okhttp:4.9.1"
    implementation "com.squareup.okhttp3:logging-interceptor:4.9.1"

    // hilt
    implementation "com.google.dagger:hilt-android:2.40"
    //Singleton and Inject annotation
    implementation 'javax.inject:javax.inject:1'
    implementation 'androidx.test.ext:junit-ktx:1.1.3'
    implementation 'androidx.test:runner:1.4.0'
    kapt "com.google.dagger:hilt-android-compiler:2.40"
    implementation "androidx.hilt:hilt-navigation-fragment:1.0.0"
    implementation "androidx.hilt:hilt-navigation-compose:1.0.0-alpha03"
    kapt "androidx.hilt:hilt-compiler:1.0.0-alpha03"

    //hilt libraries for custom Component.
    implementation "com.google.dagger:dagger:2.46"
    kapt "com.google.dagger:dagger-compiler:2.46"
    implementation "com.google.dagger:dagger-android-support:2.46"
    kapt "com.google.dagger:dagger-android-processor:2.46"
    //apiconnection
    implementation project(path: ':apiconnection')
}

和项目build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    dependencies{
        classpath 'com.android.tools.build:gradle:8.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20"
        classpath "com.google.dagger:hilt-android-gradle-plugin:2.46"
    }
    repositories {
        google()
    }
}
plugins {
    id 'com.android.application' version '8.0.0' apply false
    id 'com.android.library' version '8.0.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
    id 'com.google.dagger.hilt.android' version '2.46' apply false
}

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

我错过了什么??
先谢谢你了!

kgsdhlau

kgsdhlau1#

你不需要在一个实现中混合使用Hilt和Dagger。
如果您打算在项目中使用Hilt,则不需要手动创建“组件”。相反,您有几个基本组件,如SingletonComponentActivityComponent等。
而且你必须为你的模块使用@InstallIn(Component::class.java)注解来安装它在这个组件中的依赖项。这就是为什么你不需要创建自己的组件。
在你的实现中,你需要删除所有与匕首相关的东西,然后你可以像这样使用你的依赖项:

@HiltAndroidApp
class Application : Application(){
  @Inject
  lateinit var somePropertyFromSingletonComponent: SomeProperty

  ....     
  ....     
}

您可以再次 checkout 文档并根据文档修复代码。

相关问题