App Security
Android Development with Compose: Production & Publishing
Course 6 ยท Chapter 5 ยท Locking the Door
Security advice goes out of date faster than most Android topics, and this chapter is one of the three the course outline flagged as risky. So it is open about what it checked and how old things might be: read the evidence box first, and treat the specific classes and settings as things to confirm against the current docs before you ship.
isMinifyEnabled for code and isShrinkResources for resources in the release build type (legacy DSL); resource shrinking only works with code shrinking; use proguard-android-optimize.txt, because the older proguard-android.txt does not enable R8's optimisations; your own rules go in a file such as proguard-rules.pro; keep rules from all sources are merged; newer AGP (9.3 and higher, per the docs) has an optimization { enable = true } block and .keep files in src/<variant>/keepRules.
Secrets: the security checklist advises against keys in source code and mentions the secrets-gradle-plugin; local.properties is described as reserved for AGP, so use a separate properties file for your own values.
Storage: EncryptedSharedPreferences is deprecated (security-crypto 1.1.0), with the Jetpack notes pointing to platform APIs and direct use of the Android Keystore; its files should be excluded from Auto Backup because restoring fails without the key; the checklist advises the Keystore for keys and a tool such as Tink for stored keys; Keystore key material never enters the app process and Keystore work should stay off the main thread; DataStore has a datastore-tink artifact with an AEAD serializer; sensitive data belongs in internal storage.
Biometrics: BiometricPrompt with an executor, a callback and a PromptInfo using setAllowedAuthenticators(BIOMETRIC_STRONG or DEVICE_CREDENTIAL); a CryptoObject ties a key to the prompt, and then fallback to device credentials is not allowed for that key; the biometric codelab found is itself marked deprecated; no Compose wrapper was in the results.
Network: cleartext is off by default from API 28; per-domain opt-ins in a domain-config; debug-overrides do not affect release; a pin-set holds pins with an optional expiration date.
Backup: allowBackup, and dataExtractionRules on Android 12+ with separate cloud and device-transfer rules.
Not found: mapping.txt and retracing stack traces; the secrets plugin's setup; a full pin example. General knowledge, not checked: everything labelled so below. All code is unrun (no Kotlin compiler on the machine used to write this); nothing was seen on your install.
Shrinking and Obfuscating with R8
- (docs) R8 removes unused code and resources, shortens names and optimises, in the release build. Resource shrinking depends on code shrinking.
- (docs) The default file you pass matters: the
-optimizeone turns the optimisations on. - (general knowledge) Code that is found by name at run time (reflection) can be removed or renamed by R8 and then crash only in release builds. That is what keep rules are for. Many libraries ship their own rules, so add your own only when something breaks, and keep them narrow.
- (general knowledge, not found in the searches) Obfuscated stack traces need the
mapping.txtfile R8 writes with each release build; keep it for every version you publish (Play can also use it). The searches did not cover retracing, so confirm the details in the docs. - Always test the release build (general knowledge): a debug build does not run R8, so a keep-rule bug shows up only there.
Keeping Keys Out of the Code
(docs) Do not put keys in source code. (general knowledge) Keeping a key out of the repository is not the same as keeping it secret: whatever is inside the APK can be read by someone determined. If a key must really stay secret, it lives on a server that your app asks, and the server decides what the app is allowed to do.
Secure Storage
- Start by asking whether you need to store it. (general knowledge) Not storing a secret is the strongest protection.
- Internal storage only. (docs) Sensitive data belongs in the app's sandboxed internal storage, whatever Android version you target.
EncryptedSharedPreferencesis deprecated (docs). Older tutorials, and Android Development: Production & Publishing 5's era of advice, use it. The Jetpack notes now point to platform APIs and direct use of the Android Keystore. Check the current docs for the replacement for your case before relying on any wrapper library.- The Keystore (docs): keys are generated and used inside it and never enter your process; use it off the main thread. A common design (docs: keys with a tool such as Tink) is a Keystore-held key that encrypts the data you store.
- DataStore with Tink (docs): a
datastore-tinkartifact wraps the serializer with AEAD encryption, with a unique associated-data value per file. (general knowledge) That suits the Proto DataStore of Architecture & Data 6 when a setting is sensitive. Your notes themselves are in a Room database that is not encrypted by default; encrypting it is a bigger decision (it needs a third-party library or similar, not checked here), so decide whether your notes really need it.
Biometric Unlock
- (docs) The prompt is built with an executor, a callback and a
PromptInfo;BIOMETRIC_STRONG or DEVICE_CREDENTIALallows a fingerprint, face or the screen lock. - (docs) The biometric codelab I found is marked deprecated, so treat the sample as a pattern, and confirm the current API in the "Show a biometric authentication dialog" page.
- (general knowledge) BiometricPrompt works with a
FragmentActivity, so check what yourMainActivityextends. The check withcanAuthenticateavoids showing a prompt the device cannot answer. - (docs) A
CryptoObjectbinds a Keystore key to the prompt, which is cryptographically stronger than a screen lock, but a key like that cannot allow device-credential fallback. Choose by what you protect: a "private notes" screen can use the simple gate; unlocking encrypted data wants the key.
Network Security
- (docs) Cleartext (non-HTTPS) traffic is off by default from API 28; opt in only per domain, and only if you must. The sample repo from Architecture & Data 8 talks to an HTTPS API, so none is needed.
- (docs)
debug-overridesapplies in debug builds without changing release. - Pinning (docs: a pin-set with an optional expiration) makes the app trust only certain keys. (general knowledge) It protects against a rogue certificate authority but breaks the app if the server's key changes and your pins are stale, so use it only if you control the server and can update the app in time. Described here, not recommended for a notes app.
Backups
- (docs)
allowBackupcontrols participation; on Android 12+dataExtractionRulesseparates cloud backup from device transfer, and you can exclude sensitive files. - (docs) Encrypted preferences and Keystore-bound ciphertext should be excluded: after a restore the key is missing and the data cannot be read.
- (general knowledge) Also check that the files you name really exist under those paths; a rule that excludes nothing gives a false sense of safety.
A Short Security Checklist for the Notes App
| Risk | What this chapter does |
|---|---|
| Reverse engineering | R8 on release (raises effort, not a guarantee (general knowledge)) |
| Keys in the repository | Separate untracked properties file; server-side for real secrets |
| Someone picks up the phone | Biometric gate on private notes |
| Data on the device | Internal storage; encrypt only what needs it; exclude it from backups |
| Traffic | HTTPS only (the default) |
EncryptedSharedPreferences is now deprecated, uses the -optimize default file, and notes the newer AGP keep-rules layout.
Try It Yourself
Turn on R8 for the release build, build a release APK or bundle, and run it. Report whether it works. If something breaks, find it in the logs and write the narrowest keep rule that fixes it. Say which AGP version and which DSL form you used.
๐ View solutionAdd the biometric gate to a "private notes" screen. Test on an emulator with a fingerprint enrolled and with none. Report what canAuthenticate returns in each case and what your app does.
Audit your own app against the checklist table: list every place it stores data, every network call, whether backups include it, and any key in the code. Then check the current docs for the replacement for EncryptedSharedPreferences and write what you found.
local.properties being reserved; the EncryptedSharedPreferences deprecation and backup exclusion; Keystore key handling and the Tink advice; datastore-tink; internal storage; BiometricPrompt and CryptoObject behaviour; the deprecated codelab; cleartext defaults, debug-overrides and pin-set; allowBackup and dataExtractionRules.General knowledge, not checked: why release-only crashes happen and what reflection has to do with it;
mapping.txt and retracing; the properties-file code and the claim that anything in the APK can be extracted; the biometric code as written and the FragmentActivity requirement; the pinning trade-offs; the Room database being unencrypted by default; the backup file paths; the checklist table; every import and the code as written.Not verified: the secrets plugin's setup; a full pin example; the replacement for
EncryptedSharedPreferences (the docs point to platform APIs and the Keystore, but no replacement code was seen); the current AGP DSL for your version; every listing and solution (unrun); the exercises (not run by me). Re-check the live docs before you ship: security advice moves.Sources: Enable app optimization with R8 (developer.android.com), About keep rules (developer.android.com), Security checklist (developer.android.com), EncryptedSharedPreferences API reference (developer.android.com), Android Keystore system (developer.android.com), Show a biometric authentication dialog (developer.android.com), Network security configuration (developer.android.com), Security recommendations for backups (developer.android.com).