Encountering an error can be very stressful. Having made your coffee, headphones on, and ready to make the best application the world has seen, then boom, getting an error from the first step. Today we tackle how to fix the could not find gradle error.

Challenge

When you try to build your Ionic application using Cordova command:

$ ionic cordova run android

You may encounter an error with the message:

Checking Java JDK and Android SDK versions
ANDROID_SDK_ROOT=undefined (recommended setting)
ANDROID_HOME=C:Users\codeswag.co.uk\AppData\Local\Android\Sdk (DEPRECIATED)
could not find an installed version of Gradle either in Android Studio,
or on your system to the install gradle wrapper. Please include gradle
in your path, or install Android Studio

[ERROR] An error occurred while running subprocess cordova

Solution

The first thing we need to do is head over to the Gradle website. When we land on the Gradle website, we want to:

  1. Click the Install Gradle button.

  2. Scroll down to the install manually section and click on the Download link.

  3. Scroll down to the latest version, and here we will download binary-only.

gradle download

Our download begins.

Installation

  1. When our download is complete, we want to paste the downloaded zip file onto our C: drive. Click Continue to confirm pasting into C: drive.

  2. Next, we want to extract the file. Right-click on the zip file and select Extract All.

  3. It shows us the destination of our extraction. click Extract.

  4. Next, we now want to delete our zip file in the C Drive and then rename our extracted file to simply gradle.

  5. Click inside the gradle folder, and you can see we have the specific version of the Gradle we downloaded, gradle-7.5.1.

  6. The next step is we want to go to this PC, right-click on it, and select Properties.

    properties

  7. We want to go to Advanced system settings. Here it is on the right hand side under Related settings.

    environment variables option

  8. Next, click where it says Environment Variables.

    environment variables

  9. We want to go to the System Variables section and select Path, and with the Path highlighted, we want to click Edit.

    system variables

  10. Next, we want to add the path to the binary folder highlighted below into our Environment Variables.

    C: \ gradle \ gradle-7.5.1 \ bin
    

    To get the path we need, in the gradle-7.5.1 folder, open the bin folder, head to the address bar and copy the address to the folder.

    You need to be inside the bin folder.

    gradle bin address

  11. Now back here on our system path, we will select New.

    new environment variables

  12. Paste the path to the gradle bin folder and select Ok.

    exit environment variables

  13. We are done. Time to exit, Select Ok.

    exit environment variables

  14. Select Ok once again.

    exit environment variables

Our gradle is now in the path variable. We have to restart our machine to apply the changes.

After restarting your machine, head back to the command line and retype to see the gradle installed:

$ gradle -v

Gradle is now working.

gradle build

We will clear the screen and then try to run our ionic Cordova application using the Android Emulator. We do that with the following command:

$ ionic cordova run android

Here we see the build is succesful and we are no longer getting the gradle missing error message.

successful-build

The emulator has booted up, and there we have our app running.

emulator preview

There is not much in terms of the application but this is a successful demonstration of how to deal with the could not find gradle error in ionic.

Conclusion

After successfully installing gradle and you try to build the application, you may run into other errors. You must remove and reinstall your Android Cordova platform to resolve those errors.

To do that, you use the command:

$ ionic cordova platform rm android

And this will remove the Android platform, and when you want to add it back, you will use the command:

$ ionic cordova platform add android

Other blog posts: