kotlin 未解析的引用:hiltViewModel

c3frrgcw  于 2023-04-12  发布在  Kotlin
关注(0)|答案(1)|浏览(191)

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

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

这是我应用程序构建器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
}

以下是ProjectBuildGradle文件

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

zsbz8rwp1#

请确保在build.gradle(app)中定义此依赖项。

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

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

相关问题