Hi guys! Have you ever wondered what actually happens when you click the Run or Build button in Android Studio?

I’m a curious developer who loves to understand how things work internally. In this blog, let’s deep dive into what happens behind the scenes (BTS) when an Android app is built, and how .apk and .aab files are created.


1. Build Variants and Gradle Commands Link to heading

When you click Run or Build, Android Studio checks your build variant.

If it is Release: Link to heading

./gradlew assembleRelease   # for APK or AAR
./gradlew bundleRelease     # for AAB

If it is Debug: Link to heading

./gradlew assembleDebug
./gradlew bundleDebug

If you have a custom build variant: Link to heading

./gradlew assembleMyCustomVariant
./gradlew bundleMyCustomVariant

2. How Gradle Starts the Build Link to heading

Gradle works in three main phases:

  1. Initialization
  2. Configuration
  3. Execution

The flow looks like this:

settings.gradle        (Initialization)
        |
root build.gradle      (Configuration)
        |
app build.gradle       (Configuration)
        |
Tasks Execution        (Execution Phase)

settings.gradle Link to heading

  • Contains information about which modules are part of your project.

Root build.gradle Link to heading

  • Contains global Gradle configuration, repositories, plugins, etc.

App-level build.gradle Link to heading

  • Contains module-specific configuration
  • This is where the actual build tasks are executed.

3. Kotlin / Java Compilation Link to heading

During the Execution phase:

  • Kotlin files (.kt) are compiled using kotlinc
  • Java files (.java) are compiled using javac

Both are converted into:

.class (Java bytecode)

4. R8 and D8: From .class to .dex Link to heading

  • If R8 is enabled (usually in release builds):
    • It shrinks, optimizes, and obfuscates bytecode
  • Then D8 converts:
    .class → classes.dex
    

64K Method Limit Link to heading

  • One DEX file can hold about 65,536 method references
  • If this limit is exceeded:
    • MultiDex is enabled
    • You get:
      • classes.dex
      • classes2.dex
      • classes3.dex
      • and so on…

5. C / C++ with CMake (NDK Build) Link to heading

If your project contains C or C++ code, you define CMake in your app/build.gradle.

During Gradle execution:

  • CMake is executed first
  • It decides:
    • How many libraries to build
    • Which source files belong to which library
  • The actual build is done using:
    • Clang (compiler)
    • ld.lld (LLVM linker)

6. Understanding add_library() in CMake Link to heading

Example:

add_library(mycryptopackage SHARED
    encryptcrypto.cpp
    decryptcrypto.cpp
)

add_library(mycredentials SHARED
    rolekeeper.cpp
    resmanager.cpp
)

Here, we defined two shared libraries:

  • libmycryptopackage.so
  • libmycredentials.so

7. How .cpp Becomes .so Link to heading

For this example:

add_library(mycryptopackage SHARED
    encryptcrypto.cpp
    decryptcrypto.cpp
)

Step 1: Compilation (Clang) Link to heading

Each file is compiled into an object file:

encryptcrypto.cpp  → encryptcrypto.o
decryptcrypto.cpp  → decryptcrypto.o

Step 2: Linking (ld.lld) Link to heading

The linker (ld.lld – LLVM Linker) takes these object files and links them into:

libmycryptopackage.so

So the flow is:

.cpp → .o → .so

8. Loading the .so at Runtime Link to heading

When you call:

System.loadLibrary("mycryptopackage")
  • Android’s dynamic linker (/system/bin/linker or linker64) loads:
    libmycryptopackage.so
    
  • The library is mapped into your app’s memory and can be used via JNI.

9. Resources and Manifest (AAPT2) Link to heading

  • AAPT2 (Android Asset Packaging Tool 2):
    • Compiles and packages:
      • res/ resources
      • AndroidManifest.xml
    • Produces:
      • resources.arsc
      • Binary AndroidManifest.xml

10. Final APK / AAB Packaging Link to heading

At the end, Gradle packages:

  • classes.dex (and classes2.dex, if any)
  • .so files (inside lib/ folders per ABI)
  • resources.arsc
  • AndroidManifest.xml
  • Assets

Into:

  • APK (for install)
  • AAB (for Play Store)

Then it:

  • Signs the package
  • Aligns it (zipalign)
  • Produces the final output 🎉

11. Final Summary Link to heading

The full pipeline looks like this:

.kt / .java → (kotlinc / javac) → .class → (R8 / D8) → classes.dex

.cpp → (clang) → .o → (ld.lld) → libmylib.so

res/ + manifest → (AAPT2) → resources.arsc

All of these → Packaged → Signed → Aligned → APK / AAB

Conclusion Link to heading

So when you click Build or Run, a lot happens behind the scenes—from compiling Kotlin/Java, building native C++ libraries, processing resources, and finally packaging everything into an APK or AAB.

Understanding this pipeline makes you a much stronger Android developer 🚀