0

I'm trying to integrate Hilt into my project, but I'm not able to get pass the initial configuration. So far I added the plugin and the dependencies, following step by step the official documentation. As soon as I tag the Application class with @HiltAndroidApp, the build fails with the following errors:

/.../app/build/generated/source/kapt/stagingDebug/..../DaggerMyApp_HiltComponents_ApplicationC.java:110: error: cannot find symbol
        return ImmutableSet.<ViewModelProvider.Factory>of();}
                           ^
  symbol:   method <Factory>of()
  location: class ImmutableSet
/.../app/build/generated/source/kapt/stagingDebug/..../DaggerMyApp_HiltComponents_ApplicationC.java:143: error: cannot find symbol
          return ImmutableSet.<ViewModelProvider.Factory>of();}
                             ^
  symbol:   method <Factory>of()
  location: class ImmutableSet
2 errors

I checked up the generated file and I identified the troublesome methods:

@Override
      public Set<ViewModelProvider.Factory> getActivityViewModelFactory() {
        return ImmutableSet.<ViewModelProvider.Factory>of();}

@Override
        public Set<ViewModelProvider.Factory> getFragmentViewModelFactory() {
          return ImmutableSet.<ViewModelProvider.Factory>of();}

I'm fairly new to Dagger and Hilt, so I don't know where this might come from. I thought it could be a dependency problem, but I didn't get any luck looking into that. I'm importing some external modules in the project, this is my gradle:

project/build.gradle

buildscript {
    apply from: 'dependencies.gradle'

    repositories {
        google()
        jcenter()
        mavenCentral()

    }
    dependencies {
        classpath "com.android.tools.build:gradle:${versions.gradle_version}"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin_version}"
        classpath "io.realm:realm-gradle-plugin:${versions.realm_version}"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:${versions.navigation_version}"
        classpath "com.google.dagger:hilt-android-gradle-plugin:${versions.hilt_version}"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()

        //  maven { url "https://jitpack.io" }
    }
}

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

app/build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
apply plugin: 'androidx.navigation.safeargs.kotlin'
apply plugin: 'dagger.hilt.android.plugin'

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId "..."
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }

    dataBinding {
        enabled = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    flavorDimensions "version"
    productFlavors {
        staging{

          ...

        }
    }
}

configurations {
    cleanedAnnotations
    compile.exclude group: 'com.intellij' , module:'annotations'
}

realm {
    syncEnabled = true
}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation jetbrains.kotlin
    implementation jetbrains.coroutines
    implementation androidx.appcompat
    implementation androidx.core_ktx
    implementation google.material
    implementation androidx.constraint_layout

    // Navigation
    implementation androidx.navigation_fragment
    implementation androidx.navigation_ui

    // Lifecycle runtime scope
    implementation androidx.runtime_ktx
    // Lifecycle viewmodel scope
    implementation androidx.viewmodel_ktx
    // Lifecycle livedata scope
    implementation androidx.livedata_ktx

    implementation androidx.legacy_support
    implementation androidx.common_java8

    // Amplify
    implementation amplifyframework.core
    implementation amplifyframework.auth_cognito

    // Multidex
    implementation androidx.multidex

    kapt kapt_ext.databinding_compiler

    implementation spongycastle.core
    implementation spongycastle.prov
    implementation zetetic.database_sqlcipher

    // Timber
    implementation logging_ext.timber

    // Retrofit
    implementation remotesource.retrofit2
    implementation remotesource.gson
    implementation remotesource.converter_gson
    implementation remotesource.okhttp3_interceptor

    // Room
    implementation androidx.room
    implementation androidx.room_ktx

    // Hilt
    implementation hilt_ext.hilt
    kapt hilt_ext.hilt_compiler

    // Test
    testImplementation test_ext.junit
    androidTestImplementation test_ext.arch_core_testing
    androidTestImplementation google.truth
    androidTestImplementation test_ext.coroutines_test
    androidTestImplementation test_ext.test_ext_junit
    androidTestImplementation test_ext.espresso_core


    implementation project(path: ':...')
    implementation project(path: ':...')
    implementation project(path: ':...')

}

dependencies.gradle

ext {

    versions = [
            gradle_version              : '4.0.1',
            kotlin_version              : '1.3.72',
            coroutines_version          : '1.3.4',
            appcompat_version           : '1.1.0',
            core_ktx_version            : '1.3.1',
            material_version            : '1.1.0',
            constraint_layout_version   : '1.1.3',
            navigation_version          : '2.3.0',
            lifecycle_ktx_version       : '2.2.0',
            legacy_support_version      : '1.0.0',
            amplifyframework_version    : '1.1.1',
            databinding_compiler_version: '3.2.0-alpha10',
            multidex_version            : '2.0.1',
            realm_version               : '6.1.0',
            room_version                : '2.3.0-alpha02',
            retrofit2_version           : '2.9.0',
            gson_version                : '2.8.6',
            converter_gson_version      : '2.9.0',
            okhttp3_interceptor_version : '4.8.0',
            timber_version              : '4.7.1',
            spongycastle_version        : '1.58.0.0',
            database_sqlcipher_version  : '4.4.0',
            junit_version               : '4.13',
            arch_core_version           : '2.1.0',
            test_ext_junit_version      : '1.1.1',
            espresso_core_version       : '3.2.0',
            truth_version               : '1.0.1',
            hilt_version                : '2.28-alpha',
            hilt_extensions_version     : '1.0.0-alpha01'
    ]

    jetbrains = [
            kotlin    : "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${versions.kotlin_version}",
            coroutines: "org.jetbrains.kotlinx:kotlinx-coroutines-core:${versions.coroutines_version}"
    ]

    androidx = [
            appcompat          : "androidx.appcompat:appcompat:${versions.appcompat_version}",
            core_ktx           : "androidx.core:core-ktx:${versions.core_ktx_version}",
            constraint_layout  : "androidx.constraintlayout:constraintlayout:${versions.constraint_layout_version}",
            navigation_fragment: "androidx.navigation:navigation-fragment-ktx:${versions.navigation_version}",
            navigation_ui      : "androidx.navigation:navigation-ui-ktx:${versions.navigation_version}",
            runtime_ktx        : "androidx.lifecycle:lifecycle-runtime-ktx:${versions.lifecycle_ktx_version}",
            viewmodel_ktx      : "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions.lifecycle_ktx_version}",
            livedata_ktx       : "androidx.lifecycle:lifecycle-livedata-ktx:${versions.lifecycle_ktx_version}",
            common_java8       : "androidx.lifecycle:lifecycle-common-java8:${versions.lifecycle_ktx_version}",
            legacy_support     : "androidx.legacy:legacy-support-v4:${versions.legacy_support_version}",
            multidex           : "androidx.multidex:multidex:${versions.multidex_version}",
            room               : "androidx.room:room-runtime:${versions.room_version}",
            room_ktx           : "androidx.room:room-ktx:${versions.room_version}"
    ]

    kapt_ext = [
            databinding_compiler: "com.android.databinding:compiler:${versions.databinding_compiler_version}",
            room_compiler       : "androidx.room:room-compiler:${versions.room_version}"
    ]

    google = [
            material: "com.google.android.material:material:${versions.material_version}",
            truth   : "com.google.truth:truth:${versions.truth_version}"
    ]

    hilt_ext = [
            hilt                    : "com.google.dagger:hilt-android:${versions.hilt_version}",
            hilt_compiler           : "com.google.dagger:hilt-android-compiler:${versions.hilt_version}",
            hilt_extensions         : "androidx.hilt:hilt-lifecycle-viewmodel:${versions.hilt_extensions_version}",
            hilt_extensions_compiler: "androidx.hilt:hilt-compiler:${versions.hilt_extensions_version}"
    ]

    remotesource = [
            retrofit2          : "com.squareup.retrofit2:retrofit:${versions.retrofit2_version}",
            gson               : "com.google.code.gson:gson:${versions.gson_version}",
            converter_gson     : "com.squareup.retrofit2:converter-gson:${versions.converter_gson_version}",
            okhttp3_interceptor: "com.squareup.okhttp3:logging-interceptor:${versions.okhttp3_interceptor_version}"
    ]

    logging_ext = [
            timber: "com.jakewharton.timber:timber:${versions.timber_version}"
    ]

    amplifyframework = [
            core        : "com.amplifyframework:core:${versions.amplifyframework_version}",
            auth_cognito: "com.amplifyframework:aws-auth-cognito:${versions.amplifyframework_version}"
    ]

    spongycastle = [
            core: "com.madgag.spongycastle:core:${versions.spongycastle_version}",
            prov: "com.madgag.spongycastle:prov:${versions.spongycastle_version}"
    ]

    zetetic = [
            database_sqlcipher: "net.zetetic:android-database-sqlcipher:${versions.database_sqlcipher_version}@aar"
    ]

    test_ext = [
            junit            : "junit:junit:${versions.junit_version}",
            arch_core_testing: "androidx.arch.core:core-testing:${versions.arch_core_version}",
            coroutines_test  : "org.jetbrains.kotlinx:kotlinx-coroutines-test:${versions.coroutines_version}",
            test_ext_junit   : "androidx.test.ext:junit:${versions.test_ext_junit_version}",
            espresso_core    : "androidx.test.espresso:espresso-core:${versions.espresso_core_version}"
    ]

}

I was hoping someone could at least point me in the right direction, right now I'm blindly trying to guess where the problem lies. Thanks.

  • where is the actual class that causes the error?..btw there is new way to inject your viewModel with the hilt. you probably injecting viewModelFactory with it. there is no need of viewModelFactory with hilt just annotate your ViewModel class ``@ViewModelInject`` and don't forget to add ``@AndroidEntryPoint`` to your mainActivity and associated fragment with it.. it would be more better just start new project and get your hands on just with the basics of hilt.. – USMAN osman Aug 08 '20 at 15:35
  • @Stefano did you find anything? I am also facing the issue suddenly and could not find any solution. – Srushti Jan 20 '21 at 07:24

1 Answers1

0

ImmutableSet comes from Guava, but you don't include Guava in your dependencies.

To do so, add to your dependencies:

implementation 'com.google.guava:guava:29.0-android'

If Hilt will code-generate references to ImmutableSet, my expectation would be that com.google.dagger:hilt-android:xx would include Guava as a transitive dependency. To confirm/deny that hypothesis, and to better understand why it might not be included, run ./gradlew :app:dependnecies. and look for any use of guava.

Jameson
  • 6,400
  • 6
  • 32
  • 53