Google has officially announced Kotlin as a recommended language for Android Development and that’s why so many developers are switching from Java to Kotlin for Android development. So day by day new APIs are been introduced in Android by the Google Team and which are available in newer versions of Android devices as well. So Desugaring is also one of the important features in Android that we should enable in our app which will allow our apps to work in lower API levels as well. So in this article, we will learn about Desugaring.
- What is Desugaring?
- Why we need Desugaring?
- Practical Implementation of Desugaring.
- What’s happening under the hood?
What is Desugaring?
Android devices are getting optimized day by day. So many features in Android for users as well as developers are getting optimized. When any operating system of Android is developed, it is delivered with so many Java classes which provides support to different apps in user’s devices for various functionalities such as time, date, and many other features. Now if we consider time API it was introduced in API level 26 and the apps which use this API will crash on the lower API level devices such as Marshmallow and lower versions. So in this case to avoid the app crashes Desugaring comes into play. It allows lower API levels to work with new JAVA libraries.
Why We Need Desugaring?
Suppose for example we are using a new time API which is introduced in API level 26 and we have used this API of time in our app. This app will work perfectly for API level 26 and above but when we will install this app on the lower API level let suppose API level 23 or lower than that then our app will crash because our device OS supports the features provided from API level 23 and we are using features of API level 26 which will not work. So for avoiding this app crashes and to support the new features from API level 26 into API level 23 we have to add Desugaring in our app.
Practical Implementation of Desugaring in Android
When we are using features provided by API level 26 in the devices lower than API level 26 then the app will crash. In the apps with a lower API level, we will get to see an error as NoClassDefFoundError. So to resolve this error we have to enable Desugaring in Android.
Step by Step implementation of Desugaring
Step 1: Navigate to the Gradle Scripts > build.gradle(:app) and inside your dependencies section add the dependency given below
Now after adding this dependency you have to add the below line in your compileOptions part of your code
Step 2: Now we have to enable multiDex in our app for that navigate to your Gradle file inside that in the section of defaultConfig section add the below line as
And sync your project. After the successful project sync, Desugaring has been implemented in our app and now we will not get to see NoClassDefFoundError in the devices with lower API levels. The complete code for the Gradle file is given below:
What’s Happening Under the Hood?
So what is happening here and how the java classes which were missing suddenly appear here. This is because of D8 and R8 tools. Previously for converting your app’s code in dex code we have to use Proguard but to reduce compile-time and to reduce app size the new R8 tool was introduced. This R8 tool provides the support of missing Java classes. When this tool converts your app’s code into dex code it also adds dex code of new java libraries which are then added in the APK. You can get a clear idea about this process from the below diagram.
In this way, Desugaring works and it provides backward compatibility for new Java libraries in the devices with lower API levels.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!