9

我最近一直在尝试将我的 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]'
4

2 回答 2

13

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

android {
    ...
    defaultConfig {
        ...
        //TODO: remove this after migration to Hilt
        javaCompileOptions.annotationProcessorOptions.arguments['dagger.hilt.disableModulesHaveInstallInCheck'] = 'true'
    }
}
于 2020-08-11T20:49:45.220 回答
4

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

defaultConfig {
     javaCompileOptions {
                annotationProcessorOptions {
                    arguments["dagger.hilt.disableModulesHaveInstallInCheck"]="true"
                }
            }
}
于 2021-04-26T23:32:57.490 回答