未解决的视图模型引用
Unresolved reference to view model
我的视图模型导入语句有一些问题 class。如果我在导致错误的两个 ViewModel 及其依赖项中清理或添加一行,我可以再成功构建一次。但是,如果我在我的 activities/fragments 中更改任何内容并尝试在不清理的情况下再次构建。
我在 gradle 文件中尝试了所有不同类型的配置,但都无济于事。
我尝试了不同的 android 工作室版本、不同的 Hilt 版本、Kotlin 版本。使缓存无效,重新启动和其他一切。请有人帮助我!
我已尝试在下面粘贴我的配置,以及我的 MainActivity 导入语句和未解析引用所指的视图模型之一。
主要Activity
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import com.fauna.fauna.appointments.AppointmentsViewModel
import com.fauna.fauna.clinic.ClinicViewModel
import com.fauna.fauna.databinding.ActivityMainBinding
import com.fauna.fauna.dischargeNotes.DischargeNotesViewModel
import com.fauna.fauna.vaccinationCards.VaccinationCardsViewModel
import com.fauna.fauna.updateApp.UpdateAppDialogFragment
import com.fauna.fauna.user.UserViewModel
import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint
class MainActivity : AppCompatActivity(), CoroutineScope {
private val vaccinationCardsViewModel: VaccinationCardsViewModel by viewModels()
//Some functionality
}
VaccinationCardsViewModel
import androidx.lifecycle.viewModelScope
import com.fauna.fauna.fragment.VaccinationCardDetail
import com.fauna.fauna.util.NotificationService
import com.fauna.fauna.util.Result
import com.fauna.fauna.util.SnackBarViewModel
import com.fauna.fauna.util.getSeconds
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.ConflatedBroadcastChannel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class VaccinationCardsViewModel @Inject constructor(
private val vaccinationCardsRepository: VaccinationCardsRepository,
private val notificationService: NotificationService
) : SnackBarViewModel() { //Some functionality}
build.gradle 文件
buildscript {
ext.kotlin_version = '1.5.21'
ext.apollo_version = '2.5.5'
ext.hilt_version = '2.37'
def nav_version = '2.3.2'
repositories {
google()
mavenCentral()
}
dependencies {
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
classpath 'com.android.tools.build:gradle:7.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle 应用文件
id("com.android.application")
id("kotlin-android")
id("com.apollographql.apollo").version("$apollo_version")
id("androidx.navigation.safeargs.kotlin")
id("kotlin-kapt")
id("dagger.hilt.android.plugin")
id('com.google.gms.google-services')
id('com.google.firebase.crashlytics')
}
android {
signingConfigs {
debugConfig {
//..omittet..
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
compileSdkVersion 30
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.fauna.fauna"
minSdkVersion 23
targetSdkVersion 30
versionCode 15
versionName "0.8"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
lintOptions {
abortOnError false
}
buildFeatures {
viewBinding true
}
buildTypes {
//..Omittet..
}
compileOptions {
// Flag to enable support for the new language APIs
coreLibraryDesugaringEnabled true
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-jdk8:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
def nav_version = "2.3.5"
implementation "androidx.navigation:navigation-runtime-ktx:$nav_version"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
// Feature module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
// Jetpack Compose Integration
implementation "androidx.navigation:navigation-compose:2.4.0-alpha06"
implementation "androidx.security:security-crypto:1.1.0-alpha03"
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
//View model
implementation "androidx.activity:activity-ktx:1.3.1"
implementation "androidx.fragment:fragment-ktx:1.3.6"
// Hilt
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
}
kapt {
correctErrorTypes = true
}
hilt {
enableExperimentalClasspathAggregation = true
}
apollo {
generateKotlinModels.set(true)
}
beforeEvaluate {
getApiKey()
}
def getSecretKey(string) {
def keysFile = file("secret.properties")
def keysProperties = new Properties()
keysProperties.load(new FileInputStream(keysFile))
def key = keysProperties[string]
println "key: " + key
return key
}
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
在未清理的情况下构建项目时出错:
Task :app:compileDevKotlin
e: /main/java/com/fauna/fauna/MainActivity.kt: (19, 41): Unresolved reference: VaccinationCardsViewModel
e: /main/java/com/fauna/fauna/MainActivity.kt: (21, 29): Unresolved reference: UserViewModel
e: /main/java/com/fauna/fauna/MainActivity.kt: (47, 44): Unresolved reference: VaccinationCardsViewModel
e: /main/java/com/fauna/fauna/MainActivity.kt: (47, 73): Not enough information to infer type variable VM
e: /main/java/com/fauna/fauna/MainActivity.kt: (47, 73): Property delegate must have a 'getValue(MainActivity, KProperty<*>)' method. None of the following functions is suitable:
public inline operator fun <T> Lazy<???>.getValue(thisRef: Any?, property: KProperty<*>): ??? defined in kotlin
e: /main/java/com/fauna/fauna/MainActivity.kt: (49, 32): Unresolved reference: UserViewModel
e: /main/java/com/fauna/fauna/MainActivity.kt: (49, 49): Not enough information to infer type variable VM
e: /main/java/com/fauna/fauna/MainActivity.kt: (49, 49): Property delegate must have a 'getValue(MainActivity, KProperty<*>)' method. None of the following functions is suitable:
public inline operator fun <T> Lazy<???>.getValue(thisRef: Any?, property: KProperty<*>): ??? defined in kotlin
e: /main/java/com/fauna/fauna/MainActivity.kt: (175, 93): Unresolved reference: it
您好,您的刀柄版本可能有误,我也遇到过类似的问题,下面的设置对我有用,可以将其更改为 2.34-beta,您能否在构建 gradle 应用程序
中尝试以下操作
//Dagger - Hilt
implementation "com.google.dagger:hilt-android:2.34-beta"
kapt "com.google.dagger:hilt-android-compiler:2.34-beta"
//extra dagger hilt dependecies for view model lifecycle attaching for your reference
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0"
implementation 'androidx.hilt:hilt-navigation-compose:1.0.0-alpha03'
并将最小 sdk 版本更改为
minSdk 24
我的 kotlin gradle 插件在项目级别是 1.5.10(如果有帮助的话)
问题出在从 Hilt 和我的 Apollo 架构文件生成的文件中。我在我的 android 项目中有一个名为 user
的包,在我的 Apollo 模式目录中有一个名为 User
的子文件夹,这显然使 android 工作室在后续构建中感到困惑。解决方案是要么删除 Apollo schema 目录中的子文件夹,要么将项目中的包命名为与 schema 目录中的子文件夹不同的名称。
我的视图模型导入语句有一些问题 class。如果我在导致错误的两个 ViewModel 及其依赖项中清理或添加一行,我可以再成功构建一次。但是,如果我在我的 activities/fragments 中更改任何内容并尝试在不清理的情况下再次构建。
我在 gradle 文件中尝试了所有不同类型的配置,但都无济于事。 我尝试了不同的 android 工作室版本、不同的 Hilt 版本、Kotlin 版本。使缓存无效,重新启动和其他一切。请有人帮助我!
我已尝试在下面粘贴我的配置,以及我的 MainActivity 导入语句和未解析引用所指的视图模型之一。
主要Activity
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import com.fauna.fauna.appointments.AppointmentsViewModel
import com.fauna.fauna.clinic.ClinicViewModel
import com.fauna.fauna.databinding.ActivityMainBinding
import com.fauna.fauna.dischargeNotes.DischargeNotesViewModel
import com.fauna.fauna.vaccinationCards.VaccinationCardsViewModel
import com.fauna.fauna.updateApp.UpdateAppDialogFragment
import com.fauna.fauna.user.UserViewModel
import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint
class MainActivity : AppCompatActivity(), CoroutineScope {
private val vaccinationCardsViewModel: VaccinationCardsViewModel by viewModels()
//Some functionality
}
VaccinationCardsViewModel
import androidx.lifecycle.viewModelScope
import com.fauna.fauna.fragment.VaccinationCardDetail
import com.fauna.fauna.util.NotificationService
import com.fauna.fauna.util.Result
import com.fauna.fauna.util.SnackBarViewModel
import com.fauna.fauna.util.getSeconds
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.ConflatedBroadcastChannel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class VaccinationCardsViewModel @Inject constructor(
private val vaccinationCardsRepository: VaccinationCardsRepository,
private val notificationService: NotificationService
) : SnackBarViewModel() { //Some functionality}
build.gradle 文件
buildscript {
ext.kotlin_version = '1.5.21'
ext.apollo_version = '2.5.5'
ext.hilt_version = '2.37'
def nav_version = '2.3.2'
repositories {
google()
mavenCentral()
}
dependencies {
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
classpath 'com.android.tools.build:gradle:7.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle 应用文件
id("com.android.application")
id("kotlin-android")
id("com.apollographql.apollo").version("$apollo_version")
id("androidx.navigation.safeargs.kotlin")
id("kotlin-kapt")
id("dagger.hilt.android.plugin")
id('com.google.gms.google-services')
id('com.google.firebase.crashlytics')
}
android {
signingConfigs {
debugConfig {
//..omittet..
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
compileSdkVersion 30
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.fauna.fauna"
minSdkVersion 23
targetSdkVersion 30
versionCode 15
versionName "0.8"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
lintOptions {
abortOnError false
}
buildFeatures {
viewBinding true
}
buildTypes {
//..Omittet..
}
compileOptions {
// Flag to enable support for the new language APIs
coreLibraryDesugaringEnabled true
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-jdk8:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
def nav_version = "2.3.5"
implementation "androidx.navigation:navigation-runtime-ktx:$nav_version"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
// Feature module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
// Jetpack Compose Integration
implementation "androidx.navigation:navigation-compose:2.4.0-alpha06"
implementation "androidx.security:security-crypto:1.1.0-alpha03"
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
//View model
implementation "androidx.activity:activity-ktx:1.3.1"
implementation "androidx.fragment:fragment-ktx:1.3.6"
// Hilt
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
}
kapt {
correctErrorTypes = true
}
hilt {
enableExperimentalClasspathAggregation = true
}
apollo {
generateKotlinModels.set(true)
}
beforeEvaluate {
getApiKey()
}
def getSecretKey(string) {
def keysFile = file("secret.properties")
def keysProperties = new Properties()
keysProperties.load(new FileInputStream(keysFile))
def key = keysProperties[string]
println "key: " + key
return key
}
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
在未清理的情况下构建项目时出错:
Task :app:compileDevKotlin
e: /main/java/com/fauna/fauna/MainActivity.kt: (19, 41): Unresolved reference: VaccinationCardsViewModel
e: /main/java/com/fauna/fauna/MainActivity.kt: (21, 29): Unresolved reference: UserViewModel
e: /main/java/com/fauna/fauna/MainActivity.kt: (47, 44): Unresolved reference: VaccinationCardsViewModel
e: /main/java/com/fauna/fauna/MainActivity.kt: (47, 73): Not enough information to infer type variable VM
e: /main/java/com/fauna/fauna/MainActivity.kt: (47, 73): Property delegate must have a 'getValue(MainActivity, KProperty<*>)' method. None of the following functions is suitable:
public inline operator fun <T> Lazy<???>.getValue(thisRef: Any?, property: KProperty<*>): ??? defined in kotlin
e: /main/java/com/fauna/fauna/MainActivity.kt: (49, 32): Unresolved reference: UserViewModel
e: /main/java/com/fauna/fauna/MainActivity.kt: (49, 49): Not enough information to infer type variable VM
e: /main/java/com/fauna/fauna/MainActivity.kt: (49, 49): Property delegate must have a 'getValue(MainActivity, KProperty<*>)' method. None of the following functions is suitable:
public inline operator fun <T> Lazy<???>.getValue(thisRef: Any?, property: KProperty<*>): ??? defined in kotlin
e: /main/java/com/fauna/fauna/MainActivity.kt: (175, 93): Unresolved reference: it
您好,您的刀柄版本可能有误,我也遇到过类似的问题,下面的设置对我有用,可以将其更改为 2.34-beta,您能否在构建 gradle 应用程序
中尝试以下操作//Dagger - Hilt
implementation "com.google.dagger:hilt-android:2.34-beta"
kapt "com.google.dagger:hilt-android-compiler:2.34-beta"
//extra dagger hilt dependecies for view model lifecycle attaching for your reference
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0"
implementation 'androidx.hilt:hilt-navigation-compose:1.0.0-alpha03'
并将最小 sdk 版本更改为
minSdk 24
我的 kotlin gradle 插件在项目级别是 1.5.10(如果有帮助的话)
问题出在从 Hilt 和我的 Apollo 架构文件生成的文件中。我在我的 android 项目中有一个名为 user
的包,在我的 Apollo 模式目录中有一个名为 User
的子文件夹,这显然使 android 工作室在后续构建中感到困惑。解决方案是要么删除 Apollo schema 目录中的子文件夹,要么将项目中的包命名为与 schema 目录中的子文件夹不同的名称。