Android Studio
Gradle
source compatibility
Java 1.7
Android development

How to set -source 1.7 in Android Studio and Gradle

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Setting the Java source compatibility in Android Studio and Gradle projects is crucial when working with different versions of Java. Android Studio, which is based on IntelliJ IDEA, heavily uses Gradle as its build system to customize and manage project builds. In this article, we will explore how to set the Java source compatibility to version 1.7 in Android Studio and Gradle.

Why Source Compatibility Matters

Java source compatibility ensures that the Java compiler can compile the source code using a specific version of the Java SDK. This is particularly important in Android development, where Android apps need compatibility with specific Java versions due to Android Runtime (ART) and Android platform constraints.

Setting Java Source Compatibility in Android Studio

  1. Open Your Project: Open your Android project in Android Studio.
  2. Modify build.gradle File:
    In Android Studio, Gradle is used for build automation. You have to set the source compatibility in the build.gradle file located at the app module level.
gradle
1   android {
2       ...
3   }
4
5   // Add this block outside the android closure
6   compileOptions {
7       sourceCompatibility JavaVersion.VERSION_1_7
8       targetCompatibility JavaVersion.VERSION_1_7
9   }

The compileOptions block is where you specify the Java versions. The sourceCompatibility option specifies the minimum version of Java your code will compatible with, whereas targetCompatibility determines the version of the Java bytecode generated.

  1. Sync Project with Gradle Files: After modifying the build.gradle file, sync your project with Gradle files. You can do this by clicking on File > Sync Project with Gradle Files.

Setting Java Source Compatibility in Gradle

If you are using Gradle without Android Studio, you may manage the Java source compatibility slightly differently. Below is an example:

gradle
1apply plugin: 'java'
2
3sourceCompatibility = 1.7
4targetCompatibility = 1.7

In this snippet, the java plugin is applied, and then the source and target compatibility versions are set directly. This adds similar functionality to what was specified in Android Studio but is specifically streamlined for Java projects without the Android plugin.

Compatibility and Considerations

Key Points

FeatureDescription
sourceCompatibilitySets the minimum Java version for source code compatibility.
targetCompatibilityDetermines the Java version bytecode output.
Backward CompatibilityEnsures your code can run on older Java runtimes.
Android Plugin VersionDifferent versions of com.android.tools.build:gradle might affect certain settings.

Important Considerations

  • Java 7 Limitations: Java 7 is quite dated, and using it might limit you from using newer Java language features that improve code readability, performance, and robustness.
  • Android API Level: Always ensure that your app's minSdkVersion aligns with the Java version's compatibility.
  • Project Dependencies: Make sure that any third-party libraries used in your project are also compatible with Java 1.7.

Conclusion

Changing the source compatibility in Android Studio to Java 1.7 ensures that your Android applications are built with the restrictions and feature set provided by Java 7, improving compatibility on older Android devices. While setting sourceCompatibility and targetCompatibility might seem trivial, it is an integral process for achieving the Java version alignment necessary for stable and compatible Android applications. Be conscious of the version you choose, as it affects not only your source code but also the libraries and frameworks you might want to utilize in your project.

In summary, verify the compatibility settings during project setup and regularly review your Gradle configuration to ensure your applications run smoothly on various devices and Android versions.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.