Enable desugaring to use modern Java features on lower API levels
In this blog, we’re going to explore how to support older Android devices by lowering the
minSdk
version in ourgradle
file, without compromising on modern Java API features through desugaring. We’ll also dive into its benefits and more. Let’s jump right in!
Why do we need to support older android devices?
Supporting older Android devices can be crucial for several reasons, especially when aiming for a broader user base and ensuring the longevity of an app. Here’s why it’s often important:
- Wider Reach: Older Android versions like 9 (Pie) still run on 8.4% of devices, and Android 11 holds 19% share as of 2024. By lowering the minimum API level, developers can cater to a larger audience and ensure their apps work across more devices.
- Emerging Markets: In regions with less access to the latest devices, users often depend on older Android versions. Supporting these versions ensures inclusivity and better global market penetration.
How to support these older android versions?
As you already know, minSdk is responsible for setting up at least the api android version that our can app support. There is a well known problem when lowering our SDK version, which is the Java language api feature. Ex: If you want to use LocalDate (from java.time.LocalDate), we need to set minSdk to api level 26 (Android 8.0 Oreo). This is just one example from java, but there are a lot of java language features that still require higher minSdk in our app. In the next section we will discuss how to overcome this problem.in the next section we will discuss how to overcome this problem.
To overcome this is problem Starting from Android Gradle plugin 4.0.0 and higher provides built-in support for using Java 8 language features and third-party libraries that use them.
The default toolchain implements the new language features by performing bytecode transformations, called desugar
, as part of the D8/R8 compilation of class files into DEX code
To understand this process, we first need to understand both the D8 and R8 tools.
D8 Dexter: D8 is a tool that converts our java byte code .class into .dex for android runtime.
R8 Shrinker: R8 is a code shrinking and minification tool that converts java byte code .class to optimized dex code .dex
How to integrate this into our android studio project?
We previously learned about desurgaring to use the java 8+ language feature. Now, let’s see how to integrate this into our project.
- Update your agp to 4.0.0+ (for new projects it definetely higher than this)
- For module that uses this Java 8 lang features either in its source code or plugin dependency, update the module’s
build.gradle
for groovy orbuild.gradle.kts
for kotlin DSL file as shown below:
To support these language APIs, the plugin compiles a separate DEX file that contains an implementation of the missing APIs and includes it in your app. The desugaring process rewrites your app’s code to instead use this library at runtime.
Note: If you also want to access Java 11 language features, use the coreLibraryDesugaring lib version 2.0.3 or higher, and your Android Gradle plugin needs to be 7.4.0 or higher. For all the versions visit CHANGELOG.md
For a complete list of supported APIs, visit Java 8+ APIs available through desugaring and Java 11+ APIs available through desugaring.
Conclusion
In this short blog, we learned how to use api desugaring for our project to support older android devices to reach a broader audience, and also learned how it works and more. I hope you find this blog helpful and I will see you in the next upcoming article.