The problem

The following error occurred while building the Android project on the Mac

> Task :compileKotlin FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileKotlin'.
> Kotlin could not find the require JDK tools in the Java installtion '/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home' used by Gradle. Make sure is running on a JDK, not JRE.
Copy the code

The fault is that the JAVA_HOME environment variable is not configured.

Note: By default, the bash shell is used on the Mac, so you need to configure environment variables in ~/.bash_profile, or in ~/.zshrc if you are using the ZSH shell.

Therefore, the above problem can be solved by correctly configuring environment variables. Take the bash shell as an example.

The solution

  • Step 1: Install the JDK and get the JAVA installation path (omitted here)

    Run /usr/libexec/java_home -v on the terminal to obtain the JDK directory.

$ /usr/libexec/java_home -V Matching Java Virtual Machines (2): 1.8.281.09 (x86_64) "Oracle Corporation" - "Java"/Library/Internet plug-ins/JavaAppletPlugin plugin/Contents/Home 1.8.0 comes with _281 (x86_64) "Oracle Corporation" - "Java SE 8"/Library/Java/JavaVirtualMachines jdk1.8.0 _281. JDK/Contents/Home /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/HomeCopy the code
  • Step 2: Configure environment variables
$ vi ~/.bash_profile # android export ANDROID_HOME=/Users/ooyao/Library/Android/sdk export PATH=${PATH}:${ANDROID_HOME}/tools export PATH=${PATH}:${ANDROID_HOME}/platform-tools # java export JAVA_HOME = / Library/Java/JavaVirtualMachines jdk1.8.0 _281. JDK/Contents/Home export CLASSPAHT=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export PATH=$JAVA_HOME/bin:$PATH:Copy the code

After the editing is complete, press Esc to exit the insert mode and enter :wq to exit and save the configuration.

$ source ~/.bash_profile
Copy the code

Run the source command to make it take effect.