[英]'"The compiler option dagger.hilt.android.internal.projectType is not a recognized Hilt option" - Android Hilt with multi module project
我有一个项目,最近我们将该项目转换为多模块。 但是我无法运行它给我这个错误的模块。
我想我在依赖项和一些配置上做错了。 以下是我的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()
如果您需要查看,我可以发布其他文件。 现在我不想让它看起来很复杂。 任何帮助将不胜感激。 提前致谢。
正如我在评论中所写,首先我更新了库,以便它们在所有 build.gradle 文件中匹配。 在这种情况下,它给出了另一个错误。 然后我向 dagger github 回购提出了一个问题,他们这样回答:
Application class annotated with @HiltAndroidApp has to be defined in an android application project
这意味着您的 @HiltAndroidApp 注释 class 当前在库模块中定义(即带有插件 { id 'com.android.library' } 的 build.gradle 文件,这是不允许的。相反,您需要将 class 移动到应用程序模块中(即带有插件 {id 'com.android.application'} 的 build.gradle 文件。
所以最后我们将应用程序 class 移动到模块 dev,我们也将应用程序 class 放入库模块中,但我们没有添加 @HiltAndroidApp。 所以它看起来像这样(库模块):
class MyApplication
和应用模块:
@HiltAndroidApp
class MyApplication: Application()
显然我们也调整了清单文件。 我们也有几个错误,但我们通过谷歌搜索修复了它们。 关键部分是在 dev/module 中添加 hilt 应用程序 class。 我希望它能帮助有类似问题的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.