2026-11-17
Making the tiny Android app smaller
The first article ended with a 1,854 byte APK: 666 bytes of compressed library, a 555 byte signing block, 373 bytes of compressed manifest and 260 bytes of zip headers. I wanted to see if I could do better.
I could. make tiny now gives a 1,768 byte APK, 86 bytes fewer, signed with the same key and certificate. Three new targets shrink the certificate itself, and the smallest APK, from make record-p224, is 1,600 bytes. The app still draws the same gradient and answers its input. The tools have not changed: NDK r28 (clang 19), build-tools 35 and SDK platform 36.
As before, each section ends with the size of the APK after its changes, and the sizes are those of the unsigned APK until the section on the certificate. The first article's unsigned APK is 1,299 bytes.
Window calls
The library imported seven functions from libandroid.so. Three of them drive the window: ANativeWindow_setBuffersGeometry, ANativeWindow_lock and ANativeWindow_unlockAndPost. In Android's source, the last two are one line each:
return window->perform(
window, NATIVE_WINDOW_LOCK,
outBuffer,
inOutDirtyBounds);
An ANativeWindow is a structure of function pointers. perform is the one that takes an operation number and its arguments, as ioctl does. ANativeWindow_setBuffersGeometry makes three such calls, for the pixel format, the buffer size and the scaling mode. The app can make all these calls itself, and the three imports go away.
The NDK keeps the structure opaque. Its layout is in the platform's window.h. The NDK does not ship that header, nor the platform headers it includes, so the new native_window.h copies the structure only up to perform, which sits 152 bytes into the structure on arm64 and 88 on armeabi-v7a. A _Static_assert checks both offsets. The operations keep the names and numbers of window.h, and three inline functions replace the imports:
static inline int32_t
tiny_window_unlock_and_post(
ANativeWindow *window) {
return window->perform(window,
NATIVE_WINDOW_UNLOCK_AND_POST);
}
Until now the app called only public NDK functions, which keep working on every new Android. The window structure and the operation numbers are not public, and window.h marks most of the operations the app uses as private:
NATIVE_WINDOW_LOCK
= 11, /* private */
If a new Android changed one of these numbers, or moved perform, the app would call the wrong code. Android's current source has the same structure and numbers as native_window.h. My guess is that they will not change: GPU drivers call the window through the same structure, and a phone often gets a new Android without new drivers. I have not checked the older versions the app supports.
Each import cost a symbol of 24 bytes, a relocation and its name. The three names alone take 80 bytes. The code grows from 404 to 448 bytes, since each call now loads perform and passes the operation number. The library still shrinks, from 1,344 to 1,208 bytes, and from 666 to 621 compressed. In the first article, dlsym removed imports but kept their names, as strings. Here the names go too.
The unsigned APK drops to 1,254 bytes.
Division
The first article drew the gradient without a divide, for armeabi-v7a: some ARMv7-A cores have no divide instruction, and the compiler would call a helper from its runtime library for every pixel. ARM64 always has udiv, so the arm64 build now divides:
unsigned c = 0xff800000u
| ((y * 255 / h) << 8);
for (unsigned x = 0; x < w;
x++) {
row[x] = c | (x * 255 / w);
}
The other ABIs keep the subtraction loop, behind an #if. On arm64 the code drops from 448 to 420 bytes, and the library to 616 bytes compressed.
The unsigned APK drops to 1,249 bytes.
Manifest
The binary manifest is still 888 bytes, 373 compressed. Each attribute in it is a record of 20 bytes: a namespace, a name, a raw value and a typed value. The raw value is the attribute's text, as an index in the string pool, and the typed value is a type and 4 bytes of data. aapt2 fills them in three different ways in this manifest:
attribute raw typed
label "Tiny" string "Tiny"
hasCode none boolean 0
minSdk none integer 24
Android reads the typed value of these attributes, so for the label and the activity's name, the raw value only repeats it. apk.py manifest sets it to none, as aapt2 does for the others. A boolean stores true as 0xffffffff, and TypedArray.getBoolean also reads an integer, as true when it is not 0. So hasCode and exported become the integers 0 and 1, like the SDK versions. The records look more alike, and the manifest compresses to 370 bytes, 3 fewer, without changing size.
The string pool starts with a table of offsets, 4 bytes for each string, and the strings follow. In UTF-8, a string starts with two lengths and ends with a 0, so the empty string takes three zero bytes. It is stored first. The last offset in the table is below 256, so its three high bytes are zeros too. apk.py manifest moves the start of the strings three bytes back, onto those zeros, and drops the empty string's own three bytes. The pool must end on a multiple of 4 bytes, so one byte of padding comes back:
before:
xx 00 00 00 | 00 00 00 | ...
last offset empty
after:
xx 00 00 00 | ...
empty
Every other string keeps its offset. The same command also changes namespaces. Android looks up the attributes of uses-sdk, application and activity by resource id, so they lose their namespace. The action and the category keep it: the first article showed that the package manager looks up their name by namespace. And every element now points to the empty string for its namespace, instead of none. These changes save 4 more bytes. The manifest is 884 bytes, and 366 compressed.
The text of AndroidManifest.xml has not changed since the first article. Only its binary version has. Written back as XML, this is what is left in it, with resource ids where the names are gone:
<manifest package="c.tiny">
<uses-sdk
0x0101020c=24
0x01010270=36/>
<application
0x01010001="Tiny"
0x0101000c=0>
<activity
name=
"android.app.NativeActivity"
0x01010010=1>
<intent-filter>
<action android:name=
"android.intent.action.MAIN"/>
<category android:name=
"android.intent.category.LAUNCHER">
The first article shows the same manifest before these changes, and explains the ids. The booleans are now 0 and 1, the label and the activity's name have no raw copy, and only the action and the category keep the android: namespace. The file still ends without its five closing tags.
aapt2 dump badging prints the same lines as for the first build, and the phone still lists the app as Tiny.
The unsigned APK drops to 1,242 bytes.
Addresses and the loop
The app gives Android three of its own functions: two callbacks to NativeActivity, and the input callback to the looper. To take a function's address, the compiler writes two instructions, adrp for its 4 KB page and add for the rest. When the function is within 1 MB, one adr is enough, and lld turns the pair into a nop and an adr. These are the three pairs the first article found under --gc-sections. lld rewrites instructions in place and never removes one, so the nops stay. On arm64, inline assembly now writes the adr itself:
__asm__("adr %0, %c1"
: "=r"(redraw)
: "S"(draw));
The linker still checks that the function is in range. The arm64 loop also changed. It keeps x * 255 and y * 255 in two counters, and counts down to 0 instead of comparing with the width:
do {
*pixel++ = c | (rx / w);
rx += 255;
} while (--count);
The drawing code keeps its size, so the code only loses the three nops, from 420 to 408 bytes. With both changes, the library drops from 616 to 603 bytes compressed.
The unsigned APK drops to 1,229 bytes.
Hash table
When NativeActivity asks for ANativeActivity_onCreate, the dynamic linker hashes the name and searches the library's hash table. lld writes the GNU format: a Bloom filter, buckets, and the hash of each symbol. The first article tried the older System V format and found it larger. Cut down to one bucket, it is smaller.
A System V table is two counts, the buckets, then one chain entry for each symbol, all 32-bit words. A name's hash picks a bucket, which holds the index of a first symbol. The linker compares the names, and the symbol's chain entry gives the next symbol to try, until 0. lld gives this library six buckets for its six symbols: the null symbol, the four imports and the export. Only the export is defined in the library. apk.py hash keeps one bucket, which points to the export, and sets every chain entry to 0:
nbucket 1 nchain 6
bucket 1
chain 0 0 0 0 0 0
Every lookup in the library now ends at the export: a lookup for the export finds it, and a lookup for any other name compares it with the export and stops. That is the right answer, since nothing else is defined. The table keeps its 56 bytes, and the unused words become zeros. The library drops from 603 to 591 bytes compressed.
Two fields are only read by tools: the export's size, and the alignment of the dynamic segment. apk.py hash sets both to 0, and the library drops to 587 bytes compressed.
On armeabi-v7a, the same table crashed the app at launch:
get_string CHECK
'index < strtab_size_' failed
The first article put the string table's section header over the symbol table, on the value and the size of symbol 1, which was an import, and these fields of an import are never read. The GNU format needs the defined symbols last, so the export was symbol 5. With a System V table, lld puts it first, so symbol 1 became ANativeActivity_onCreate. Its address now pointed to the strings, and its size of 0 gave the linker an empty string table. armeabi-v7a keeps the GNU table. There, the alignment is also the name of a section header, and 0 compresses worse, so it keeps that too. Only its export's size goes to 0. apk.py now stops the build if a section header lands on a symbol the library defines.
The unsigned APK drops to 1,213 bytes.
The certificate
The certificate is where the biggest cut is: up to 168 bytes, against 86 for the code, the manifest and the hash table together. Signed with the first article's certificate, the APK is 1,768 bytes, and the signing block takes 555 of them. A v2 signing block carries the whole certificate. The certificate is in DER, the binary encoding of X.509, where each field is a tag byte, a length and the content:
02 01 01 INTEGER 1
(the serial number)
Besides the serial number, the certificate holds the algorithm of its own signature, the name of the issuer, the validity dates, the name of the subject and the public key, followed by the issuer's signature over all of that. The certificate signs itself, so the issuer and the subject are both CN=a.
Android's v2 verifier checks the APK's signature with the public key stored in the signing block. It then parses the certificate and checks that its public key is the same. It never checks the certificate's own signature.
apk.py cert short rewrites the certificate. The algorithm becomes an object identifier of one byte, 0, which names nothing:
30 03 SEQUENCE
06 01 00 OID 0.0
Each name keeps one attribute, whose type is that same OID 0.0, with an empty value. The signature becomes a bit string with no bits. The serial number, the dates and the key do not change. The certificate drops from 256 to 167 bytes, and make tiny-short gives a 1,677 byte APK, which apksigner verify still accepts.
make record goes further. With empty names, the certificate drops to 149 bytes, and apksigner rejects it. Android still installs the APK. I also tried dates without their seconds. apksigner accepted that certificate, and the phone refused it, so the dates keep their seconds.
An EC public key is a point on the curve: two numbers, x and y, of 32 bytes each with P-256. The certificate stores a byte 04, then x and y. But y can be computed from x, with the curve's equation, y² = x³ − 3x + b. Each x gives two values of y. They are numbers modulo an odd prime, and add up to it, so one is even and the other odd. A compressed point stores only x, after a byte that says which y it is:
04 | x | y 65 bytes
02 | x 33 bytes, y even
03 | x 33 bytes, y odd
In the certificate, the key also has 26 bytes of header, which name the algorithm and the curve. With them, it drops from 91 to 59 bytes. The signing block holds a second copy of the key, and with both copies compressed, the phone refused the APK:
Public key mismatch between
certificate and signature record
The verifier compares the signing block's key, as stored, with the certificate's key once Java has parsed it and encoded it again. On the phones I tested, that encoding comes out uncompressed. So only the certificate's key is compressed. The certificate is now 115 bytes, and make record gives a 1,626 byte APK.
A smaller curve gives a smaller key and a smaller signature. make record-p224 generates a separate P-224 key, and the certificate drops to 108 bytes, the APK to 1,600. The v2 specification lists P-256, P-384 and P-521, not P-224, and a 224 bit key is weaker. Android installs it anyway. Since the specification does not list it, a later version could refuse it.
Android compares the whole certificate when it updates an app. Each of these certificates is a new identity, even with the same key, and an APK signed with one cannot update an app signed with another. make tiny keeps the original certificate for that reason.
I tested the eight APKs, four certificates on two ABIs, on an Android phone: install, launch from the launcher, two taps, 12 seconds of waiting, then Back.
What I left out
The four imports left are the input functions. Removing them means finding libandroid.so's functions at run time, and no system call does that: dlopen is code in the dynamic linker, in user space. I measured dlopen and dlsym again with four imports, and the library grew from 616 bytes compressed to 766. A resolver that walks the loaded libraries with dl_iterate_phdr and reads their hash tables took 1,099. The app stays linked to libandroid.so. In a bigger program, with more libraries and more imports, I would use the resolver. It stores a 4-byte hash for each function, in place of its name, its symbol and its relocation, and its own code is paid once. I have not measured how many imports it takes to win.
Shorter names cost more than they save. Renaming ANativeActivity_onCreate to a saves 22 compressed bytes in the library, but the manifest then needs a meta-data element to name it, which costs 57. Renaming libmain.so also makes the APK bigger, for the same reason.
The package name can also be android.app.NativeActivity. The activity's name is already that string, so the string pool keeps one copy and loses c.tiny. The manifest compresses to 360 bytes, 6 fewer. But the package name is what identifies the app on the phone. Any other app built this way would have the same package name, and installing one would replace the other, or fail if it is signed with a different key.
Searching for the best order of the library's strings and of the manifest's string pool, and grouping the relocations differently, saves 7 more bytes. Such orders only fit this exact code, like the function order I left to lld last time, so the Makefile keeps the order of the tools.
Summary
Each step for arm64-v8a. Down to the hash table rows, the sizes are those of the unsigned APK. The last four rows are that APK signed with the four certificates:
| Step | APK (bytes) | Saved (bytes) |
|---|---|---|
| first article, unsigned | 1,299 | |
Window calls through perform | 1,254 | 45 |
| Division on arm64 | 1,249 | 5 |
| Manifest: raw values, booleans | 1,246 | 3 |
| Manifest: empty string, namespaces | 1,242 | 4 |
adr instructions, loop | 1,229 | 13 |
| Hash table: System V, one bucket | 1,217 | 12 |
| Hash table: export size, alignment | 1,213 | 4 |
Signed: original certificate, make tiny | 1,768 | |
Signed: short names, make tiny-short | 1,677 | |
Signed: compressed key, make record | 1,626 | |
Signed: P-224, make record-p224 | 1,600 |
The armeabi-v7a build keeps its subtraction loop and its GNU hash table, and only loses the window imports, the manifest bytes and its export's size. It goes from 1,780 bytes to 1,721 with the original certificate, and to 1,555 with P-224.
Conclusion
The code, the manifest and the hash table took 86 bytes from the unsigned APK. The certificate took up to 168 more. In the first article, most cuts removed what the compiler and the linker add for features this app does not use. This time most of them rely on what Android does not check, such as the certificate's own signature, or on what it keeps private, such as the window's operation numbers.
The smallest APK, signed with P-224, is now 587 bytes of compressed library, 387 of signing block, 366 of compressed manifest and 260 of zip headers. Of the library's 1,192 bytes, 408 are arm64 code.
The source is on GitHub, with a Makefile target for each of the four certificates.