In an Android build, what do an APK, an AAB and an AAR each package, and who consumes each one?
answer
- two plugins, three outputs
- install format vs publishing format
- Play generates and signs the APKs
- library archive with resources and manifest
- consumer keep rules travel inside
basics
~20 sAn APK is the signed package a device installs and runs; an AAB is the publishing format Google Play turns into signed, per-device APKs; an AAR is an Android library archive that app modules consume as a dependency.
solid answer
~50 sAn app module (`com.android.application`) builds an APK with the `assemble` tasks or an Android App Bundle with the bundle task; a library module (`com.android.library`) builds an AAR. An APK holds DEX code, compiled resources, the merged manifest and native libraries, signed so a device can install it directly. An AAB holds the same compiled code and resources but defers APK generation and signing to Google Play, which serves each device only the code and resources it needs; new apps have had to publish AABs on Play since August 2021, and a bundle is not something a device installs. An AAR is a ZIP whose only mandatory entry is `AndroidManifest.xml`, usually with `classes.jar`, `res/` and `R.txt`, optionally native libraries and a `proguard.txt` of consumer keep rules; the consuming app merges all of it at build time.
code
kotlin · 20 linesplugins {
id("com.android.library")
}
android {
namespace = "com.example.notes.sync"
compileSdk = 37
defaultConfig {
minSdk = 24
consumerProguardFiles("consumer-rules.pro")
}
}
dependencies {
// Public functions of this library return WorkManager types.
api(libs.androidx.work.runtime)
// Used only inside the library.
implementation(libs.androidx.datastore.preferences)
}go deeper
Know which file a device installs, which file goes to Google Play, and which file a library produces.
Explain what an AAR contains beyond code, how its resources and keep rules merge into the app, and what api versus implementation changes for consumers.
Show you can test a bundle before release, diagnose resource clashes between libraries, and keep a library's API surface small with implementation.
Weigh where shared code lives: project modules versus published AARs, and what each costs in versioning, build time and consumer rules.
## Two plugins, three outputs The Android Gradle plugin is applied as one of two plugins that matter here. A module that applies `com.android.application` is an **app module** and produces something a device or a store can take. A module that applies `com.android.library` is a **library module** and produces something another module builds against. Turning an app module into a library is mostly a matter of switching the plugin id and deleting `applicationId`, which only an app module may define. | Output | Built by | What it is | Who consumes it | |---|---|---|---| | APK | App module, `assemble` tasks (`assembleRelease`) | A signed, installable package | A device, an emulator, a test runner, another app store | | AAB | App module, bundle task (`bundleRelease`) | A publishing format with all code and resources | Google Play, which generates per-device APKs | | AAR | Library module | An Android library archive | Other modules, at build time | ## The APK An **APK** (Android Package) is what actually runs. It contains the app's DEX bytecode, compiled resources, the merged `AndroidManifest.xml`, assets and native libraries for the ABIs you ship, plus a signature. You can install it directly with the IDE, `adb install` or a test harness, which is why debug builds and QA builds are usually APKs. ## The Android App Bundle An **AAB** holds all of the app's compiled code and resources but leaves two jobs to Google Play: generating APKs and signing them. - Play builds a base APK plus configuration APKs per device, so a phone downloads only its language, screen density and ABI, and users get smaller downloads. - New apps have had to publish with the App Bundle on Google Play since August 2021; legacy apps created before then may still release APKs. - The total compressed download of the APKs needed to install the app must stay within 4 GB. - Bundles do not support APK expansion (`.obb`) files. - The app's `bundle { }` block sets `enableSplit` per configuration type (language, density, ABI); leaving the splits on is what keeps each download limited to what the device needs. - Sideloading only some of the split APKs fails on Google-certified devices and on Android 10 (API 29) and higher; to test a bundle locally, generate APKs from it with bundletool rather than installing the `.aab`. ## The AAR An **AAR** (Android Archive) is a ZIP file with the extension `.aar`. Unlike a plain JAR, it can carry Android resources, a manifest and native code. Its only mandatory entry is `/AndroidManifest.xml`; the usual optional entries include: - `/classes.jar` with the compiled code, and `/res/` with the resources. - `/R.txt` and `/public.txt`, the resource symbols and the resources the library declares public. - `/jni/<abi>/*.so` native libraries, `/assets/`, `/libs/*.jar`. - `/proguard.txt`, the **consumer keep rules** set with `consumerProguardFiles`, which the app's R8 run appends to its own configuration. - `/lint.jar` with custom lint checks. Some build-time consequences follow from merging an AAR into the app: 1. Each library generates its own `R` class in its own namespace package. 2. If the app and a library define a resource with the same name, the app's resource wins; between two AARs, the one listed first in `dependencies` wins. 3. The app's `minSdk` must be equal to or greater than the library's. 4. `compileOnly` cannot be used with AAR dependencies. 5. All of a library's resources are public by default; declaring at least one in `res/values/public.xml` makes the rest private, and lint then warns consumers who reference them. How the AAR travels matters too. Published to a Maven repository, it carries metadata, so its own dependencies resolve in the consuming build and duplicates are removed. A raw `.aar` file dropped into a `libs/` folder carries no such metadata, and the consuming app must declare the library's transitive dependencies by hand. ## api vs implementation in a library module How a library declares its own dependencies decides what its consumers see: - `implementation` puts a dependency on the library's compile classpath and in the build output, but does not leak it to consumers at compile time. A change to it recompiles only the library and its direct dependents, so it keeps builds fast. - `api` also exports the dependency to every consumer's compile classpath. Use it only when the library's public classes expose that dependency's types, because a change to an `api` dependency recompiles everything that can see it. ## Picking the right artifact Ask who receives the file: a store gets an AAB, a device or tester gets an APK, and another module gets an AAR (published to a Maven repository, so its own dependencies travel as metadata, or added as a project dependency).
- Why can't you hand a tester the .aab file to install?A bundle is a publishing format, not an install format: Google Play generates the device-specific APKs from it and signs them. To test outside Play, build an APK from the app module or generate APKs from the bundle with bundletool. Copying over only some of the split APKs fails on Android 10 and higher, because the install is missing required splits.
- When should an Android library declare a dependency with api instead of implementation?Only when the library's public classes or functions expose that dependency's types, so consumers must compile against it. Everything else should be `implementation`: it still ships at run time, but it stays off consumers' compile classpath, and changing it recompiles far fewer modules.
- How does a library make sure apps keep the code it loads by reflection?It ships consumer keep rules: `consumerProguardFiles` in the library's `defaultConfig` packages them as `proguard.txt` inside the AAR, and the app's R8 run appends them to its own rules. Since AGP 9.0 an app filters global options such as `-dontobfuscate` out of library consumer rules, so a library can only keep its own code.
- An AAB can be sideloaded onto a device like an APK
- An AAR is just a JAR with a different file extension
- A library should run R8 itself so its AAR ships shrunk
- Declare every library dependency with api so consumers never miss a class
- Google Play installs the uploaded AAB exactly as it was built