Android Studio unresolved reference. Project compi

2020-08-10 07:18发布

问题:

After upgrading Android Studio, a project with no issues started showing issues in the editor. I have lots of Unresolved Reference errors. Anything under the support libraries (support-v4, support-v7).

In the screen capture above, anything showing in red isn't resolving and showing as an error. I am also using Lifecycle components and Room database. They seem to have issues as well. Looks like interfaces can be found but classes can't.

For example, in one of my classes using Room,

android.arch.persistence.room.Database and android.arch.persistence.room.TypeConverters resolve correctly, but

android.arch.persistence.room.Room and android.arch.persistence.room.RoomDatabase do not.

Note: The project builds and runs fine on Android emulators and devices without any issues. It builds and runs using the build button on Android Studio without any errors. I'm not getting any Class not found errors. This is just an issue inside the Android Studio editor. I have already restarted Android Studio, cleaned and rebuilt the project.

Here's my project build file:

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

And module build file:

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"

}

回答1:

I had the same problem. It is related with new AndroidX libraries and migration on it.

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



回答2:

I agree with Stanislav Mukhametshin that may be it is related with new AndroidX libraries and migration on it. I had cleaned and rebuilt project but that didn't work. So, I tried this way and error goes away.

1) Go to File and close project.

2) Reopen the same project.



回答3:

Please Delete the .idea and .gradle project from the app directory and Invalidate and Cache Restart the Android Studio. This will resolve this issue.



回答4:

Simple Solution Is That:

1: GoTo File And Close The Project.

2: Then GoTo File And Click On "Open Recent" And Open Your Project.

That Trick Works For Me.