Short answer: you cannot make an Android app tamper-proof, but you can make attacks noisier and more expensive. The practical approach is to combine several independent checks for root, emulators and hooking frameworks like Frida and Xposed, treat them as risk signals instead of hard stops, and verify the result on your server.
This guide walks through the common detection techniques in Kotlin, what each one catches, and where it fails.
I'm Padmakar Garg, a Senior Android Developer. I work on Android security (EDR and RASP-style protections) and real-time messaging apps. Everything below is defensive: the goal is to protect your own app and your users.
Why bother detecting root and hooking?
A rooted device or a hooked process lets an attacker read app memory, bypass authentication, intercept encrypted traffic, or modify app behaviour at runtime. Banking, messaging, payments and enterprise apps all have a reason to notice when that is happening.
Detection will not stop a determined attacker, but it:
- raises the cost of casual attacks and automated tooling,
- gives your backend a signal to restrict risky sessions,
- helps you measure how often your app runs in hostile environments.
1. Detecting a rooted device
No single root check is reliable. Use several and combine them.
import android.os.Build import java.io.File object RootChecks { private val suPaths = listOf( "/system/bin/su", "/system/xbin/su", "/sbin/su", "/system/sd/xbin/su", "/data/local/xbin/su", "/data/local/bin/su" ) fun hasSuBinary(): Boolean = suPaths.any { File(it).exists() } fun hasTestKeys(): Boolean = Build.TAGS?.contains("test-keys") == true } You can also look for known root-manager packages such as Magisk. On Android 11 and later you must declare them in your manifest with <queries> or the package manager will not report them.
What it catches: basic, unhidden root.
Where it fails: Magisk with Zygisk and DenyList can hide su, the manager app and its mount points. File and package checks alone are easy to bypass.
2. Detecting an emulator
fun isProbablyEmulator(): Boolean = Build.FINGERPRINT.startsWith("generic") || Build.HARDWARE.contains("goldfish") || Build.HARDWARE.contains("ranchu") || Build.MODEL.contains("Emulator") || Build.MODEL.contains("Android SDK built for") Emulators are useful for automation and reverse engineering, so many security-sensitive apps flag them. Treat this as a signal rather than proof, because Build properties can be spoofed.
3. Detecting Frida
Frida is a dynamic instrumentation toolkit. Because it runs inside your process, the best detections look for its traces in that process.
import java.io.File import java.net.InetSocketAddress import java.net.Socket object FridaChecks { // Look for Frida's libraries or JS runtime in the memory map fun fridaInMemoryMap(): Boolean = runCatching { File("/proc/self/maps").useLines { lines -> lines.any { it.contains("frida", ignoreCase = true) || it.contains("gum-js-loop", ignoreCase = true) } } }.getOrDefault(false) // frida-server listens on 27042 by default. Run this off the main thread. fun fridaPortOpen(): Boolean = runCatching { Socket().use { it.connect(InetSocketAddress("127.0.0.1", 27042), 200) } true }.getOrDefault(false) } Always call the port check from a background dispatcher, never the main thread.
What it catches: default Frida setups.
Where it fails: renamed binaries, custom ports and patched builds. Attackers who know your checks can bypass them, so do not rely on one signature.
4. Detecting Xposed
fun xposedPresent(): Boolean { val classPresent = runCatching { Class.forName("de.robv.android.xposed.XposedBridge") true }.getOrDefault(false) val inStackTrace = Throwable().stackTrace.any { it.className.contains("de.robv.android.xposed") } return classPresent || inStackTrace } Modern forks such as LSPosed try to hide these traces, so this catches only unhidden or older setups.
5. Move sensitive checks to native code
Kotlin and Java checks are easy to find and patch in decompiled code. Moving key checks to C/C++ through the NDK and JNI makes them harder to locate and hook, because the attacker now has to work on native binaries instead of smali. It is not magic, but it raises the effort.
6. Combine signals into a risk score
The mistake I see most often is a single if (isRooted) finish(). That is trivial to bypass and breaks legitimate users. A better design:
- Run several independent checks.
- Give each one a weight and add them into a risk score.
- Send the score and the individual signals to your backend.
- Let the server decide: allow, step-up authentication, limit features or block.
data class DeviceRisk(val signals: Set<String>) { val score: Int get() = signals.size } fun collectRisk(): DeviceRisk { val s = mutableSetOf<String>() if (RootChecks.hasSuBinary()) s += "su_binary" if (RootChecks.hasTestKeys()) s += "test_keys" if (isProbablyEmulator()) s += "emulator" if (FridaChecks.fridaInMemoryMap()) s += "frida_maps" if (xposedPresent()) s += "xposed" return DeviceRisk(s) } 7. Use server-side attestation
Client-side detection runs on a device the attacker controls, so its answer can be faked. Pair it with Google Play Integrity API, which gives your server a signed verdict about device and app integrity that is much harder to forge. Always verify the verdict on your backend, never inside the app.
Common mistakes
- Relying on one check or one signature.
- Blocking users instantly instead of scoring and deciding on the server.
- Running network or file checks on the main thread.
- Logging detection results in plain text, which tells attackers exactly what you check.
- Assuming detection equals protection. It is one layer among many.
Summary
- Combine root, emulator, Frida and Xposed checks instead of trusting one.
- Treat each result as a signal, not a verdict.
- Move critical checks into native code where possible.
- Verify with Play Integrity on your server.
- Accept that determined attackers can bypass client checks, and design the rest of your security (encryption, server validation, rate limits) so that a bypass alone does not break you.
If you want to see a hardening-focused open-source project, check my GitHub: github.com/gargpadmakar.
About the author: Padmakar Garg is a Senior Android Developer with 8+ years of experience in Kotlin, Jetpack Compose and mobile security. Portfolio and blog: gargpadmakar.github.io. Connect on LinkedIn.