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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Love
  • 1,709
  • 2
  • 22
  • 30
  • 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. – guenhter Jul 17 '17 at 14:24
  • @guenhter I've just verified that the problem remains after restart on both Windows and Mac :) – Love Jul 17 '17 at 14:34
  • 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. – guenhter Jul 17 '17 at 14:49
  • 3
    You can try `File - Invalidate caches` then restart – JK Ly Jul 17 '17 at 15:06
  • @guenhter Done: https://issuetracker.google.com/u/0/issues/63754961 – Love Jul 17 '17 at 15:06
  • @JKLy No luck with `Invalidate caches` – Love Jul 17 '17 at 15:16
  • After adding Kotlin to a project that previously contained only Java classes, invalidating the caches and restarting solved this problem for me. – mcfisty Apr 06 '20 at 05:09

3 Answers3

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.

Ardi
  • 285
  • 3
  • 8
  • 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. – easy_breezy Jan 29 '20 at 07:52
  • 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. – FEBRYAN ASA PERDANA May 27 '20 at 07:56
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

  • 1
    This worked after a restart. Do you know why configuring 1.1 in gradle isn't enough? – Love Jul 28 '17 at 12:35
  • 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. – Troy Heninger Jul 28 '17 at 14:54
  • 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? – Erik Aug 01 '17 at 06:41
  • 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. – Troy Heninger Aug 14 '17 at 17:45
0

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

HTugsadK
  • 81
  • 5