Created
September 23, 2020 17:12
-
-
Save eldetulado/33c40e5b764e6eee51bca0c5b804ba92 to your computer and use it in GitHub Desktop.
Root status checked - Android Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Build | |
import java.io.BufferedReader | |
import java.io.File | |
import java.io.InputStreamReader | |
class RootUtil { | |
val isDeviceRooted: Boolean | |
get() = checkRootMethod1() || checkRootMethod2() || checkRootMethod3() | |
private fun checkRootMethod1(): Boolean { | |
val buildTags = Build.TAGS | |
return buildTags != null && buildTags.contains("test-keys") | |
} | |
private fun checkRootMethod2(): Boolean { | |
val paths = arrayOf( | |
"/system/app/Superuser.apk", | |
"/sbin/su", | |
"/system/bin/su", | |
"/system/xbin/su", | |
"/data/local/xbin/su", | |
"/data/local/bin/su", | |
"/system/sd/xbin/su", | |
"/system/bin/failsafe/su", | |
"/data/local/su", | |
"/su/bin/su" | |
) | |
for (path in paths) { | |
if (File(path).exists()) return true | |
} | |
return false | |
} | |
private fun checkRootMethod3(): Boolean { | |
var process: Process? = null | |
return try { | |
process = Runtime.getRuntime().exec(arrayOf("/system/xbin/which", "su")) | |
val `in` = BufferedReader(InputStreamReader(process.inputStream)) | |
`in`.readLine() != null | |
} catch (t: Throwable) { | |
false | |
} finally { | |
process?.destroy() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment