If you are building Android via the command line (which includes our recommended pre-push hooks), you will need to configure Java. If not, you can just use Android Studio and do not need to read further.
Notes:
- this guide is written using macOS and will need to be updated to additionally support Linux and Windows.
- if you find a better way to do configure Java, let us know!
At the time of writing, Android development seems to work best when using Java 8. By default brew
and other package managers will install more recent versions that may throw errors during development.
Caveat: Robolectric warns that Java 9 is needed for certain functionality. This seems inconsistent with the rest of the Android ecosystem, however.
Do the following:
- Download and install Android Studio
- Add the following line to your
~/.zshrc
or equivalent shell startup file:export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home"
- Update the path if you installed Android Studio to a non-standard location
That's it! To verify correctness, open a new shell, navigate to a directory with Android source code, and type ./gradlew tasks
.
This works because macOS comes with a /usr/bin/java
stub which will defer the location of the JDK to the value in the JAVA_HOME
environment variable.
You can install Java from Homebrew using the command below.
brew install java11
TODO
To see what version of Java you have installed, run:
java -version
If you have Java 8 configured, you'll see output like:
openjdk version "1.8.0_242-release"
OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
OpenJDK 64-Bit Server VM (build 25.242-b3-6222593, mixed mode)
If you encounter this error, particularly when running our recommended pre-push hooks:
Could not initialize class org.codehaus.groovy.runtime.InvokerHelper
You may be on Java 14: consider installing Java 8 (see above). If preferred, other users have found installing any version of Java, 13 or lower, appears to resolve the issue:
Steps to downgrade Java Version on Mac with Brew:
- Install Homebrew (https://brew.sh/)
- run
brew update
- To uninstall your current java version, run
sudo rm -fr /Library/Java/JavaVirtualMachines/<jdk-version>
- run
brew tap homebrew/cask-versions
- run
brew search java
- If you see java11, then run
brew install java11
- Verify java-version by running
java -version