未解决的参考:hiltViewModel

Geo*_*man 2 android kotlin android-developer-api mobile-development android-jetpack-compose

这是我的代码,我将其作为参数传递 未解决的参考:hiltViewModel 这是用红色标记我的文件,因此我无法将我的解决方案编译到我的设备中,我也访问过https://developer.android.com/jetpack/ compose/libraries#hilt-navigation但 api 无法解决问题不知道为什么

fun NotesScreen(
    navController: NavController,
    viewModel: NotesViewModel = hiltViewModel()
) 
Run Code Online (Sandbox Code Playgroud)

这是我的应用程序构建 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
}
Run Code Online (Sandbox Code Playgroud)

这是项目构建 gradle 文件

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
}
Run Code Online (Sandbox Code Playgroud)

Fra*_*del 8

确保在您的build.gradle(app).

dependencies {
    implementation("androidx.hilt:hilt-navigation-compose:1.0.0")
}
Run Code Online (Sandbox Code Playgroud)

详细信息可以在这里找到。