androidkotlinandroid-jetpack-composemobile-developmentandroid-developer-api

Unresolved reference: hiltViewModel


this is my code where i passed it as a parameter Unresolved reference: hiltViewModel this is flagging my file with red and therefore I can't compile my solution into my device also i have visited https://developer.android.com/jetpack/compose/libraries#hilt-navigation but the api can not resolve the issue don't know why

fun NotesScreen(
    navController: NavController,
    viewModel: NotesViewModel = hiltViewModel()
) 

here is my app build gradle

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
    id 'com.google.dagger.hilt.android'
}

android {
    namespace 'com.example.aer'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.aer"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.2.0'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
//    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
//    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.0"
    implementation "androidx.navigation:navigation-compose:2.5.3"
    implementation 'androidx.activity:activity-compose:1.3.1'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    implementation 'androidx.compose.material:material:1.2.0'
    implementation 'androidx.core:core-ktx:+'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
    implementation "com.google.dagger:hilt-android:2.44"
    kapt "com.google.dagger:hilt-compiler:2.44"

//    coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'

    implementation "androidx.room:room-runtime:2.5.0"
    kapt "androidx.room:room-compiler:2.5.0"

    implementation "androidx.room:room-ktx:2.5.0"

    implementation 'androidx.hilt:hilt-work:1.0.0'

    kapt 'androidx.hilt:hilt-compiler:1.0.0'

    annotationProcessor 'androidx.hilt:hilt-compiler:1.0.0'
}
kapt {
    correctErrorTypes = true
}

here is project build gradle file

buildscript {
    ext {
        compose_ui_version = '1.2.0'
    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.4.2' apply false
    id 'com.android.library' version '7.4.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.20-RC' apply false
    id 'com.google.dagger.hilt.android' version '2.44' apply false
}

Solution

  • Make sure to define this dependency in your build.gradle(app).

    dependencies {
        implementation("androidx.hilt:hilt-navigation-compose:1.0.0")
    }
    

    Details can be found here.