Android Studio and Kotlin: Unresolved reference: also
Asked Answered
C

3

12

also compiles and runs from Android Studio:

val greeted = "World".also { println("Hello $it") }

Yet the editor highlights also and it as errors and don't give any help with intellisense. apply still works and when I go to source I can see the definition of both apply and also. How do I get Android Studio to recognize also?

  • Android Studio 2.3.3
  • kotlin_version = '1.1.3-2'
  • Kotlin plugin 1.1.3-release-Studio2.3-2

I've also had this problem with earlier versions.

Clarino answered 17/7, 2017 at 14:2 Comment(7)
I had a similar problem and just a restart of the IDE helped. I guess you already tried this Windows-Have-you-tried-turning-if-off-and-on-again strategy.Daggett
@Daggett I've just verified that the problem remains after restart on both Windows and Mac :)Clarino
In that case I guess it would be the best to file a bug. I know, this doesn't help you, but it will in the future.Daggett
You can try File - Invalidate caches then restartBottle
@Daggett Done: issuetracker.google.com/u/0/issues/63754961Clarino
@JKLy No luck with Invalidate cachesClarino
After adding Kotlin to a project that previously contained only Java classes, invalidating the caches and restarting solved this problem for me.Serenity
J
5

The also function was added in Kotlin 1.1 but you have your Language version set to 1.0. Go to your settings/preferences and change the Kotlin version to 1.1.

enter image description here

Jarrettjarrid answered 27/7, 2017 at 23:40 Comment(4)
This worked after a restart. Do you know why configuring 1.1 in gradle isn't enough?Clarino
That's a good question. I don't know. But my guess is that the Kotlin compiler in AS is not looking at the Gradle setting. So it has to have its own setting. You might want to report that discrepancy to JetBrains.Jarrettjarrid
This was my exact problem. However, I've set the Kotlin compiler version to 1.1 already many times in the past for the project and it keeps resetting after some unknown magics... Any idea if this is an Android Studio bug? A Kotlin plugin bug?Guss
Could it be that someone has checked in an older .iml file for the module into your repository so when you do a clean checkout it get's reset to the previous version? If so, I would recommend that you either replace the .iml file with a newer one, or (better) remove the .iml file from the repository and .ignore them in GIT.Jarrettjarrid
D
7

I tried everything possible and nothing worked. The only solution for me was to delete .AndroidStudio folder from C:/Users/YourName/. Android Studio resets and everything works as it should.

Due answered 22/12, 2019 at 0:54 Comment(2)
Thanks it worked for me. I had problems with all kotlin keywords like apply, until, and even with annotations like JvmOverload, Throws and so on, after i followed your advice everything forks fine.Calamondin
Came across this answer as I had similar problem, and this one solved the issue very well. Should be the accepted answer after all, so others confronting similar problem can immediately implement this workaround.Flightless
J
5

The also function was added in Kotlin 1.1 but you have your Language version set to 1.0. Go to your settings/preferences and change the Kotlin version to 1.1.

enter image description here

Jarrettjarrid answered 27/7, 2017 at 23:40 Comment(4)
This worked after a restart. Do you know why configuring 1.1 in gradle isn't enough?Clarino
That's a good question. I don't know. But my guess is that the Kotlin compiler in AS is not looking at the Gradle setting. So it has to have its own setting. You might want to report that discrepancy to JetBrains.Jarrettjarrid
This was my exact problem. However, I've set the Kotlin compiler version to 1.1 already many times in the past for the project and it keeps resetting after some unknown magics... Any idea if this is an Android Studio bug? A Kotlin plugin bug?Guss
Could it be that someone has checked in an older .iml file for the module into your repository so when you do a clean checkout it get's reset to the previous version? If so, I would recommend that you either replace the .iml file with a newer one, or (better) remove the .iml file from the repository and .ignore them in GIT.Jarrettjarrid
G
0

In my case when i added id 'kotlin-android-extensions' to gradle file (app) and synced, problem is solved.

Gorlovka answered 31/5, 2021 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.