Dex compilation is an integral part of the Android application development process. It’s during this process that the compiler transforms .class bytecode into .dex bytecode. This is a requirement for applications to work for the Android Runtime (or Dalvik in the case of older versions of Android). Most of the work here is done under the hood, but it’s this process that impacts how long it takes for your application to build for each new version you want to test.
Android has introduced D8/R8 compilation to speed up build time and smaller dex sizes.Following changes are made in Compilation process.
Incremental Dexing -
When we run gradle build in android there is two major steps.First conversion of java/kotlin class file to java bytecode then in second step it is converted to dalvik bytecode.
Before gradle 3.0 whenever there is some change in java code only that file and affected file will be recompiled(incremental changes) for java bytecode conversion but for dalvik bytecode conversion whole code is recompiled.
In Gradle 3.0+Gradle builds Dex conversion process has been breakdown in two parts ,now individual dex file be made for each class and in next steps all files will be merged.So After this Dex conversion process will be also incremental.In debug build it will be automatically enabled.
Note:-Dex merging process is very fast because dalvik bytecode is Register based machine.
Note- Incremental changes are only available when we are not using annotations because annotation can reach any part of source code so it can’t be incremental.
New Java language features required new bytecodes and language APIs, older devices not support these. Desugar will replace new Bytecodes and language APIs with older ones.It will be run After Java bytecode conversion.
D8 is new Dexer, Dexer is a tool that convert java bytecodes from the stack based machines to Dalvik bytecodes which is register based.Previously DX is used as dexer.
D8 have faster compilation, smaller code and provide better debug info at compile time. Desugaring steps is also integrated inside D8 .
New shrinker and optimizer, it is compatible with proguard.
Proguard is run before DX generaion previously because proguard is java to java compilation so it will increase build time.
R8 can be enabled by setting flag in gradle properties file.It will replace Proguard ,Desugar, D8 with one single step R8. It will save around 25% of dex file size.
So that’s all about changes in compilation process.Check more posts from Google I/O 2018;
Android Runtime Improvements
Android Runtime (ART) is an application runtime environment used by the Android operating system. Replacing Dalvik, the…