Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
"Desugaring" in the context of Android refers to the process by which newer Java language features and APIs can be used on older Android devices that don't natively support them. This is achieved through bytecode transformation, where the Java bytecode produced using the newer features is transformed into an equivalent form that can be executed on older Android runtimes.
Here's a more detailed look:
Why is desugaring needed?
Android apps are typically written in Java or Kotlin. Over time, both Java and Kotlin have introduced newer language features. However, older Android devices might be running older versions of the Android runtime (ART) that don't support these new features. To ensure that developers can still use modern language constructs while targeting older devices, Android introduced the concept of "desugaring."
How does desugaring work?
When you compile your Android app, the Java or Kotlin code is first converted to bytecode. If you're using newer language features that aren't supported on all the Android API levels you're targeting, the Android build tools will perform a process called desugaring. This process converts (or "desugars") the modern bytecode into a form that's compatible with older versions of the Android runtime.
Examples:
java.time
package, which offers a comprehensive and modern date/time API. For older Android versions, the Android Gradle Plugin provides desugaring to backport the java.time
APIs.How to enable desugaring?
If you're using Android Studio and the Android Gradle plugin, enabling desugaring is relatively straightforward. For Java 8 language features, you'll typically do something like this in your app's build.gradle
:
android { compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } // If using Kotlin, you might also have this kotlinOptions { jvmTarget = '1.8' } }
Limitations and Considerations:
Evolution:
As newer Android versions become widespread, the need to desugar certain features may diminish. For example, as the adoption rate of Android versions that natively support Java 8 features grows, fewer apps will need to desugar these features. However, as the Java language evolves (with Java 9, 10, 11, and onwards), new desugaring challenges and opportunities might arise.
In conclusion, desugaring is a powerful tool in the Android development ecosystem, enabling developers to use modern language features while maintaining compatibility with a wide range of devices.
Configuring desugaring in Android Gradle:
android { compileOptions { coreLibraryDesugaringEnabled true sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } }
Android Gradle Plugin and desugaring support:
dependencies { classpath 'com.android.tools.build:gradle:7.0.0' }