Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved reference @HiltAndroidApp or any other Hilt annotation in Android

I have done everything properly but still, I can't use Hilt in my project.

build.gradle(:app)

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"


    buildFeatures{
        viewBinding = true
        dataBinding = true
    }



    defaultConfig {
        applicationId "com.emptysheet.pdfreader_autoscroll"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 7
        versionName "1.7"
        multiDexEnabled true

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

// To inline the bytecode built with JVM target 1.8 into
// bytecode that is being built with JVM target 1.6. (e.g. navArgs)


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.preference:preference:1.1.1'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.navigation:navigation-fragment:2.2.2'
    implementation 'androidx.navigation:navigation-ui:2.2.2'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation project(path: ':android-pdf-viewer')

    //firebase for crashlytics
    implementation 'com.google.firebase:firebase-analytics-ktx:17.4.3'
    implementation 'com.google.firebase:firebase-crashlytics:17.0.1'
    // for ads
    implementation 'com.google.android.gms:play-services-ads:19.1.0'
    // multidex
    //room
    implementation 'androidx.room:room-ktx:2.2.5'
    kapt "androidx.room:room-compiler:2.2.5"

    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0"
    //debug database
    debugImplementation 'com.amitshekhar.android:debug-db:1.0.6'
    implementation 'de.psdev.licensesdialog:licensesdialog:2.1.0'
    //support for APK less than 21
    implementation 'androidx.multidex:multidex:2.0.1'
    // in app billing
    implementation 'com.android.billingclient:billing-ktx:3.0.0'
    // Hilt
    implementation "com.google.dagger:hilt-android:2.28-alpha"
    kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"

}

build.gradle(Project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.3.72'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
        classpath 'com.google.gms:google-services:4.3.3'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.1.1'
        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
}

When I am using @HiltAndroidApp or any other Hilt annotation. It says Unresolved reference. Create annotation. I have tried re-installing Hilt but it's still not working.

I am also using Android Studio 4.0

like image 704
Rajesh kumar Avatar asked Jun 19 '20 04:06

Rajesh kumar


People also ask

How do you inject ViewModels with hilt?

Inject ViewModel objects with Hilt Provide a ViewModel by annotating it with @HiltViewModel and using the @Inject annotation in the ViewModel object's constructor. Note: To use Dagger's assisted injection with ViewModels, see the following Github issue.

What is the use of Hilt in Android?

Hilt is a dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project. Doing manual dependency injection requires you to construct every class and its dependencies by hand, and to use containers to reuse and manage dependencies.

What is hilt dependency injection?

Hilt provides a standard way to incorporate Dagger dependency injection into an Android application. The goals of Hilt are: To simplify Dagger-related infrastructure for Android apps. To create a standard set of components and scopes to ease setup, readability/understanding, and code sharing between apps.

What is @HiltViewModel?

Annotation Type HiltViewModel Identifies a ViewModel for construction injection. The ViewModel annotated with HiltViewModel will be available for creation by the dagger. hilt. android.


Video Answer


2 Answers

Try the usual:

File -> Invalidate Caches / Restart ... (and click Invalidate and Restart)

like image 160
Bartek Lipinski Avatar answered Oct 11 '22 03:10

Bartek Lipinski


  1. From the toolbar, press 'Sync Project with Gradle Files'.
  2. Invalidate Caches / Restart
like image 25
Amjad Alwareh Avatar answered Oct 11 '22 04:10

Amjad Alwareh