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

0s0u357o  于 2023-04-18  发布在  Android
关注(0)|答案(3)|浏览(115)

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

App Levelbuild.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]'
jq6vz3qz

jq6vz3qz1#

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

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

brvekthn2#

你也可以添加类似下面的内容:

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

tjrkku2a3#

如果使用Kotlin,你可以添加一个kapt参数。

android {
    ...
}

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

相关问题