Rotating the signing key
Android only updates an app if the new version carries the signature of the installed one. Rotation exists to change keys without breaking that: a lineage is a file in which the old key vouches for the new one. An apk signed with the new key and with the lineage installs over one signed with the old key.
Rotation is part of the v3 signing scheme. Devices running Android 13 (API 33) or later use the new key. Earlier ones keep seeing the old one, which is why you have to keep signing with both.
1. Create the lineage
ngr rotate needs the two keystores and where to write the lineage:
ngr rotate -o lineage.bin \
--old-ks old.p12 --old-ks-pass env:OLD_PASS \
--new-ks new.p12 --new-ks-pass env:NEW_PASS
wrote lineage.bin
Keep lineage.bin with your keys. You need it for every signature from now on.
2. Sign with both keys and the lineage
ngr sign --ks old.p12 --ks-pass env:OLD_PASS \
--ks new.p12 --ks-pass env:NEW_PASS \
--lineage lineage.bin app.apk
Each --ks opens a signer, and its --ks-pass goes after it. The lineage decides which is the old key, not the
order you write them in.
3. Check
ngr verify -v app_signed.apk
Verifies
Verified using v1 scheme (JAR signing): true
Verified using v2 scheme (APK Signature Scheme v2): true
Verified using v3 scheme (APK Signature Scheme v3): true
Verified using v3.1 scheme (APK Signature Scheme v3.1): true
...
The v3.1 line at true is the one for the rotated key.
To see which certificates are in a lineage, or in that of an apk that is already rotated:
ngr lineage --print-certs lineage.bin
ngr lineage --print-certs app_signed.apk
Signer #1 in lineage certificate DN: CN=Example Old, O=Example, C=ES
...
Has installed data capability: true
Has shared UID capability : true
Has permission capability : true
Has rollback capability : false
Has auth capability : true
Signer #2 in lineage certificate DN: CN=Example New, O=Example, C=ES
...
The capabilities of the old key
Each key in the lineage has five capabilities, which say what an apk signed with it can keep doing:
| Capability | What it allows |
|---|---|
installed-data |
Keeping the installed data when updating from that key |
shared-uid |
Sharing a UID with apps signed with that key |
permission |
Sharing signature permissions with apps signed with that key |
rollback |
Going back to a version signed with that key |
auth |
That the key remains valid for authentication between apps |
A new link grants all of them except rollback. They are changed when creating the lineage, with --old-set and
--old-unset, or afterward with ngr lineage:
ngr lineage --ks old.p12 --ks-pass env:OLD_PASS --unset shared-uid -o lineage-2.bin lineage.bin
lineage only writes the output when something has changed.
Rotating again
To move to a third key, give rotate the lineage you already have. The outgoing key must be the newest one in the
lineage:
ngr rotate -o lineage-3.bin \
--old-ks new.p12 --old-ks-pass env:NEW_PASS \
--new-ks newer.p12 --new-ks-pass env:NEWER_PASS \
lineage.bin
Things to know
- Rotating cannot be undone on devices. A device that has installed a version with the new key expects that key from then on. Test the whole process with a test app first.
- Do not lose the old key yet. Devices earlier than Android 13 still need it.
--rotation-min-sdk-versionchanges from which API level the rotated key applies. By default it is 33.