使用标志 -Adagger.hilt.disableModulesHaveInstallInCheck=true 从 Dagger 迁移到 Hilt 时抑制 @InstallIn 检查时出错

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

我最近一直在尝试将我的 Android 应用程序从 Dagger 迁移到 Hilt。

我想分阶段进行整个迁移,因此试图抑制 Hilt 警告,因为不使用 @InstallIn 模块。

一直遵循此处给出的迁移指南:https://dagger.dev/hilt/migration-guide.html

还在这里找到了禁用@InstallIn检查的标志:https://dagger.dev/hilt/compiler-options.html#disable-install-in-check

但是,我一直在努力让这个标志发挥作用。

为了更好地理解这个问题,我尝试将 Dagger Codelab (https://codelabs.developers.google.com/codelabs/android-dagger/#0) 迁移到 Hilt。

即使在那里也没有找到成功。

有人可以指出错误吗?我已附上 build.gradle 文件和下面生成的错误。

项目级别build.gradle文件

buildscript {
    ext.kotlin_version = '1.3.61'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.dagger:hilt-android-gradle-plugin:2.28-alpha"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

应用程序级别build.grade文件

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

apply plugin: 'dagger.hilt.android.plugin'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.android.dagger"
        minSdkVersion 14
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "com.example.android.dagger.MyCustomTestRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        String sharedTestDir = 'src/sharedTest/java'
        test {
            java.srcDir sharedTestDir
        }
        androidTest {
            java.srcDir sharedTestDir
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    
    tasks.withType(JavaCompile) {
        configure(options) {
            options.compilerArgs << "-Adagger.hilt.disableModulesHaveInstallInCheck=true"
        }
    }
}

dependencies {
    def dagger_version = "2.27"

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "androidx.appcompat:appcompat:1.1.0"
    implementation "androidx.core:core-ktx:1.2.0"
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.lifecycle:lifecycle-livedata:2.2.0'

    implementation "com.google.dagger:dagger:$dagger_version"
    kapt "com.google.dagger:dagger-compiler:$dagger_version"

    testImplementation 'junit:junit:4.12'
    testImplementation 'org.mockito:mockito-core:3.3.1'
    testImplementation 'android.arch.core:core-testing:1.1.1'

    def androidx_test_version = "1.2.0"
    androidTestImplementation "androidx.test:runner:$androidx_test_version"
    androidTestImplementation "androidx.test:core-ktx:$androidx_test_version"
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    kaptAndroidTest "com.google.dagger:dagger-compiler:$dagger_version"

    implementation "com.google.dagger:hilt-android:2.28-alpha"
    kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
}

生成警告和错误

e: D:\androidLearn\dagger\android-dagger\app\build\tmp\kapt3\stubs\debug\com\example\android\dagger\di\AppSubcomponents.java:7: error: [Hilt]
public final class AppSubcomponents {
             ^
  com.example.android.dagger.di.AppSubcomponents must also be annotated with @InstallIn.
  [Hilt] Processing did not complete. See error above for details.
e: D:\androidLearn\dagger\android-dagger\app\build\tmp\kapt3\stubs\debug\com\example\android\dagger\di\StorageModule.java:7: error: [Hilt]
public abstract class StorageModule {
                ^
  com.example.android.dagger.di.StorageModule must also be annotated with @InstallIn.
  [Hilt] Processing did not complete. See error above for details.
w: warning: The following options were not recognized by any processor: '[dagger.hilt.android.internal.disableAndroidSuperclassValidation, kapt.kotlin.generated]'
android dagger-2 dagger-hilt
4个回答
17
投票

您可以将编译器选项添加到 android -> defaultConfig 块中的 app/build.gradle 文件中,如下所示:

android {
    ...
    defaultConfig {
        ...
        //TODO: remove this after migration to Hilt
        javaCompileOptions.annotationProcessorOptions.arguments['dagger.hilt.disableModulesHaveInstallInCheck'] = 'true'
    }
}

5
投票

您还可以添加相同的内容,如下所示:

defaultConfig {
     javaCompileOptions {
                annotationProcessorOptions {
                    arguments["dagger.hilt.disableModulesHaveInstallInCheck"]="true"
                }
            }
}

0
投票

如果使用 Kotlin,您可以添加

kapt
参数。

android {
    ...
}

kapt {
    arguments {
        arg("dagger.hilt.disableModulesHaveInstallInCheck", "true")
    }
}

0
投票

如果使用

ksp
,那么配置与kapt非常相似

ksp {
    arg("dagger.hilt.disableModulesHaveInstallInCheck", "true")
}
© www.soinside.com 2019 - 2024. All rights reserved.