Open In App

Desugaring in Android

Last Updated : 21 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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 

coreLibraryDesugaring ‘com.android.tools:desugar_jdk_libs:1.0.9’

Now after adding this dependency you have to add the below line in your compileOptions part of your code 

coreLibraryDesugaringEnabled true

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 

multiDexEnabled true

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: 

Kotlin




plugins {
    id 'com.android.application'
    id 'kotlin-android'
}
  
android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"
  
    defaultConfig {
        applicationId "com.gtappdevelopers.desugaring"
        minSdkVersion 19
        multiDexEnabled true
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
  
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
  
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
        coreLibraryDesugaringEnabled true
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}
  
dependencies {
  
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}


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. 

Desugaring in Android

In this way, Desugaring works and it provides backward compatibility for new Java libraries in the devices with lower API levels.     



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads