SDK location not found. Define a valid SDK location with an ANDROID_HOME environment variable or by setting the sdk.dir path

Trying Android development with command line tools only (I am not installing Android Studio).
The following error occurs when I run ./gradlew build --warning-mode all:

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 '/Users/folder1/android-projects/genisis/local.properties'. 

From whatever I have seen over here, I understood that I have to specify the path of android sdk to a variable named ANDROID_HOME or to sdk.dir in local.properties.
Now the thing is, both have been done and I am still struck in this eternal suffering caused by gradlew.

Is it that I have not installed Sdk properly?
How did I install Sdk and its stuff?
1.went to the :Android studio download website
2. I Scrolled down to the bottom to the “Command line tools only” section. Since I am using Mac-os (intel x86 chips) now, I have downloaded the Mac’s zip file.
3. extracted the zip file, went into cmdline-tools/latest/bin, launched sdkmanager and downloaded the following: platform-tools, build-tools, platforms;android-35

I followed this tutorial to do the above.

Now my directory looks like this
Android:

/build-tools
/cmdline-tools
/licences
/platform-tools
/platforms

platforms folder contains the folder named android-35 which I belive to be the sdk.

So my environment variables look like this:

export ANDROID_HOME=Users/folder1/Android
export PATH=$PATH:$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools
export ANDROID_SDK_ROOT=Users/folder1/Android

all are defined in ~/.bash_profile

and my sdk.dir in local.properties have this value : Users/folder1/Android/

What abomination have happened and how can I recover from this? Is the only solution is to go back to android studio

What did I try and what was I expecting? uhhh… I don’t know… I don’t know what to do or what to try

what did I expect? all I expected was an apk to be outputted

What makes my question different from other questions with the same error text?
Its because I did all the ones that were mentioned(as far as I have read. If you find any post that has a new solution please let me know) and I am still struck with the same error

as you can see the contents of the ~/.bash_profile that I have provided above, can see that I have a variable named ANDROID_HOME.
And Also I have mentioned that I updated the sdk path in sdk.dir in local.properties.

It seems like you’re on the right track but still experiencing issues with the Android SDK path configuration. Let’s go through the steps again and troubleshoot this.

Common Causes of the Error:

  1. Incorrect SDK Path: If the SDK path is not correctly set, Gradle won’t be able to find the Android SDK.
  2. Incorrect Environment Variable Configuration: If environment variables are not set properly, Gradle will fail to locate the SDK.
  3. Missing or Incorrect Permissions: Sometimes, the SDK folder or its contents might not have the right permissions.

Possible Solutions:

1. Ensure Correct SDK Path in local.properties

The path in the local.properties file should be absolute. Try using the full absolute path rather than a relative one. For example:

sdk.dir=/Users/folder1/Android

Ensure that there’s no typo and the folder exists.

2. Fix ANDROID_HOME and ANDROID_SDK_ROOT in Environment Variables

You might have set incorrect paths in the environment variables. Double-check and make sure they are set as absolute paths.

Your .bash_profile should look something like this:

export ANDROID_HOME=/Users/folder1/Android
export ANDROID_SDK_ROOT=/Users/folder1/Android
export PATH=$PATH:$ANDROID_HOME/platform-tools:$ANDROID_HOME/cmdline-tools/latest/bin

This assumes your SDK folder is /Users/folder1/Android. Make sure the paths exist by running:

ls /Users/folder1/Android/platform-tools

and

ls /Users/folder1/Android/cmdline-tools/latest/bin

If the directories exist, then your paths should be correct.

After editing .bash_profile, don’t forget to source it:

source ~/.bash_profile

3. Permissions

Make sure that the SDK directory has the proper permissions for the current user. You can change the ownership or permissions of the Android SDK folder like this:

sudo chown -R $USER /Users/folder1/Android

This command will change the ownership of the entire Android SDK folder to your current user.

4. Ensure Required SDK Components are Installed

Make sure that all the required SDK components are installed. You mentioned you’ve installed platform-tools, build-tools, and platforms;android-35. To check if they are installed properly, run the following command:

sdkmanager --list

Make sure build-tools, platform-tools, and the correct android-35 platform are installed. If they are missing, you can install them like this:

sdkmanager "build-tools;35.0.0" "platform-tools" "platforms;android-35"

5. Check Gradle Version

Make sure the Gradle version you’re using is compatible with the SDK and build tools you’ve installed. You can update the Gradle wrapper in your project:

./gradlew wrapper --gradle-version <latest_version>

You can check the latest stable Gradle version at the Gradle releases page.

6. Check JAVA_HOME

Ensure that your JAVA_HOME is set correctly, and you’re using a compatible JDK version. You can check by running:

echo $JAVA_HOME

If it’s not set, you can set it in your .bash_profile like this:

export JAVA_HOME=$(/usr/libexec/java_home)

Then reload the profile:

source ~/.bash_profile

Ensure you’re using JDK 11 or JDK 17, as Android Gradle Plugin requires one of these versions for Android development.

7. Run gradlew build Again

Once you’ve done the above steps, try running your Gradle build command again:

./gradlew build --warning-mode all

This should allow Gradle to locate the SDK and start building your project.

Debugging Checklist:

  1. Ensure absolute paths in local.properties and environment variables.
  2. Ensure the necessary SDK components are installed (use sdkmanager --list to confirm).
  3. Ensure Gradle version compatibility with your project.
  4. Ensure JAVA_HOME is set correctly.

Final Considerations:

If after trying all of the above you’re still stuck, you can try a clean Gradle build by deleting the .gradle folder in your project root and re-running the build:

rm -rf .gradle
./gradlew clean
./gradlew build --warning-mode all

This will force Gradle to re-download dependencies and might help resolve any leftover issues.

Let me know if you need more clarification or if the issue persists after these steps!