Android studio build system is based on Gradle. Gradle has a lot of plugins. Android Gradle Plugin (AGP) is one of them which manages the full build process through several tools and steps to convert an android project to an APK or AAB. Here’s a simplified summary:
Step 1: Resource Compilation
In first step, resource compilation is started. AGP uses AAPT tools for this task. AAPT compile all the resource under res directory like layout files, drawables etc including AndroidManifest.xml file to binary format and generate R.java file with resources ids.
Step 2 : Source Code Compilation:
Second step is for compiling source code. In this step all java and kotlin files are compiled (including R.java file generated in step 1 and the code from app dependencies and libraries) to .class files (java bytecode). AGP uses kotlinc compiler to compile Kotlin file and javac compiler to compile java files.
Step 3: Shrinking, Obfuscation, and Optimization:
This step is optional. Only if we enable code shrinking and obfuscation then this step will be executed on compiled resources and .class files (java bytecode) .
R8 and ProGuard are tools used for this (R8 offers better Kotlin support and more size reduction).
Step 4: Dalvik Bytecode (DEX Files) Generation:
Now .class files are converted to .dex files. But why? becauuse .class files are ok for JVM to run. But Android apps runs on ART previously on DVM. Both ART and DVM do not understant .class files(java bytecode). They only understand DEX files (Dex bytecode) thats why all the .class files are converted to .dex files using D8 dex compiler. D8 also enables ART to use Java 8 features through this compilation process. ART does not support java 8 features by default. Desugaring is a process through which D8 essentially converts Java 8 bytecode to Java 7 bytecode to support in ART.
Step 5: Packaging
This is the final step where all DEX files and compiled resources are combined into an APK or AAB by the packager.
Remember, the debug version is automatically signed, while the release version requires configuration of a release keystore. That’s a simplified overview of the Android build process!
Happy Coding!