94

I'm running Ubuntu 17.10, and I've just updated Android Studio from version 3.0.1 to 3.1. Here's the version information in Help -> About:

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

Unfortunately, while the IDE was able to resolve android-specific references before in my (first ever) Kotlin project, in the new version it appears unable to. Here's a screenshot showing what I mean:

In Android Studio 3.1, it's claiming that core Android functions such as setContentView() don't exist, and keeps prompting me to create an abstract function for it - which I clearly don't want to do. However, if I hit the build button, I get a BUILD SUCCESSFUL in 8s.

Why is my editor doing this, and how I get get Android-related functions and classes to resolve correctly again?

  • Build → Clean and then Build → Rebuild doesn't help.
  • File → Invalidate Caches and Restart doesn't help either
  • Neither does a manual Gradle sync.
  • A different project that's written in Java instead of Kotlin works fine.
  • Link to the project I'm experiencing issues with (it's open-source)
double-beep
  • 5,031
  • 17
  • 33
  • 41
starbeamrainbowlabs
  • 5,692
  • 8
  • 42
  • 73
  • Did you upgrade android gradle plugin to the latest version? It helped resolve something related to unresolved references for my collegue today (I overheard the conversation) – dimsuz Mar 28 '18 at 22:19
  • It looks like you are not picking up symbols from libraries (e.g., `appcompat-v7`). If you have not done so already, try the standard fixes: Build > Clean Project, then Build > Rebuild Project (from the AS main menu). – CommonsWare Mar 28 '18 at 22:40
  • Thanks for the suggestions, @dimsuz and @CommonsWare! Unfortunately, cleaning & rebuilding has no effect, and if I right click on the Gradle plugin in my settings, the "update plugin" option is greyed out. It's currently version "173.4301.25". – starbeamrainbowlabs Mar 29 '18 at 08:41
  • you can try to do a Gradle Sync manually – EpicPandaForce Mar 29 '18 at 09:23
  • @EpicPandaForce Ah, good idea. Unfortunately, while hitting the manual Gradle sync button makes it successfully perform one (and build the project successfully too), it sadly does not resolve the issue. – starbeamrainbowlabs Mar 29 '18 at 09:26
  • You can also try "File -> Invalidate Caches and Restart" – EpicPandaForce Mar 29 '18 at 09:27
  • @EpicPandaForce Yep, tried that too - it's had no effect either :-( – starbeamrainbowlabs Mar 29 '18 at 10:37
  • I'm very new to all this, so it probably won't help, but as I'm trying to work through my own issues, I came across what seems to be similar to this problem. I was able to fix it by adding `apply plugin: 'kotlin-android-extensions'` to the `app/build.gradle` file. Am curious to know if that helps yours too. –  Mar 29 '18 at 18:22
  • @CrazyTrain Ah, thanks for the suggestion! I've checked, and I have that line in already on line #5 :-( – starbeamrainbowlabs Mar 29 '18 at 18:55
  • File -> Invalidate Caches and Restart doesn't help either helped me resolve the issue. – Bashta Oct 26 '18 at 12:48
  • @Bashta Glad you fixed your issue! If `File -> Invalidate Caches and Restart` fixed your issue, then you might have had a slightly different issue tot he one described here :-) – starbeamrainbowlabs Oct 26 '18 at 14:32
  • Today I updated Android Studio to 3.2.1 version and I founded this error, I just execute the invalidate cache and restart option and the errors were fixed. – snti Nov 13 '18 at 18:32
  • @SantiagoLaport Great! Glad you fixed your issue. However, I suspect that the issue you encountered is distinct and different to the one I've described here. – starbeamrainbowlabs Nov 14 '18 at 20:36
  • Hi @starbeamrainbowlabs, the issue seemed to be the same, but you know, in Android Studio all could be possible haha, regards! – snti Nov 15 '18 at 17:15

17 Answers17

110
  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.

Slava Glushenkov
  • 1,378
  • 1
  • 9
  • 10
  • 1
    I'm having the same (or similar) problem and this did not work for me. Ubuntu 16.04 LTS. I've actually gotten similar issues with AS 3.2 alpha resolved, so I don't get why AS 3.1 is giving me so much trouble. – AutonomousApps May 13 '18 at 20:03
  • After deleting .idea folder, I opted "Invalidate Caches / Restart". It makes studio work. – Shanki Bansal Oct 05 '20 at 09:38
  • "seems to me that's most important" I double that! Thanks for saving me. – Cyber Avater Jul 31 '23 at 19:21
84

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

VikingBadger
  • 953
  • 5
  • 7
15

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

Dimitar
  • 659
  • 7
  • 6
  • 1
    I use `com.android.tools.build:gradle:3.2.1` and still have this problem. @Slava Glushenkov 's solution did work for me. – WindRider Nov 07 '18 at 17:56
  • 1
    I'm on AS 3.2.1 .... Invalidate and Restart option worked for me. Thanks @Dimitar – ArdenDev Jan 08 '19 at 07:12
  • this is sort of was what worked for me. except I went to the latest stable version of gradle which was newer than the one i was using. None of the other solutions worked for me. – Victor Dec 10 '20 at 21:55
10

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


SmokeyTBear
  • 129
  • 1
  • 6
  • It wasn't specifically the `R` import for me - but I wound up deleting _all_ of the imports & just re-adding them. For whatever insane reason that worked. – Jordan May 13 '20 at 18:08
7

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

Adriatik Gashi
  • 332
  • 4
  • 10
6

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
dbog
  • 106
  • 1
  • 4
5

Goto File->Invalidate Cache/Restart works for me

shafeeq
  • 1,499
  • 1
  • 14
  • 30
3

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.

Diego Palomar
  • 6,958
  • 2
  • 31
  • 42
  • Yeah I tried everything but my problem is solved only by disable kotlin plugin-> restart , Enable plugin and -> invalid catches and restart that's solved my prblem – Bhavin Patel Jan 09 '20 at 12:03
  • 2
    For me disabling kotling prevents AS from even starting, I have to delete the disabledplugins.txt from the AS main folder. But also none of the other solutions worked so far, what a huge f*** up, when you have to work on your code but a simple error hangs everything. – Luis Mar 22 '21 at 13:30
  • 1
    Bad Idea. After disabling it gave the error in Android Studio startup - Missing essential plugin. Please reinstall the Android Studio from scratch. – Mihir Trivedi Jan 19 '22 at 16:04
  • In case Android Studio not starting after disabling plugins, apply this: https://stackoverflow.com/a/62153817/538408 – Murat Jul 06 '22 at 14:43
2

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

Anna
  • 532
  • 4
  • 14
1

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..!

Pranav P
  • 1,755
  • 19
  • 39
1

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

Pai-Hsiang Huang
  • 342
  • 3
  • 12
1

I removed all cached filed from

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

HOME/.android/ -> cache/

from project folder -> .gradle/

Davide
  • 3,407
  • 1
  • 20
  • 22
0

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).
Richard Miller
  • 576
  • 1
  • 7
  • 18
  • Disabled the kotlin plugin - then you can't start android studio ever again (re-installation does not help). Until you delete the disabled_plugins.txt THEN however things started working again. – Olof_t Apr 26 '22 at 09:28
0

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

0

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.

Rajkiran
  • 15,845
  • 24
  • 74
  • 114
0

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.

Luis
  • 123
  • 1
  • 3
  • 18
0

I solved it upgrading the gradle version to 3.6.4 in project module

classpath 'com.android.tools.build:gradle:3.6.4'

and upgrade version in gradle-wrapper.properties

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

Images reference Step 1 Step 2