Explain Codes LogoExplain Codes Logo

How to set -source 1.7 in Android Studio and Gradle

java
java-8
gradle
android-studio
Anton ShumikhinbyAnton Shumikhin·Feb 21, 2025
TLDR

Park Java 1.7 in your build.gradle right here:

android { ... compileOptions { // wanna know what these lines are doing? Keep reading, it's fun, or at least fun-ish sourceCompatibility JavaVersion.VERSION_1_7 targetCompatibility JavaVersion.VERSION_1_7 } }

Press the Sync button to apply the changes. I mean, who wouldn't want to compile and run with Java 1.7?

Deep dive: Source and target compatibility

The sourceCompatibility option is your friend here, telling your IDE which Java version your source code is cozy with. On the flip side, targetCompatibility dictates the Java byte code output during compilation.

To leverage Java 1.7 features, bless both sourceCompatibility and targetCompatibility with 1.7. However, for a smooth sail, guarantee that your Android Studio and Gradle Plugin versions are current and can groove with Java 1.7 features.

JDK location and environment settings

Your build.gradle is well-dressed, but make sure your JAVA_HOME environment variable is also a part of the fashion show, correctly pointing to the Java 1.7 JDK.

// Let's play Sherlock and detect our Java version
$ java -version

Now, tip-toe to Android Studio's Project Structure (File > Project Structure > SDK Location), and set the JDK location to the Java 1.7 installation directory.

When it comes to using Java 1.7-specific features, the minimum SDK requirement must be on your radar. Pesky limitations can pop up if you don't set the minimum SDK to 19 or higher.

Preparing your codebase

For a truly unparalleled Java 1.7 experience, march to language level 7 in Android Studio's settings. Toss in Java 1.7 language features, and if that's not cutting it, set your sights on a newer JDK.

After any significant changes, don't forget about clean and rebuild to clear out any remnants of the old state and ensure a fresh environment.