'Build failed: SDK location not found (:app:compileDebugJavaWithJavac)

Since a recent upgrade of Android Studio, I cannot build my project anymore. The surface error is:

e: C:\Users\<user>\dev\<project>\app\build\generated\source\navigation-args\release\com\<org>\<project>\ui\home\HomeFragmentDirections.kt: (3, 8): Unresolved reference: R

If I dig further and build from the terminal with gradlew (gradlew assembleDebug --debug):

FAILURE: Build failed with an exception.

What went wrong:
Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
SDK location not found. Define a valid SDK location with an ANDROID_HOME environment variable or by setting the sdk.dir path in your project's local properties file at '/mnt/c/Users/<user>/dev/<project>/local.properties'.

My local.properties file already contains this line:

sdk.dir=C\:\\Users\\<user>\\AppData\\Local\\Android\\Sdk

and that is where the SDK is installed.

In my build.gradle:

...
android {
  compileSdkVersion 32
  buildToolsVersion '32.1.0-rc1'
...
  defaultConfig {
      ...
      minSdkVersion 23
      targetSdkVersion 32
      ...
  }

settings.gradle:

include ':app'
rootProject.name = "<projectname>"

What I tried:

  • As per the error, I set the ANDROID_HOME environment variable to the SDK location C:\Users\<user>\AppData\Local\Android\Sdk, and restarting my machine to make sure the var is seen.
  • Clean project, rebuild project
  • File -> Invalidate caches
  • Install different versions of the SDK and SDK Tools
  • Reinstalling the SDK completely
  • Reinstalling Android Studio completely

I found innumerable posts with a problem similar to mine, and all seem to be solved by upgrading the SDK, changing compileSdkVersion/buildToolsVersion etc. in build.gradle, changing the SDK location in local.properties or the ANDROID_HOME environment variable. Or even reverting to an older SDK version. None of that works for me.

I'm out of options at this point. Any help/idea would be appreciated.

Current software:

Android Studio:
Android Studio Chipmunk | 2021.2.1
Build #AI-212.5712.43.2112.8512546, built on April 28, 2022
Runtime version: 11.0.12+7-b1504.28-7817840 amd64
VM: OpenJDK 64-Bit Server VM by Oracle Corporation
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 12
Registry: external.system.auto.import.disabled=true
Non-Bundled Plugins: org.jetbrains.kotlin (212-1.6.21-release-334-AS5457.46)

Android SDK: Android API 32 (Sv2)
Build-Tools installed: 32.1.0-rc1, 32.0.0, 30.0.3
ext.kotlin_version = "1.6.21"
Operating system: Windows 10


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source