Android Studio 3.1: 编辑器中错误的未解决引用

我正在运行 Ubuntu 17.10,我刚刚将 安卓工作室从3.0.1版本更新到3.1版本。以下是帮助中的版本信息-> 关于:

Android Studio 3.1
Build #AI-173.4670197, built on March 22, 2018
JRE: 1.8.0_152-release-1024-b01 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.13.0-37-generic

不幸的是,虽然 IDE 以前在我(有史以来第一次)的 Kotlin 项目中能够解析 android 特定的引用,但在新版本中似乎无法解析。下面的截图显示了我的意思:

安卓工作室3.1中,它声称像 setContentView()这样的核心 Android 功能不存在,并不断提示我为它创建一个抽象函数——显然我不想这么做。但是,如果我按下构建按钮,我会得到一个 BUILD SUCCESSFUL in 8s

为什么我的编辑器要这样做,以及我如何让 Android 相关的函数和类再次正确解析?

  • 构建→清理然后构建→重构没有帮助。
  • 文件→无效缓存和重新启动也没有帮助
  • 手动同步也不行。
  • 用 Java 而不是 Kotlin 编写的另一个项目运行良好。
  • 链接到我遇到问题的项目 (它是开源的)
61656 次浏览
  1. Exit Studio.
  2. Delete .idea/ (seems to me that's most important), build/, app/build directories.
  3. Start Studio
  4. Set Settings -> Build -> Gradle settings back (last stable Gradle local distribution in my case).
  5. Clean&rebuild project.

Helps me.

Delete {projectDir}/.idea/libraries, then go to File -> Sync Project with Gradle Files.

I had the same problem on Android Studio 3.2.1.

The solution was to use stable 'com.android.tools.build:gradle:3.2.1', not alpha...

In the project build.gradle change the version like the code below (or if there is a newer stable version)

dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}

If this doesn't solve your problem than in File >> choose Invalidate caches/Restart... and on the next dialog choose Invalidate and Restart

1 step

2 step

For me this step works :

I) Delete .idea folder from the android studio

II) Go to File > Invalidated caches/ Restarts

III) It will ask you to confirm and click on invalidate and restart.

IV) Go to Build > Clean project

V) Go to Build > Rebuild project

Try with this.

Happy Coding..!

I tried all the solutions proposed here but they did not work. What worked for me was to disable and then re-enable the Kotlin plugin.

I had the same problem. None of the "fixes" listed above helped.

Just run in a terminal:

./gradlew --stop
./gradlew --rerun-tasks assemble{flavour}

For example:

./gradlew --stop
./gradlew --rerun-tasks assembleDevDebug

If it happens to you after refactoring, and you've tried all the mentioned below (I will elaborate the list in a moment), try going to the class which has unresolved references and delete all its imports. Then do the imports again, making sure you import the correct classes.

I tried the following which didn't worked for me:

  • Invalidate Cache and restart Android Studio
  • Delete .build, app/build, and .idea
  • Sync Gradle files
  • Enable and Disable the Kotlin plugin.
  • Clean/Rebuild project
  • Restart my pc (MacOS).

It helped when I deleted Android Studio and installed again. The advices above didn't help.

I had a very similar issue:

Layout resources (ie: activity_view.xml) that worked fine would all of a sudden not be detected or show up in the autocomplete lists, etc.

I tried all these "delete .idea folder/invalidate caches/restart Android studio" solutions and nothing worked..

Solution that worked:

Turned out that an import statement of the entire R package (import android.R) had at some point been added to the import statements and was somehow creating the issue (for specific layout resources only for some bizarre reason)

Removing import android.R instantly resolved the issue, and putting it back recreated it instantly as well.

Hope that helps any that come across this post for the same/similar reason


What helped me was Using AS 3.6.3 I go to build.gradle (module: app) change the buildToolsVersion from "29.0.3" to "28.0.3" sync my project and it worked

I think there was something wrong with the 29.0.3 version.

I uninstall the 29.0.3 version and install it again and use it. 29.0.3 version is working perfectly for me

Make sure you have not import android.R in any of your activities/fragments. This can happened when Android Studio tries to convert code from Java to Kotlin.

Delete this line and try to run your project.

enter image description here

Goto File->Invalidate Cache/Restart works for me

Read till the end :)

Surprisingly after so many answers, none of them worked for me. I figured out the issue was with the android tools version and gradle distribution.

I upgraded classpath from

'com.android.tools.build:gradle:3.6.1'

to 'com.android.tools.build:gradle:4.0.1'

and also upgraded distributionUrl from

distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip

to distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip

So, if clean/build, invalidating cache, deleting .idea doesn't work for you. Please try upgrading (or downgrading if your recent change caused this issue) these versions.

I tried every other solution given here or anywhere else, what helped me was changing the way my plugins were in the gradle file.

I changed it to this format:

plugins{
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'com.google.gms.google-services'
}

After this, invalidated caches and restarted, rebuilt the project and all was fine. Amazing how this simple nothing results in a colossal error.

What worked for me was to change the kotlin-gradle-plugin version to the same version of Kotlin plugin used in the IDE.

That is, in your top level build.gradle, find kotlin-gradle-plugin and change the version to the same version of IDE Kotlin plugin.

The IDE Kotlin plugin version can be found in File -> Settings -> Plugins -> Kotlin

enter image description here

I removed all cached filed from

HOME/.gradle/ -> cache/ and .tmp/

HOME/.android/ -> cache/

from project folder -> .gradle/