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

Android Studio中Kotlin协程报未解析引用但编译正常如何解决

Android Studio 2020.3.1 协程Unresolved Reference异常问题

问题描述

使用Android Studio 2020.3.1开发时,所有Kotlin协程相关方法均报Unresolved Reference错误,但代码可正常编译、应用运行无异常。唯一可能相关的前置操作是前一天新建了Git分支,新建分支时功能完全正常。
错误截图

项目配置

应用级build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
android {
    compileSdkVersion 31
    defaultConfig {
        minSdkVersion 24
        targetSdkVersion 31
        versionCode 13
        versionName "1.5.4"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        kapt {
            arguments {
                arg("room.schemaLocation", "$projectDir/schemas".toString())
            }
        }
        kotlinOptions {
            jvmTarget = '1.8'
        }
        compileOptions {
            targetCompatibility = "8"
            sourceCompatibility = "8"
        }
    }
    buildFeatures {
        viewBinding true
    }
    packagingOptions {
        exclude 'META-INF/atomicfu.kotlin_module'
    }
    sourceSets { main { java.srcDirs = ['src/main/java', 'src/main/java/utilities'] } }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-reflect:1.5.31"
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.5.0-alpha05'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    implementation 'androidx.preference:preference-ktx:1.1.1'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    implementation "androidx.viewpager2:viewpager2:1.0.0"
    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    // Logcat
    implementation 'com.squareup.logcat:logcat:0.1'
    // Room components (SQL Wrapper)
    implementation "androidx.room:room-runtime:$rootProject.roomVersion"
    implementation "androidx.room:room-ktx:$rootProject.roomVersion"
    implementation "androidx.room:room-rxjava3:$rootProject.roomVersion"
    kapt "androidx.room:room-compiler:$rootProject.roomVersion"
    androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"
    // Paging
    implementation "androidx.paging:paging-runtime-ktx:$rootProject.pagingVersion"
    implementation "androidx.paging:paging-rxjava2-ktx:$rootProject.pagingVersion"
    // Fragment
    implementation "androidx.fragment:fragment-ktx:$fragment_version"
    // Lifecycle components
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" // ViewModel
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" // LiveData
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version" // Lifecycles only (without ViewModel or LiveData)
    implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version" // Saved state module for ViewModel
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" // Annotation processor"
    implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version" // optional - helpers for implementing LifecycleOwner in a Service
    implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version" // optional - ProcessLifecycleOwner provides a lifecycle for the whole application process
    implementation "androidx.lifecycle:lifecycle-reactivestreams-ktx:$lifecycle_version" // optional - ReactiveStreams support for LiveData
    testImplementation "androidx.arch.core:core-testing:$arch_version" // optional - Test helpers for LiveData
    // RxJava
    implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
    implementation 'io.reactivex.rxjava3:rxjava:3.0.2'
    implementation 'io.reactivex.rxjava3:rxkotlin:3.0.0'
    // ViewModel Kotlin support
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.archLifecycleVersion"
    // Coroutines
    // api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines"
    // api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines"
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'
    implementation 'androidx.core:core-ktx:1.7.0'
    // Location
    implementation 'com.google.android.gms:play-services-location:18.0.0'
    // Apache common math
    implementation 'org.apache.commons:commons-math3:3.6.1'
    // CameraX core library using the camera2 implementation
    // The following line is optional, as the core library is included indirectly by camera-camera2
    implementation("androidx.camera:camera-core:$rootProject.camerax_version")
    implementation("androidx.camera:camera-camera2:$rootProject.camerax_version")
    // If you want to additionally use the CameraX Lifecycle library
    implementation("androidx.camera:camera-lifecycle:$rootProject.camerax_version")
    // If you want to additionally use the CameraX View class
    implementation("androidx.camera:camera-view:1.0.0-alpha30")
    // If you want to additionally use the CameraX Extensions library
    implementation("androidx.camera:camera-extensions:1.0.0-alpha30")
    // FireBase
    implementation platform('com.google.firebase:firebase-bom:28.4.0')
    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation 'com.google.firebase:firebase-crashlytics-ktx'
}

项目级build.gradle

buildscript {
    ext.kotlin_version = '1.5.31'
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.10'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.1'
    }
}
allprojects {
    repositories {
        google()
        mavenCentral()
        maven { url "https://jitpack.io" }
    }
}
ext {
    roomVersion = '2.3.0'
    pagingVersion = '3.0.1'
    archLifecycleVersion = '2.2.0'
    androidxArchVersion = '2.0.0'
    coroutines = '1.2.0'
    lifecycle_version = '2.4.0'
    arch_version = '2.1.0'
    fragment_version = '1.3.6'
    camerax_version = "1.0.2"
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

已尝试的无效修复方案

  • 执行Invalidate Caches并重启Android Studio
  • 重新同步gradle文件
  • 删除项目目录下的.idea和.gradle文件夹
  • 切换到此前运行正常、无任何修改的git分支
  • 重启电脑

临时解决说明

将build.gradle中的kotlinx协程依赖修改为

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2-native-mt'

后错误自动消失,但不清楚底层原因。

问题原因与官方解决方案

核心原因

该问题是Android Studio 2020.3.1内置Kotlin插件的已知索引Bug,会偶发无法正确识别1.5.0版本协程包的类结构,由于编译时使用的是独立的gradle构建链,不受IDE索引影响,所以可以正常编译运行。你切换到1.5.2-native-mt版本后问题解决,是因为该版本的包结构刚好可以被对应版本的Kotlin插件正常识别,且和你使用的Kotlin 1.5.31版本完全兼容。

推荐修复方案

  1. 优先使用Kotlin版本对应兼容区间的协程最新稳定版:Kotlin 1.5.x系列建议搭配org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2稳定版,无需使用native-mt分支版本即可同时满足IDE识别和生产环境稳定性要求;
  2. 若仍偶发索引异常,可在项目根目录的gradle.properties中添加配置kotlin.mpp.stability.nowarn=true,关闭多平台兼容告警,避免告警干扰IDE索引逻辑。

内容的提问来源于stack exchange,提问作者P. Leibner

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 19:27:02