57

When I create an empty project with Gradle Kotlin DSL, even without any modifications, it would prompt Cannot access script base class 'org.gradle.kotlin.dsl.KotlinBuildScript'. Check your module classpath for missing or conflicting dependencies The project can run, but the syntax highlighting and autocompletion for build.gradle.kts don't work.

What I've tried

System

IntelliJ IDEA 2020.3.1 (Ultimate Edition)
Build #IU-203.6682.168, built on December 29, 2020
Licensed to Hykilpikonna Gui
Subscription is active until November 28, 2021.
For educational use only.
Runtime version: 11.0.9.1+11-b1145.63 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 10.15.7
GC: ParNew, ConcurrentMarkSweep
Memory: 1998M
Cores: 6
Registry: debugger.watches.in.variables=false, js.debugger.webconsole=false
Non-Bundled Plugins: CMD Support, Rider UI Theme Pack, Statistic, com.alayouni.ansiHighlight, com.intellij.ideolog, net.vektah.codeglance, String Manipulation, com.alibaba.p3c.smartfox, wu.seal.tool.jsontokotlin, com.demonwav.minecraft-dev, lermitage.intellij.extra.icons, com.github.masahirosuzuka.PhoneGapIntelliJPlugin, Dart, io.flutter, deno, org.jetbrains.kotlin, com.cybrosis.catdea, siosio.kodkod, izhangzhihao.rainbow.brackets, org.jetbrains.plugins.ruby, Pythonid, org.jetbrains.plugins.vue, tv.twelvetone.intellij.plugins.intellivue, ru.meanmail.plugin.requirements
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Hykilpikonna
  • 1,749
  • 2
  • 15
  • 32

14 Answers14

51

Answer credit to @AlexeyBelkov - Answered here: https://youtrack.jetbrains.com/issue/KTIJ-893

The syntax highlighting feature worked after:

  1. Delete ~/.gradle/caches
  2. Delete ~/Library/Application\ Support/Library/JetBrains/IntelliJIdea2020.3 (or ~/.cache/JetBrains/IntelliJIdea2020.3 for ubuntu/linux users)
  3. Delete <project dir>/.gradle
  4. Delete <project dir>/.idea
  5. Start IDEA and reimport the project.
PHaroZ
  • 23
  • 1
  • 4
Hykilpikonna
  • 1,749
  • 2
  • 15
  • 32
  • 8
    It seems deleting C:\Users\\AppData\Local\JetBrains\IntelliJIdea2021.1\caches fixes the issue. – Michael Böckling Apr 13 '21 at 15:37
  • 4
    After doing this something improved, maybe, but I still had all KTS files "red". For Android Studio the solution was: I went to: File -> Project Structure -> SDK Location -> "JDK Location" textbox: replace default "Embedded JDK" with a path to the separately downloaded Open JDK 11 resolved the issue. More info: https://youtrack.jetbrains.com/issue/KT-41141 – yvolk May 27 '21 at 04:35
  • 1
    All I had to do was update the JDK as described by @yvolk above. The other suggestions (though generally helpful in these kinds of situations) wasn't the issue here. – arberg Jun 02 '21 at 11:10
  • 2
    For AndroidStudioPreview2021.1 deleting ~/Library/Caches/Google/AndroidStudioPreview2021.1/caches worked – a.r. Dec 03 '21 at 06:00
  • I like your (currently deleted) [suggestion for commit count](https://stackoverflow.com/a/73095840/6309). Don't forget to add, [since Git 2.34](https://stackoverflow.com/a/48028196/6309), the option `--unsorted-input`, for an even faster result. – VonC Jul 24 '22 at 04:53
  • @VonC I'm sorry, I deleted that answer because it didn't actually work... Even though it did run faster, it returned 1 instead of the actual number of commits – Hykilpikonna Jul 24 '22 at 18:20
  • @Hykilpikonna OK. What version of Git were you using? – VonC Jul 24 '22 at 18:23
  • @VonC I'm using the latest stable release 2.37.1. Also, it doesn't seem to recognize the `--unsorted-input` option. After some digging, it seems that `--unsorted-input` is added in `2.34.0-rc1` but later removed in the stable release of `2.34.0` (See [man page](https://github.com/git/git/blob/v2.34.0-rc1/Documentation/rev-list-options.txt)) – Hykilpikonna Jul 24 '22 at 18:33
  • True: [commit a7df4f5](https://github.com/git/git/commit/a7df4f52affe49f6b06246b9398e99e1937f9efc). Too bad! (`git log -S "unsorted-input" -- **.c`) – VonC Jul 24 '22 at 18:40
  • correct answer is here: https://stackoverflow.com/a/72912179/2297550 – user2297550 Sep 09 '22 at 07:53
16
  1. Close the Android Studio. (Mine was version 2021.2).
  2. Delete jdk.table.xml from Android studio's config folder .
  3. Open Android Studio and configure JDK in Gradle Settings (Project Structure -> SDK Location -> Gradle Settings).

Found the solution from JetBrain's issue tracker.

jaga
  • 753
  • 11
  • 18
  • 1
    This should be marked as answer, deleting cache solves the problem only temporary. – Calin Aug 22 '22 at 10:43
  • yes, this is correct -- the config folder needs cleaning and other answers don't mention that – user2297550 Sep 09 '22 at 07:52
  • after everything with deleting caches etc. failed, simply renaming `jdk.table.xml` helped (note: I didn't delete /.idea for some reason) – Harald Mar 23 '23 at 18:07
  • you save my life – Junerver May 10 '23 at 00:16
  • 2
    Thanks! I just add some additional info about path: {config_path}/options/jdk.table.xml – STAYER Jun 22 '23 at 10:57
  • Also: item #3 setting is no longer there (or am I missing something?) but for me choosing JDK for Gradle in General IDE's settings (settings -> Build & Execution -> Gradle) to something other and then back to what you need (e.g. to jdk 1.8 and then to jdk 11) did the trick – Den Drobiazko Jun 26 '23 at 16:26
15

In my case (the OS is Windows 10) it was enough to:

  1. Delete C:\Users\<you>\AppData\Local\JetBrains\IntelliJIdea2021.1\caches
  2. Restart IntelliJ IDEA.
  3. Click "Load Script Configurations" when it appears after re-indexing.

Thanks @MichaelBöckling for a hint!

Dmitry Rakovets
  • 557
  • 1
  • 6
  • 15
pkalinow
  • 1,619
  • 1
  • 17
  • 43
8

Mac OS:

  1. File -> Project Structure -> SDK Location -> click on Gradle Settings
  2. Modify the Gradle JDK
  3. File -> Invalidate Caches...

enter image description here

enter image description here

Frank Nguyen
  • 6,493
  • 3
  • 38
  • 37
4

Worked for me after I'd deleted:

Delete <project dir>/.gradle
Delete <project dir>/.idea
Sergiy
  • 1,854
  • 4
  • 22
  • 34
3

Guided Cache Recovery (IntelliJ IDEA 2021.3+)

IntelliJ IDEA versions from 2021.3 and on (as of writing, available from JetBrain's Early Access Program, or EAP) has a featured called "Guided Cache Recovery" (File, Run Guided Cache Recovery...). While I can't find any documentation yet about this feature, it appears to give the user the ability to apply diagnostic/recovery steps, one at a time, to fixing any cache issues.

For me, the problem reported by the OP was resolved at step 2: "Rescan Project Indexes".

The really cool thing about this is that, not only did it fix my problem, but IntelliJ then created an issue report with details about the problem found, and gave me the option of submitting this report to JetBrains via the crash reporter. This tells me that JetBrains appears to be serious about fixing these issues for users, and about resolving their root causes.

Permissions Issues in .idea, .gradle, or gradle

Another possible problem is permissions issues in one of the .idea, .gradle, or gradle directories. For example, running a rootfull docker container, mounting the project as a volume (for example, to run a tool like Qodana) could cause some files to become owned by root unless the appropriate Docker options are used. If this issue is noticed, reset permissions with chmod and chown and then sync again in IDEA.

Raman
  • 17,606
  • 5
  • 95
  • 112
2

I set the project SDK to Kotlin under Project Settings > Modules. I also set the SDK in Plaftorm Settings > SDKs although I don't know if that's required. And just in case I restarted IntelliJ with File > Invalidate Caches / Restart ...

enter image description here

Jonathan Morales Vélez
  • 3,030
  • 2
  • 30
  • 43
0

For IntelliJ 2021.1, I had to delete the caches folder at:
~/Libary/Caches/JetBrains/IntelliJIdea2021.1/caches

bedalton
  • 97
  • 1
  • 3
0

For me, solution was to install a separate JDK version (I installed Temurin JDK 17). I then simply set it to JAVA_HOME and used it in the AS and the errors disappeared.

Primož Ivančič
  • 1,984
  • 1
  • 17
  • 29
0

In my case I had to set the Java version in IntelliJ -> Settings -> Build -> Gradle to 1.8 to match the projects Java version

Adrian Witaszak
  • 121
  • 1
  • 5
0

You need to delete de cache at

~/.gradle/caches

But an easier way is to do it from IDE, under file menu you will find "Invalidate Caches" option

Hope it works for you

enter image description here

JPilson
  • 1,075
  • 11
  • 10
0

My solution is as shown below

I tried but The method below didn't work. use in android studio android ver AndroidStudio2022.1

  1. remove ~/Library/Caches/Google/AndroidStudio2022.1/caches
  2. remove /.gradle/caches
0

In the latest IntelliJ/Android Studio versions they've added a new action called "Repair IDE".
By running that, it incrementally does more steps that could potentially fix such issues for you.
I was experiencing this issue for example, and after clicking the automatic actions 3 times, where it reopened my project, this was fixed. I would really recommend you try out that action next time you feel like your IDE is in an odd state.

Stylianos Gakis
  • 862
  • 8
  • 19
-3

In my case, I had to install the previous IDE version (2021.1.3).

Kelvin Schoofs
  • 8,323
  • 1
  • 12
  • 31