|
| 1 | += How does Mill build Android apps? |
| 2 | + |
| 3 | +// tag::header[] |
| 4 | +:author: Vasilis Nicolaou |
| 5 | +:revdate: 17 September 2025 |
| 6 | + |
| 7 | +_{author}, {revdate}_ |
| 8 | + |
| 9 | + |
| 10 | +Until recently, Gradle was the only realistic option for Android builds. Today, Mill provides an alternative to Gradle that is easier to use and learn. |
| 11 | + |
| 12 | +In less than a year, Mill went from minimal Android support to producing installable APKs for projects as complex as: |
| 13 | + |
| 14 | +- xref:mill::android/hilt-sample.adoc[Jetpack Compose + Dependency Injection with Hilt] |
| 15 | +- xref:mill::android/java.adoc#_using_third_party_native_libraries[Android Native] |
| 16 | +- xref:mill::android/compose-samples.adoc[Jetpack Compose]. |
| 17 | +// end::header[] |
| 18 | +
|
| 19 | +- Multi-module Pokedex sample app (https://github.com/NicosNicolaou16/Pokedex_Compose_Multi_Module[original], https://github.com/vaslabs/Pokedex_Compose_Multi_Module/tree/testing-mill[with mill]) |
| 20 | +image:AndroidPokedexMultimoduleExample.png[A multi-module Android app built with Mill, showing a list of Pokémon and details for each pokemon.] |
| 21 | +
|
| 22 | +
|
| 23 | +=== Why you might prefer this to Gradle |
| 24 | + |
| 25 | +Because Mill’s Android support is built out of simple, object-oriented modules (AndroidModule, AndroidAppModule, etc.), the entire pipeline is transparent and hackable. If something doesn’t work, you don’t need to wait for a plugin update, you can open the task in your IDE, see the source, and tweak it yourself. This is the same design that let us implement end-to-end Android support in under a year, and it’s what makes Mill attractive if you value control and debuggability in your build. |
| 26 | + |
| 27 | +For example, you can inspect how Mill builds an Android APK with: |
| 28 | + |
| 29 | +[,console] |
| 30 | +---- |
| 31 | +$ ./mill inspect __.androidApk |
| 32 | +[1/1] inspect |
| 33 | +app.androidApk(AndroidAppModule.scala:439) |
| 34 | + Signs the APK using a keystore to generate a final, distributable APK. |
| 35 | +
|
| 36 | + The signing step is mandatory to distribute Android applications. It adds a cryptographic |
| 37 | + signature to the APK, verifying its authenticity. This method uses the `apksigner` tool |
| 38 | + along with a keystore file to sign the APK. |
| 39 | +
|
| 40 | + If no keystore is available, a new one is generated using the `keytool` utility. |
| 41 | +
|
| 42 | + For more details on the apksigner tool, refer to: |
| 43 | + [[https://developer.android.com/tools/apksigner apksigner Documentation]] |
| 44 | +
|
| 45 | +Inputs: |
| 46 | + androidSdkModule0.apksignerPath |
| 47 | + app.androidAlignedUnsignedApk |
| 48 | + app.androidSignKeyDetails |
| 49 | +... |
| 50 | +---- |
| 51 | + |
| 52 | +This post is a walkthrough of the basic Android build flow in Mill: what it does, why it’s complex, and why Mill makes it easier to reason about. |
| 53 | + |
| 54 | +== The Android build process |
| 55 | + |
| 56 | +Building and running a mixed Java/Kotlin application can be as simple as: |
| 57 | + |
| 58 | +.Standard JVM build pipeline |
| 59 | +[graphviz] |
| 60 | +.... |
| 61 | +digraph G { |
| 62 | + rankdir=TB |
| 63 | + node [shape=box width=0 height=0] |
| 64 | +
|
| 65 | + "Sources" -> "Compile (Java/Kotlin)" |
| 66 | + "Compile (Java/Kotlin)" -> "Jar" |
| 67 | + "Jar" -> "Run" |
| 68 | +} |
| 69 | +.... |
| 70 | + |
| 71 | +Android adds a dozen more steps, each involving different tools and formats: |
| 72 | + |
| 73 | +- Resource compilation (aapt2) for layouts, drawables, and strings |
| 74 | +- Manifest merging (app + library manifests) |
| 75 | +- Code shrinking and obfuscation (R8/Proguard) |
| 76 | +- DEX bytecode conversion (d8/r8) |
| 77 | +- APK packaging (resources + bytecode) |
| 78 | +- App signing (debug or release keystores) |
| 79 | +- Emulator deployment and test execution (ADB) |
| 80 | + |
| 81 | + |
| 82 | +Each step is order-sensitive: resources must be compiled before classes, manifests merged before packaging, APKs signed before installation. With Gradle, these steps are usually hidden inside plugin logic. When we explored Gradle builds, we often had to reverse engineer its behavior to understand what was going on. Mill instead exposes each phase as a target you can call, inspect its sources, or override. |
| 83 | + |
| 84 | +=== The Mill Android Build Pipeline |
| 85 | + |
| 86 | +.Standard Android build pipeline (without Hilt) |
| 87 | +[graphviz] |
| 88 | +.... |
| 89 | +digraph G { |
| 90 | +rankdir=TB |
| 91 | +node [shape=box width=0 height=0] |
| 92 | +
|
| 93 | +"Java/Kotlin Sources" -> "Compile (Java/Kotlin)" |
| 94 | +"Resources (res/)" -> "Compile Resources (aapt2)" |
| 95 | +"AndroidManifest.xml" -> "Manifest Merging" |
| 96 | +"Manifest Merging" -> "Linked Resources" |
| 97 | +"Compile Resources (aapt2)" -> "Linked Resources" |
| 98 | +"Compile (Java/Kotlin)" -> "Compiled Classes" |
| 99 | +"Linked Resources" -> "Package APK" |
| 100 | +"Compiled Classes" -> "DEX (d8/r8)" |
| 101 | +"DEX (d8/r8)" -> "Package APK" |
| 102 | +"Package APK" -> "Code Shrinking (r8/Proguard)" |
| 103 | +"Code Shrinking (r8/Proguard)" -> "Sign APK" |
| 104 | +"Sign APK" -> "Install to Emulator" |
| 105 | +"Install to Emulator" -> "Run/Test via ADB" |
| 106 | +} |
| 107 | +.... |
| 108 | + |
| 109 | + |
| 110 | +This flow considers plain apps without any dependencies. But real-world apps depend on libraries, which may have their own resources, manifests, and even native code. Android dependencies come with their own complexities: |
| 111 | + |
| 112 | +- Instead of a simple jar file that can be added to the classpath, Android libraries are distributed as AAR files, which are zip files containing compiled classes, resources, manifests, native libraries, Proguard files and more. |
| 113 | +- The AAR dependencies must be unpacked and each component processed separately in the appropriate step of the build pipeline. |
| 114 | + |
| 115 | + |
| 116 | +.Standard Android build pipeline with dependencies (AARs) |
| 117 | +[graphviz] |
| 118 | +.... |
| 119 | +digraph G { |
| 120 | + rankdir=TB |
| 121 | + node [shape=box width=0 height=0 style=filled fillcolor=white] |
| 122 | +
|
| 123 | + // --- App inputs |
| 124 | + "App Java/Kotlin Sources" -> "Compile (Java/Kotlin)" |
| 125 | + "App Resources (res/)" -> "Compile App Resources (aapt2)" |
| 126 | + "App AndroidManifest.xml" -> "Manifest Merging" |
| 127 | + "App Proguard rules" -> "Proguard rules" |
| 128 | +
|
| 129 | + // --- Library inputs |
| 130 | + subgraph cluster_aar { |
| 131 | + label="Dependency (AARs)" |
| 132 | + rankdir=TB |
| 133 | + node [shape=box width=0 height=0 style=filled fillcolor=white] |
| 134 | +
|
| 135 | + "AAR Files" -> "Unpack AARs" |
| 136 | + "Unpack AARs" -> "AAR classes.jar" |
| 137 | + "Unpack AARs" -> "AAR res/" |
| 138 | + "Unpack AARs" -> "AAR AndroidManifest.xml" |
| 139 | + "Unpack AARs" -> "AAR proguard.txt" |
| 140 | + "Unpack AARs" -> "AAR native .so (optional)" |
| 141 | + } |
| 142 | +
|
| 143 | + // --- Resource/link phase |
| 144 | + "AAR res/" -> "Compile Lib Resources (aapt2)" |
| 145 | + "Compile App Resources (aapt2)" -> "Linked Resources" |
| 146 | + "Compile Lib Resources (aapt2)" -> "Linked Resources" |
| 147 | + "AAR AndroidManifest.xml" -> "Manifest Merging" |
| 148 | + "Manifest Merging" -> "Linked Resources" |
| 149 | +
|
| 150 | + // --- Classes & DEX |
| 151 | + "Compile (Java/Kotlin)" -> "Compiled Classes" |
| 152 | + "AAR classes.jar" -> "Compile Classpath" |
| 153 | + "AAR classes.jar" -> "DEX (d8/r8)" |
| 154 | + "Compile Classpath" -> "Compile (Java/Kotlin)" |
| 155 | + "Linked Resources" -> "Package APK" |
| 156 | + "Compiled Classes" -> "DEX (d8/r8)" |
| 157 | + "DEX (d8/r8)" -> "Package APK" |
| 158 | +
|
| 159 | + // --- Proguard / main-dex rules |
| 160 | + "AAR proguard.txt" -> "Proguard rules" |
| 161 | + "Linked Resources" -> "Proguard rules" |
| 162 | + "Proguard rules" -> "DEX (d8/r8)" |
| 163 | +
|
| 164 | + // --- Native libs & META-INF (optional) |
| 165 | + "AAR native .so (optional)" -> "Package APK" |
| 166 | +
|
| 167 | + // --- Final steps |
| 168 | + "Package APK" -> "Code Shrinking (r8/Proguard)" |
| 169 | + "Code Shrinking (r8/Proguard)" -> "Sign APK" |
| 170 | + "Sign APK" -> "Install to Emulator" |
| 171 | + "Install to Emulator" -> "Run/Test via ADB" |
| 172 | +} |
| 173 | +.... |
| 174 | + |
| 175 | +The diagram above still doesn’t tell the whole story! It shows a typical build flow for a basic Android app, but there are more features to consider: |
| 176 | + |
| 177 | +- Hilt/Dagger code generation (annotation processing) |
| 178 | +- Jetpack Compose code generation (Kotlin compiler plugin) |
| 179 | +- Instrumented tests (separate APK, own resources, manifests, dependencies) |
| 180 | +- Native code (NDK builds, CMake integration) |
| 181 | + |
| 182 | +We cover a lot of these architecture styles in various Android examples, based on xref:mill::android/java.adoc[Java], xref:mill::android/kotlin.adoc[Kotlin] and third party integration examples covering xref:mill::android/compose-samples.adoc[Android Compose], xref:mill::android/android-native-example.adoc[Android Native] and xref:mill::android/hilt-sample.adoc[Dependency Injection with Hilt]. |
| 183 | + |
| 184 | +Endless tunnel sample app |
| 185 | +image:AndroidEndlessTunnelExample.png[An Android app built with Mill using native code, showing a 3D tunnel effect.] |
| 186 | + |
| 187 | +== Try it out |
| 188 | + |
| 189 | +Mill’s Android support is still young, but it already covers the full build pipeline: resource compilation, manifest merging, packaging, signing, running, and even testing on emulators. |
| 190 | + |
| 191 | +What makes this different from Gradle are control and transparency: every build step is a visible Mill task, easy to run on its own, inspect, check its dependencies, or override, without needing any extra/third party plugins. That means you can debug problems faster, adapt the pipeline to your project’s needs, and extend it without fighting opaque built-in or plugin logic. |
| 192 | + |
| 193 | +If you’re curious, the best way to appreciate this is to try it yourself: |
| 194 | + |
| 195 | +Get the `architecture-samples` containing the Todo App. |
| 196 | + |
| 197 | +[source,console] |
| 198 | +---- |
| 199 | +> git clone [email protected]:android/architecture-samples.git |
| 200 | +> cd architecture-samples |
| 201 | +---- |
| 202 | + |
| 203 | +Install mill |
| 204 | + |
| 205 | +[source,console] |
| 206 | +---- |
| 207 | +> curl -L https://repo1.maven.org/maven2/com/lihaoyi/mill-dist/1.0.5/mill-dist-1.0.5-mill.sh -o mill |
| 208 | +> chmod +x mill |
| 209 | +> echo "//| mill-version: 1.0.5" > build.mill |
| 210 | +> ./mill version |
| 211 | +---- |
| 212 | + |
| 213 | +Configure the mill build |
| 214 | + |
| 215 | +[source,console] |
| 216 | +---- |
| 217 | +> curl https://raw.githubusercontent.com/com-lihaoyi/mill/bef0194f3eecb4c7938f07e0cfcdf8d741a04468/example/thirdparty/androidtodo/build.mill >> build.mill |
| 218 | +---- |
| 219 | + |
| 220 | +Start the emulator and run the app |
| 221 | + |
| 222 | +[source,console] |
| 223 | +---- |
| 224 | +> ./mill show app.createAndroidVirtualDevice |
| 225 | +> ./mill show app.startAndroidEmulator |
| 226 | +> ./mill show app.androidInstall |
| 227 | +> ./mill show app.androidRun --activity com.example.android.architecture.blueprints.todoapp.TodoActivity |
| 228 | +---- |
| 229 | + |
| 230 | +The Android Todo App built with Mill |
| 231 | +image:AndroidTodoExample.png[The Todo app built with Mill, showing a list of tasks and a button to add new tasks.] |
| 232 | + |
| 233 | +Run the instrumented tests and watch the app being tested inside the emulator: |
| 234 | + |
| 235 | +[source,console] |
| 236 | +---- |
| 237 | +> ./mill app.androidTest |
| 238 | +---- |
| 239 | + |
| 240 | +image:androidtodo_test.gif[Android Test running inside an emulator, showing the Todo app being tested automatically.] |
| 241 | + |
| 242 | +Let's say you want to know how the apk is built. First, you can check the plan of `androidApk`, i.e which |
| 243 | +tasks it depends on: |
| 244 | +[,console] |
| 245 | +---- |
| 246 | +$ ./mill plan app.androidApk |
| 247 | +[1/1] plan |
| 248 | +androidSdkModule0.sdkPath |
| 249 | +androidSdkModule0.buildToolsVersion |
| 250 | +androidSdkModule0.platformsVersion |
| 251 | +androidSdkModule0.remoteReposInfo |
| 252 | +androidSdkModule0.installAndroidSdkComponents |
| 253 | +androidSdkModule0.buildToolsPath |
| 254 | +androidSdkModule0.apksignerPath |
| 255 | +androidSdkModule0.zipalignPath |
| 256 | +app.mandatoryMvnDeps.super.javalib.JavaModule |
| 257 | +app.kotlinVersion |
| 258 | +---- |
| 259 | + |
| 260 | + |
| 261 | +You can use this to visualise the relationships between these tasks and how they feed each other and ultimately the `androidApk` task: |
| 262 | + |
| 263 | +[,console] |
| 264 | +---- |
| 265 | +$ ./mill visualizePlan app.androidApk |
| 266 | +[3/3] visualizePlan |
| 267 | +[ |
| 268 | + ".../architecture-samples/out/visualizePlan.dest/out.dot", |
| 269 | + ".../architecture-samples/out/visualizePlan.dest/out.json", |
| 270 | + ".../architecture-samples/out/visualizePlan.dest/out.png", |
| 271 | + ".../architecture-samples/out/visualizePlan.dest/out.svg", |
| 272 | + ".../architecture-samples/out/visualizePlan.dest/out.txt" |
| 273 | +] |
| 274 | +[3/3] ============================== visualizePlan app.androidApk ============================== 2s |
| 275 | +---- |
| 276 | + |
| 277 | +You can also check the code of each task and what it does exactly inside your IDE: |
| 278 | +image:AndroidIDEExplore.png[Exploring the Mill Android build tasks in an IDE, showing the source code for the androidApk task.] |
| 279 | + |
| 280 | + |
| 281 | +In addition, due to xref:12-direct-style-build-tool.adoc#_direct_style_builds[Mill's direct style], you can reason what's going on with relative ease. |
| 282 | + |
| 283 | +=== Example: tweak the build in your `build.mill` |
| 284 | + |
| 285 | +[source,scala] |
| 286 | +---- |
| 287 | +import mill._ |
| 288 | +import mill.androidlib._ |
| 289 | +
|
| 290 | +object app extends AndroidAppModule { |
| 291 | + def androidApplicationNamespace = "com.example.app" |
| 292 | + def androidApplicationId = "com.example.app" |
| 293 | + def androidCompileSdk = 35 |
| 294 | +
|
| 295 | + // Add extra files into the APK |
| 296 | + override def androidPackageableExtraFiles = super.androidPackageableExtraFiles() ++ |
| 297 | + Seq( |
| 298 | + AndroidPackageableExtraFile( |
| 299 | + PathRef(moduleDir / "assets/about.txt"), |
| 300 | + os.RelPath("assets/about.txt") |
| 301 | + ) |
| 302 | + ) |
| 303 | +
|
| 304 | +} |
| 305 | +---- |
| 306 | + |
| 307 | +=== Further Exploration |
| 308 | + |
| 309 | +You may also inspect xref:mill::android/android-initial-setup.adoc[the getting started docs] to find out more. |
| 310 | + |
| 311 | +We’d love feedback from the Android community, whether it’s bug reports, feature requests, or success stories. If you’ve ever wished Android builds felt less like a black box, Mill is worth a look. |
0 commit comments