If Android developers don’t setup the JDK correctly, you’ll be greeted with nasty errors like this:

error shown in Android Studio when incorrect jdk used
Check your module classpath error
Cannot access 'java.lang.constant.Constable' 
which is a supertype of 'java.lang.Class'. 
Check your module classpath for missing or conflicting dependencies

Your app might compile fine but the IDE will keep surfacing pesky errors. This is because Android developers have a variety of ways to setup the JDK for your development environment. Too many.

On episode #249 of Fragmented we discussed the numerous JDK options and what they’re used for. Google now has an excellent developer doc for this1.

different ways for android developer to choose a jdk
Courtesy: Android Developer docs

If you don’t have time to catch up on all the valuable content above, here’s the takeaways:

1. Use the JDK that comes with Android Studio

JBR2 is battle tested with Android Studio and includes enhancements for running Android Studio well. To use it as your SDK, switch your Gradle JDK to JBR:

pick JBR
how to set jbr as your gradle jdk

Do a File > Invalidate Caches after this…

Famous Android Studio option - Invalidate Caches
mother of all fixes

2. Match yourJAVA_HOME with the Gradle JDK

The Gradle JDK from Step 1 is used to compile your app from Android Studio. If you use the command line to build your app on the other hand, the JDK from your $JAVA_HOME3 is used.

If both of these don’t match, you can have multiple gradle daemons running around. Make sure to match the two.

# ~/.zshenv
export JAVA_HOME=$HOME/Applications/Android\ Studio.app/Contents/jbr/Contents/Home

If you’re not sure where the jbr-jdk is located:

JBR path

This is the way.

  1. no seriously, it’s really good. ↩︎

  2. JetBrains Runtime ↩︎

  3. …if you haven’t customized your environment too much. It’s a little more involved↩︎