d8 is a command line tool that Android Studio and the Android Gradle Plugin
use to compile your project's Java bytecode into DEX bytecode that runs
on Android devices, and it allows you to use Java 8 language features in your
d8 is also included as a standalone tool in Android Build
Tools 28.0.1 and higher:
d8 is simple to use and requires only a path to the compiled Java bytecode
that you want to convert into DEX bytecode, as shown below.
The input bytecode can be in any combination of
*.class files or
containers, such as JAR, APK, or ZIP files. You can also include
DEX files for
d8 to merge them into the DEX output, which is useful
when including output from an incremental build.
d8 compiles the Java bytecode into optimized DEX files
and includes debug information that you can
use to debug your code during runtime. However, you can include optional
flags to, for example, perform an incremental build, specify classes that
should be compiled into the main DEX file, and specify paths to
additional resources required to use Java 8 language features.
d8 path-to-input-files [options]
The following table describes the optional flags you can use with
Compiles DEX bytecode to include debug information, such as debug symbol
This option is enabled by default. To include debug information in your
When compiling DEX files for the release version of your app or
library, instead use the
Compiles DEX bytecode without debug information. However,
You should pass this flag when compiling bytecode for a public release.
Specifies the desired path for the DEX output. By default,
If you specify a path and name of a ZIP or JAR file,
Specifies the path to the
Specifies classpath resources that
||Specifies the minimum API level you want the output DEX files to support.|
Pass this flag to let
When you want to compile DEX files that you intend to run on a device, exclude this flag and specify the path to the intermediate DEX classes as an input.
Compiles each class into separate DEX files.
Enabling this flag allows you to perform more incremental builds by re-compiling only the classes that have changed. When performing incremental builds using the Android Gradle Plugin, this optimization is enabled by default.
You can not use this flag while also specifying
||Disables Java 8 language features. Use this flag only if you don't intend to compile Java bytecode that uses Java 8 language features.|
Specifies a text file that lists classes
Because the Android system loads the main DEX file first when starting your app, you can use this flag to prioritize certain classes at startup by compiling them into the main DEX file. This is particularly useful when supporting legacy multidex because only classes in the main DEX file are available at runtime until the legacy multidex library is loaded.
Keep in mind, the each DEX file must still satisfy the
the 64K reference limit. So, be
sure not to specify too many classes for the main DEX file or you get a
compile error. By default, when specifying classes using
You can not use this flag while also specifying
Prints the version of
Prints help text for using
Perform incremental builds
To improve build speeds during development, such as for continuous integration
builds, you can instruct
d8 to compile only a subset of your project's Java
bytecode. For example, if you enable per-class dexing, you can re-compile only
the classes that you have modified since the previous build.
The following command performs an incremental build of a few classes and enables per-class dexing. The command also specifies an output directory for the incremental build.
d8 MainActivity.class R.class --intermediate --file-per-class --output ~/build/intermediate/dex
d8 performs an incremental build, it stores additional information in the
DEX output that it later uses to correctly process the
option and merge DEX files during a full build of your app. For example, when
processing Java 8 lambda classes,
d8 keeps track of which lamdba classes are
created for each input class. During a full build, when
d8 includes a class in
the main DEX file, it consults the metadata to ensure all of the lambda
classes created for that class are also included in the main DEX file.
If you have already compiled all of your project's bytecode into DEX files
across multiple incremental builds, you can perform a full build by passing the
directory of intermediate DEX files to
d8, as shown below. Additionally, you
can specify the classes you want
d8 to compile into the main DEX file using
--main-dex-list. Because the input is a set of files that is already compiled
into DEX bytecode, this build should complete faster than a clean build.
d8 ~/build/intermediate/dex --release --main-dex-list ~/build/classes.txt --output ~/build/release/dex
Compile bytecode that uses Java 8 language features
d8 enables you to use Java 8 language features
in your code through a compile process called desugaring, which converts these
useful language features into bytecode that can run on the Android platform.
Android Studio and the Android Gradle Plugin include classpath
d8 requires to enable desugaring for you. However, when using
d8 from the command line, you need to include them yourself.
One such resource is the
android.jar from your target Android SDK. This
resource includes a set of Android platform APIs, and you specify its path using
Another resource is the set of your project's compiled Java bytecode that you
are currently not compiling into DEX bytecode but require to compile
other classes into DEX bytecode. For example, if your code uses
default and static interface methods, which is a Java
8 language feature, you need to use this flag to specify the path to all of your
project's Java bytecode, even if you don't intend to compile all of it into DEX
bytecode. That's because
d8 requires this information to understand
your project's code and resolve calls to the interface methods.
The following code sample performs an incremental build of a class that accesses a default interface method:
d8 MainActivity.class --intermediate --file-per-class --output ~/build/intermediate/dex --lib android_sdk/platforms/api-level/android.jar --classpath ~/build/javac/debug