How AndroidTweaker 3 is structured
Rebuilding an Android tuning app around a pure Kotlin domain module, one gate for privileged work, and a shell engine you can read top to bottom.
On this page
Why version 3 was a rewrite
AndroidTweaker started in 2024 as a root module: shell scripts that set CPU governors and kernel parameters at boot. Over two years an app grew around them.
By version 2 the app was hard to change. Screens read SharedPreferences directly, battery maths sat next to Compose code, and the only way to test a policy was on a phone. Version 3 rebuilt the app layer. The scripts stayed, and they are still the only code that writes kernel nodes. That decision shaped the rest.
Decisions don't need Android
Two Gradle modules:
:app Compose UI, Hilt, DataStore, Room, services, shells
│ depends on
:domain plain Kotlin on the JVM: models, maths, policies, script builders
:domain is a kotlin.jvm module, so the compiler refuses Android imports. Everything that can be decided without a device lives there: battery health and drain estimates, alert and charging-session policies, the report schedule, each profile's config file, the typed commands sent to the shell engine, telemetry parsing, and the migration from version 2's JSON formats.
Domain tests run on a plain JVM in milliseconds. The rules for when to warn about a hot battery, or which release to offer as an update, get tested on every change instead of on my phone.
One path to a root shell
A tuning app can break a phone, so there is exactly one path from a button to a privileged shell:
UI action
→ TuningController busy gate, access check, primary-user check
→ TweakEngine mutex, writes features.conf and <profile>.conf
→ Shell RootShell or ShizukuShell
→ dispatcher.sh the bundled engine
→ EngineResult → event → snackbar or dialog
Screens never touch a shell, a data store or the engine. They render an immutable UiState and call an actions interface the ViewModel implements. TuningController refuses a second change while one is running. TweakEngine serialises every run through a coroutine Mutex, so a boot reapply and a user's tap can't interleave:
suspend fun run(shell: Shell, command: EngineCommand, fromBoot: Boolean = false) =
withContext(io) {
operations.withLock { execute(shell, command, fromBoot) }
}
Inside execute, a boot reapply is refused if the user turned it off or changed profiles since, and a root-only command is refused on a Shizuku shell before anything is written.
Three tiers of access
Not every user has root, and the app has to be useful without it. ShellAccess alone decides which backend is available and publishes it as an AccessMode:
| Access | What works |
|---|---|
| None | Battery monitor, health estimates, alerts, history, widget, hardware monitor |
| Shizuku | ADB-level commands: doze, network, display, per-app game modes |
| Root | Kernel profiles, charge limits, charging bypass, thermal control |
With Shizuku, the engine scripts are staged into a temporary directory the shell user can read. Feature switches are filtered against the live access level before saving, then pushed to the engine with a 350 ms debounce: dragging through five options produces one shell run.
A shell engine you can read
The engine is POSIX shell, bundled as assets and extracted into the app's private directory with a version stamp. One file per profile. Battery, Balanced and Performance deliberately repeat their commands, so a mode reads top to bottom without chasing a value table through other files:
if want cpu; then
for _cpu in "$_S"/sys/devices/system/cpu/cpu*/cpufreq/; do
case "$(cat "${_cpu}scaling_available_governors")" in
*schedutil*)
write "${_cpu}scaling_governor" schedutil
write "${_cpu}schedutil/up_rate_limit_us" "${CPU_SU_UP-8000}"
;;
esac
done
fi
Two details keep this safe on hardware I've never held. ${CPU_SU_UP-8000} takes the user's saved override if there is one and the profile default otherwise. And every write goes through one helper that skips missing nodes, skips values already set, logs the change and supports a dry run:
write() {
[ -f "$1" ] || { log "skip: $1 missing"; return 1; }
_curval=$(cat "$1" 2>/dev/null)
[ "$_curval" = "$2" ] && return 1
[ "${DRY_RUN:-0}" = "1" ] && { log "dry-run: $1 $_curval -> $2"; return 0; }
echo "$2" >"$1" && log "write: $1 $_curval -> $2"
}
Shell is hard to unit test, so the suite records what each profile writes against simulated kernel nodes and compares every run with the recorded traces. When I rewrote the profiles to read top to bottom, the traces showed the rewrite issued exactly the same writes as before.
State flows one way
Repositories expose Kotlin Flows, and each ViewModel combines them into one UiState with stateIn(WhileSubscribed(5_000)). Live readings such as battery current and per-core frequency are cold flows with a polling loop inside, so they stop when the screen leaves composition. No lifecycle observers to forget.
The background monitor is event driven. Battery and screen broadcasts wake it, and each tick runs the same pipeline: read, record, evaluate alerts, update the notification and widget, apply charging and screen-off automation.
Updates without a store
AndroidTweaker ships through GitHub releases, so it updates itself. Once a day it asks GitHub's API for the newest stable release, and UpdatePolicy in :domain decides whether it's newer than the installed one. If the user accepts, the app downloads the APK, checks its SHA-256 against the published checksum, checks it is signed with the installed copy's certificate, and hands it to PackageInstaller. Pre-releases are never offered.
What I'd do differently
- Set the boundary first. Moving code into
:domainwas easy once the rule was "no Android imports". Deciding class by class would have taken longer than the rewrite. - Keep the dangerous part boring. The shell engine changed least, and version 2's preferences and history migrate in one pass, so upgrading reset nobody's setup.
- Undo is a requirement. Most fixes in 3.0.3 were about switching things off: a resolution that stayed changed, an app left on the doze whitelist, forced doze not released. Every switch is reversible now. It should have been from the start.