'“编译器选项 dagger.hilt.android.internal.projectType 不是可识别的 Hilt 选项” - 具有多模块项目的 Android Hilt

问题描述 投票:0回答:2

我有一个项目,最近我们将该项目转换为多模块。 但是我无法运行该模块,它给了我这个错误。

我认为我的依赖项和一些配置出了问题。 以下是我的

app/build.gradle

plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
    id 'com.google.protobuf'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
    id 'kotlin-android-extensions'

}
apply plugin: 'kotlin-android'


android {
    compileSdk 33

    defaultConfig {
        minSdk 26
        targetSdk 33
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled 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 {
        viewBinding true
        dataBinding true
    }
    packagingOptions{
        pickFirst "androidsupportmultidexversion.txt"
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
    hilt {
        enableAggregatingTask = true
    }
    namespace '##.###.########.###############'
}

dependencies {
    def lifecycle_version = "2.5.1"
    def roomVersion = "2.4.3"
    def nav_version = "2.5.3"

    implementation 'androidx.core:core-ktx:1.8.0'
    implementation 'androidx.appcompat:appcompat:1.5.0'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.2'
    implementation 'androidx.multidex:multidex:2.0.1'

    // UI Tests
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4:1.2.1'

    //Material Design
    implementation 'com.google.android.material:material:1.6.1'

    def fragments_version = "1.5.2"
    implementation "androidx.fragment:fragment-ktx:$fragments_version"
    // ViewModel
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")
    // ViewModel utilities for Compose
    implementation("androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version")
    // LiveData
    implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version")
    // Lifecycles only (without ViewModel or LiveData)
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version")
    // Annotation processor
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
    // To use Kotlin annotation processing tool (kapt)
    implementation("androidx.room:room-runtime:$roomVersion")
    kapt("androidx.room:room-compiler:$roomVersion")
    // optional - Kotlin Extensions and Coroutines support for Room
    implementation("androidx.room:room-ktx:$roomVersion")
    // Coroutines
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.2")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.2")

    // Kotlin
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    kapt "org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.5.0"


    //Criptografia
    implementation 'com.madgag.spongycastle:bctls-jdk15on:1.58.0.0'

    //AWS SDK for Android
    implementation 'com.amazonaws:aws-android-sdk-core:2.51.0'
    implementation 'com.amazonaws:aws-android-sdk-auth-core:2.51.0'
    implementation 'com.amazonaws:aws-android-sdk-iot:2.36.0'

    implementation 'com.amplifyframework:aws-auth-cognito:1.37.1'

    implementation 'com.jcraft:jsch:0.1.55'

    implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
    implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'

    implementation "androidx.compose.ui:ui:1.2.1"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'

    // Coil
    implementation "io.coil-kt:coil-compose:1.4.0"

    //Protobuf
    apply plugin: 'com.google.protobuf'
    implementation 'com.google.protobuf:protobuf-lite:3.0.0'

    //Barber
    implementation("app.cash.barber:barber:0.3.3")

    implementation 'com.journeyapps:zxing-android-embedded:4.3.0'

    implementation fileTree(include: ['*.jar'], dir: 'libs')
    // G800 Lib
    implementation(name: 'libgedi-0.190121.gpos800', ext:'aar')
    implementation(name: 'SatGerLib', ext: 'aar')

    //Dagger Hilt
    implementation 'com.google.dagger:hilt-android:2.43.2'
    kapt 'com.google.dagger:hilt-compiler:2.43.2'

    implementation 'com.airbnb.android:lottie:3.4.0'

    implementation ('io.socket:socket.io-client:2.0.1') {
        // excluding org.json which is provided by Android
        exclude group: 'org.json', module: 'json'
    }

    implementation 'com.loopj.android:android-async-http:1.4.11'

    //Encryption
    implementation "androidx.security:security-crypto:1.0.0"

    //UNZIP
    implementation "org.apache.commons:commons-compress:1.21"
    implementation "androidx.core:core-ktx:+"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    //GPS
    implementation 'com.google.android.gms:play-services-location:18.0.0'

    //QR CODE GENERATOR
    implementation 'com.google.zxing:core:3.4.1'

    //Gson
    implementation 'com.google.code.gson:gson:2.9.1'

    /* Retrofit */
    implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
    implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
    implementation "com.squareup.okhttp3:okhttp:$okHttpVersion"
    implementation "com.squareup.okhttp3:logging-interceptor:$okHttpInterceptorVersion"
    implementation "com.squareup.retrofit2:converter-scalars:2.4.0"
    implementation 'com.android.support:multidex:1.0.3'
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.0.0'
    }
    plugins {
        javalite {
            artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.plugins {
                javalite { }
            }
        }
    }
}
repositories {
    mavenCentral()
}

这个是

dev/build.gradle
(子模块)

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

}

android {
    namespace 'br.com.###.dev'
    compileSdk 33

    defaultConfig {
        applicationId "br.com.###.dev"
        minSdk 26
        targetSdk 33
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        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
    }
    packagingOptions{
        pickFirst "androidsupportmultidexversion.txt"
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
    hilt {
        enableAggregatingTask = true
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation project(':app')
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
    implementation(name: 'libgedi-0.190121.gpos800', ext:'aar')
    implementation(name: 'SatGerLib', ext: 'aar')

    implementation 'com.google.dagger:hilt-android:2.43.2'
    kapt 'com.google.dagger:hilt-compiler:2.43.2'
}

这是顶层

build.gradle
文件

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = '1.7.0'
    repositories {
        google()
        mavenCentral()
        maven { url='https://jitpack.io'}

    }
    ext {
        compose_version = '1.2.1'
        /* Http */
        retrofitVersion = "2.9.0"
        okHttpVersion = "4.10.0"
        okHttpInterceptorVersion = "4.10.0"
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.1'
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.18'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20'
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:1.7.20"
        classpath "com.apollographql.apollo:apollo-gradle-plugin:2.4.6"

        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.44.2'

        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        flatDir {
            dirs 'libs'
        }
        maven { url "https://jitpack.io" }


    }
}


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

app\src\main\java\packagename\MyApplication.kt

@HiltAndroidApp
class MyApplication: Application()

如果您需要查看,我可以发布其他文件。现在我不想让它看起来很复杂。 任何帮助将不胜感激。预先感谢。

android kotlin dagger dagger-hilt kapt
2个回答
0
投票

正如我在评论中所写,首先我更新了库,以便它们在所有 build.gradle 文件中匹配。在这种情况下,它又出现了另一个错误。然后我向 dagger github repo 开了一个 issue ,他们的回答是这样的:

Application class annotated with @HiltAndroidApp has to be defined in an android application project

这意味着您的 @HiltAndroidApp 注解的类当前是 在库模块中定义(即带有插件的 build.gradle 文件 { id 'com.android.library' },这是不允许的。相反,您需要 将该类移动到应用程序模块中(即 build.gradle 文件 带有插件 {id 'com.android.application'}。

最后我们将应用程序类移至模块 dev,并将应用程序类也放入库模块中,但我们没有添加 @HiltAndroidApp。 所以它看起来像这样(库模块):

class MyApplication

以及应用模块:

@HiltAndroidApp
class MyApplication: Application()

显然我们也调整了清单文件。 我们也遇到了一些错误,但我们通过谷歌搜索修复了它们。 关键部分是在开发/模块内部添加 hilt 应用程序类。 希望对遇到类似问题的人有所帮助。


0
投票

我有完全相同的错误消息,但有不同的解决方案。我只需要更新 hilt 和 kapt 版本。我从文档中复制了 hilt 和 kapt 版本,但它不是最新的。我所做的是,在 android studio 中,右键单击应用程序模块,转到“打开模块设置”并转到“建议”。我建议更新我的模块,我猜是最新的稳定版本,它解决了问题。当然你可以在网上找到版本,但是使用IDE可能更方便。

© www.soinside.com 2019 - 2024. All rights reserved.