androiddagger-2dagger-hilt

Error in supressing @InstallIn check whilst migrating from Dagger to Hilt using flag -Adagger.hilt.disableModulesHaveInstallInCheck=true


I have been recently trying to migrate my Android App from Dagger to Hilt.

I would like to carry out the entire migration in phases and hence was trying to suppress the Hilt warning for not using @InstallIn for modules.

Have been following the migration guide given here: https://dagger.dev/hilt/migration-guide.html

Also found the flag which disables the @InstallIn check here: https://dagger.dev/hilt/compiler-options.html#disable-install-in-check

However, I have been struggling to get this flag to work.

To understand the issue better, I tried to migrate the Dagger Codelab (https://codelabs.developers.google.com/codelabs/android-dagger/#0) to Hilt.

Didn't find success even there.

Could someone please point out the error? I have attached the build.gradle files and the errors generated below.

Project Level build.gradle file

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 Level build.grade file

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"
}

Generated warning and errors:

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]'

Solution

  • You can add the compiler option to your app/build.gradle file in the android -> defaultConfig block like this:

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