Android Studio未解决的参考。项目编译

时间:2018-10-15 07:05:56

标签: android android-studio kotlin unresolved-external

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

enter image description here

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

例如,在我的一个使用Room的课程中,

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

android.arch.persistence.room.Roomandroid.arch.persistence.room.RoomDatabase不会。

enter image description here

注意:该项目可以在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"

}

4 个答案:

答案 0 :(得分:21)

我有同样的问题。它与新的AndroidX库及其上的迁移有关。

  

尝试文件->打开->然后单击build.gradle重新打开项目。

答案 1 :(得分:2)

请从应用目录中删除.idea和.gradle项目,然后使Invalidate和Cache重新启动Android Studio。 这样可以解决此问题。

答案 2 :(得分:0)

我同意Stanislav Mukhametshin的观点,这可能与新的AndroidX库及其上的迁移有关。我已经清理并重建了项目,但是没有用。因此,我尝试了这种方法,错误消失了。

1)转到File关闭项目

2)重新打开同一项目。

答案 3 :(得分:0)

简单的解决方案是:

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

2:然后转到文件并单击“最近打开”并打开您的项目。

那招对我有用。