# D8 Library Desugaring

DevFeed: [D8 Library Desugaring](<https://devfeed.tech/articles/d8-library-desugaring-20928.md>)

Original publisher: [Read original article](<https://jakewharton.com/d8-library-desugaring/>)

Published: 2019-12-18T00:00:00Z

Content type: article

Language: en

Sources: [Jake Wharton](<https://devfeed.tech/sources/jake-wharton.md>)

Topics: [Android](<https://devfeed.tech/topics/android.md>), [Java](<https://devfeed.tech/topics/java.md>), [Library](<https://devfeed.tech/topics/library.md>), [Android Studio](<https://devfeed.tech/topics/android-studio.md>), [Streams](<https://devfeed.tech/topics/streams.md>), [Google](<https://devfeed.tech/topics/google.md>), [API](<https://devfeed.tech/topics/api.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-studio](<https://devfeed.tech/tags/android-studio.md>), [google](<https://devfeed.tech/tags/google.md>), [java](<https://devfeed.tech/tags/java.md>), [library](<https://devfeed.tech/tags/library.md>), [streams](<https://devfeed.tech/tags/streams.md>)

## AI overview

This article discusses D8 core library desugaring, an Android toolchain feature that makes selected newer Java APIs, including streams, optional, and time APIs, available on older Android versions. It describes the feature's relationship to Android Studio, Java library compatibility, and earlier API desugaring behavior.

## Source excerpt

Note: This post is part of a series on D8 and R8, Android's new dexer and optimizer, respectively. For an intro to D8 read "Android's Java 8 support". For an intro to R8 read "R8 Optimization: Staticization". So far in this series the coverage of D8 has been about desugaring of Java 8 language features, working around vendor- and version-specific bugs in the platform, and performing method-local optimization. In this post we'll cover an upcoming feature of D8 called "core library desugaring" which makes newer APIs available on older versions of Android. Library desugaring of Java 8 APIs such as streams, optional, and the new time APIs was announced at the developer keynote of Google I/O 2019 and delivered at Android DevSummit 2019 with the first canary build of Android Studio 4.0. This will allow developers to use these features introduced in API 24 and 26 on every version their app targets. No more backport libraries and duplicated APIs! This is also a boon to the Java library ecosystem. Many libraries have long-since moved on to Java 8 but are unable to use newer APIs in order to maintain Android compatibility. While every new API is not available, D8 desugaring should allow these libraries to use the APIs which are most desired. Not a new feature Despite the recent fanfare, desugaring APIs is not a actually a new feature of D8. Since it became a usable alternative to dx, D8 has desugared calls to the API 19 Objects.requireNonNull method. But, why that one method? Certain code patterns will cause the Java compiler to synthesize an explicit null check. class Counter { final int count = 0; } class Main { void doSomething(Counter counter) { int count = counter.count; } } When compiled with JDK 8, the Java bytecode of the doSomething method contains a call to getClass() whose return value is then thrown away. void doSomething(Counter); Code: 0: aload_1 1: invokevirtual #2 // Method java/lang/Object.getClass:()Ljava/lang/Class; 4: pop 5: iconst_0 6: istore_2 ⋮ The zero