Android Studio 未解决的参考。工程编译

Android Studio unresolved reference. Project compiles

升级 Android Studio 后,一个没有问题的项目开始在编辑器中显示问题。我有很多 Unresolved Reference 错误。支持库(support-v4、support-v7)下的任何内容。

在上面的屏幕截图中,任何显示为红色的内容都没有解决并显示为错误。 我也在使用 Lifecycle 组件和 Room 数据库。他们似乎也有问题。看起来可以找到接口,但 类 找不到。

例如,在我的 类 之一中使用 Room

android.arch.persistence.room.Databaseandroid.arch.persistence.room.TypeConverters 正确解析,但

android.arch.persistence.room.Roomandroid.arch.persistence.room.RoomDatabase 没有。

注意:该项目在 Android 模拟器和设备上构建和运行良好,没有任何问题。它使用 Android Studio 上的构建按钮构建并运行,没有任何错误。我没有收到任何 Class not found 错误。这只是 Android Studio 编辑器中的一个问题。我已经重新启动 Android Studio,清理并重建项目。

这是我的项目构建文件:

buildscript {
  ext.kotlin_version = '1.2.70'
  ext.serialization_version = '0.6.2'
  ext.gradle_plugin_version = '3.2.0'

  repositories {
    google()
    jcenter()
    maven { url "https://jitpack.io" }
    maven { url "https://kotlin.bintray.com/kotlinx" }
  }
  dependencies {
    classpath "com.android.tools.build:gradle:3.2.1"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "org.jetbrains.kotlinx:kotlinx-gradle-serialization-plugin:$serialization_version"
  }
}

allprojects {
  repositories {
    google()
    jcenter()
    maven { url 'https://jitpack.io' }
    maven { url "https://kotlin.bintray.com/kotlinx" }
  }
}

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

ext {
  roomVersion = '1.1.1'
  archLifecycleVersion = '1.1.1'
  buildToolsVersion = '28.0.3'
  supportLibVersion = '28.0.0'
}

和模块构建文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlinx-serialization'

android {
  compileSdkVersion 28

  defaultConfig {
    applicationId "com.example.project"
    minSdkVersion 19
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    vectorDrawables.useSupportLibrary = true
    multiDexEnabled true
    kapt {
        arguments {
            arg("room.schemaLocation", "$projectDir/schemas".toString())
        }
    }
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
flavorDimensions 'version'
productFlavors {
    live {
        dimension 'version'
    }
    dev {
        dimension 'version'
        versionNameSuffix '-dev'
    }
}
buildToolsVersion "$rootProject.buildToolsVersion"
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
dataBinding {
    enabled = true
  }
}

kotlin {
  experimental {
    coroutines 'enable'
  }
}



dependencies {
implementation "com.android.support:multidex:1.0.3"
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"

// Support and google services
implementation "com.android.support:support-compat:$rootProject.supportLibVersion"
implementation "com.android.support:support-core-utils:$rootProject.supportLibVersion"
implementation "com.android.support:support-core-ui:$rootProject.supportLibVersion"
implementation "com.android.support:support-media-compat:$rootProject.supportLibVersion"
implementation "com.android.support:support-fragment:$rootProject.supportLibVersion"
implementation "com.android.support:design:$rootProject.supportLibVersion"
implementation "com.android.support:appcompat-v7:$rootProject.supportLibVersion"
implementation "com.android.support:gridlayout-v7:$rootProject.supportLibVersion"
implementation "com.android.support:preference-v7:$rootProject.supportLibVersion"
implementation "com.android.support.constraint:constraint-layout:1.1.3"
implementation "com.android.support:support-annotations:$rootProject.supportLibVersion"
implementation "com.android.support:support-vector-drawable:$rootProject.supportLibVersion"
implementation "com.android.support:recyclerview-v7:$rootProject.supportLibVersion"
implementation "com.google.android.gms:play-services-plus:15.0.1"

// Rx
implementation "io.reactivex.rxjava2:rxandroid:2.1.0"
implementation "io.reactivex.rxjava2:rxjava:2.2.2"

// Retrofit
implementation "com.google.code.gson:gson:2.8.5"
implementation "com.squareup.retrofit2:retrofit:2.4.0"
implementation "com.squareup.retrofit2:converter-gson:2.4.0"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.4.0"
implementation "com.squareup.okhttp3:logging-interceptor:3.11.0"

// Testing
testImplementation "junit:junit:4.12"
androidTestImplementation "com.android.support.test:runner:1.0.2"
androidTestImplementation "com.android.support.test.espresso:espresso-core:3.0.2"

///---
// java 8
implementation "android.arch.lifecycle:common-java8:$archLifecycleVersion"

// Room components
implementation "android.arch.persistence.room:runtime:$rootProject.roomVersion"
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"
androidTestImplementation "android.arch.persistence.room:testing:$rootProject.roomVersion"
kapt "android.arch.persistence.room:compiler:$rootProject.roomVersion"

// Lifecycle components
implementation "android.arch.lifecycle:extensions:$rootProject.archLifecycleVersion"
kapt "android.arch.lifecycle:compiler:$rootProject.archLifecycleVersion"
annotationProcessor "android.arch.lifecycle:compiler:$rootProject.archLifecycleVersion"

}

我遇到了同样的问题。它与新的 AndroidX 库和迁移有关。

Try File -> Open -> and click build.gradle to reopen project.

我同意 Stanislav Mukhametshin 的观点,这可能与新的 AndroidX 库和迁移有关。我已经清理并重建了项目,但这没有用。所以,我尝试了这种方式,错误消失了。

1) 转到 File 关闭项目

2) 重新打开 同一个项目。

简单的解决方案是:

1: 转到文件并关闭项目。

2:然后转到文件并单击 "Open Recent" 并打开您的项目。

这个技巧对我有用。

请从应用程序目录中删除 .idea 和 .gradle 项目并使其无效并缓存 重新启动 Android Studio。 这将解决此问题。