You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中Activity里by viewModels()报未解析引用但编译正常

Android Studio标记Activity中by viewModels()为错误,但代码可正常编译运行

Android Studio会将Activity中的by viewModels()标记为错误,但实际代码能够成功编译并正常运行。该问题仅出现在Activity中,Fragment内无此异常;改用Koin的by viewModel()也会出现同样情况。

已尝试以下解决方案,但均未奏效:

  • 清除IDE缓存(Invalidate Caches)
  • 重启IDE、重启电脑
  • 在另一台电脑打开项目
  • 参考Stack Overflow相关问题的解决方案

错误截图:
错误截图

app模块依赖

implementation(project(":domain"))
implementation(project(":data"))

implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'

def koin_version= "3.2.1"
implementation "io.insert-koin:koin-android:$koin_version"

implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.8.0-alpha01'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation "androidx.fragment:fragment-ktx:1.5.3"
implementation 'com.my.target:mytarget-sdk:5.15.4'

def lottieVersion = "5.2.0"
implementation "com.airbnb.android:lottie:$lottieVersion"

implementation 'com.google.firebase:firebase-crashlytics-ktx:18.2.13'
implementation 'com.google.firebase:firebase-config-ktx'
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation platform('com.google.firebase:firebase-bom:30.3.1')
implementation 'com.google.android.gms:play-services-auth:20.3.0'
implementation 'com.google.android.gms:play-services-oss-licenses:17.0.0'
implementation 'com.google.android.gms:play-services-location:20.0.0'

implementation 'com.github.bumptech.glide:glide:4.13.2'
annotationProcessor 'com.github.bumptech.glide:compiler:4.13.2'

项目级Build.gradle

buildscript {
    repositories {
        google()
    }
    dependencies {
        classpath 'com.google.android.gms:oss-licenses-plugin:0.10.5'
        classpath 'com.google.gms:google-services:4.3.14'
        classpath 'com.android.tools.build:gradle:7.3.0'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.2'
    }
}
plugins {
    id 'com.android.application' version '7.3.0' apply false
    id 'com.android.library' version '7.3.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
    id 'org.jetbrains.kotlin.jvm' version '1.7.10' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

内容的提问来源于stack exchange,提问作者Murzify

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 07:00:56