Android 反黑产

474 阅读1分钟

如何判断手机是否 root

public static boolean isRooted(Context context) {
    if (TextUtils.equals("1", getSystemProperty("ro.secure", null))) {
        return true;
    }
    final String[] filePaths =
            new String[]{"/system/bin/su", "/system/sbin/su", "/system/xbin/su", "/vendor/bin/su", "/sbin/su"};
    for (String filePath : filePaths) {
        final File file = new File(filePath);
        if (file.exists()) {
            return true;
        }
    }
    return false;
}
 
@Nullable
public static String getSystemProperty(String name, String defaultValue) {
    if (TextUtils.isEmpty(name)) {
        return defaultValue;
    }
    try {
        final Class<?> cls = Class.forName("android.os.SystemProperties");
        final Method method = cls.getDeclaredMethod("get", String.class, String.class);
        return (String) method.invoke(null, name, defaultValue);
    } catch (Throwable th) {
        return defaultValue;
    }
}

这些文件夹下都可能存在 su 文件:


"/system/bin/", "/system/sbin/", "/system/xbin/", "/vendor/bin/", "/sbin/"

参考资料

mp.weixin.qq.com/s/sl33d2pny…

juejin.cn/post/684490…

juejin.cn/post/684490…