Could not find tools.jar. Ionic Android

If you are getting the following error while running npx cap run android

 > Could not find tools.jar. Please check that /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
        contains a valid JDK installation.

then you need to follow the following steps to fix that issue:

Install JDK. The steps ccould be different for your machine. You can follow https://www.oracle.com/java/technologies/downloads/#java8 for other versions.

RUn the echo $JAVA_HOME to get the JDK path

echo $JAVA_HOME 

Output will be like this:

/Library/Java/JavaVirtualMachines/jdk1.8.0_331.jdk

Open gradle.properties file present in android folder in your Ionic project.

Add the following line to the last of that file:

org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_331.jdk/Contents/Home

The jdk1.8.0_331.jd folder could be different depending upon your JDK location.

Running npx cap run android again will work and will launch your Andoird simulator.