Flutter - Check If Device is Rooted or Jailbroken

This tutorial shows you how to check whether an Android device is rooted and whether an iOS device is jailbroken.

Some Android and iOS users deliberately make their devices 'more open' by rooting (on Android) or jailbreaking (on iOS) their devices. While they can do more things, it may expose the devices to more security threats. As a result, some applications that have a high concern about security such as mobile banking apps need to prevent users from running the application on a rooted or jailbroken device. If your application is developed using Flutter, it's possible to to check whether the device that runs the application is rooted or jailbroken.

Using safe_device Package

The safe_device package provides the functionality to check whether an Android device is rooted or an iOS device is jailbroken. To use the package, you need to add it to the pubspec.yaml file.

  dependencies:
    safe_device: ^1.1.2

Then, run flutter pub get to install the package.

To use the package, add the following import

  import 'package:safe_device/safe_device.dart';

It has a static getter isJailBroken which returns true if the device is rooted or jailbroken.

  final bool isRootedOrJailBroken = await SafeDevice.isJailBroken;

Below is an example which also checks the platform where the application runs.

  if (Platform.isAndroid) {
    setState(() {
      _result = isJailBroken ? 'Rooted' : 'Not rooted';
    });
  } else if (Platform.isIOS) {
    setState(() {
      _result = isJailBroken ? 'Jailbroken' : 'Not jailbroken';
    });
  } else {
    setState(() {
      _result = '-';
    });
  }

You need to be aware that an emulator is usually rooted. Therefore, you may also need to check whether the Flutter application runs on an emulator.

Full Code

  import 'dart:io';
  
  import 'package:flutter/material.dart';
  import 'package:safe_device/safe_device.dart';
  
  void main() => runApp(const MyApp());
  
  class MyApp extends StatelessWidget {
  
    const MyApp({Key? key}) : super(key: key);
  
    @override
    Widget build(BuildContext context) {
      return const MaterialApp(
        title: 'Woolha.com Flutter Tutorial',
        home: CheckIsRootedOrJailBroken(),
      );
    }
  }
  
  class CheckIsRootedOrJailBroken extends StatefulWidget {
  
    const CheckIsRootedOrJailBroken({Key? key}) : super(key: key);
  
    @override
    State<StatefulWidget> createState() {
      return _CheckIsRootedOrJailBrokenState();
    }
  }
  
  class _CheckIsRootedOrJailBrokenState extends State<CheckIsRootedOrJailBroken> {
  
    String? _result;
  
    @override
    void initState() {
      super.initState();
      _checkIsRootedOrJailBroken();
    }
  
    void _checkIsRootedOrJailBroken() async {
      final bool isJailBroken = await SafeDevice.isJailBroken;
  
      if (Platform.isAndroid) {
        setState(() {
          _result = isJailBroken ? 'Rooted' : 'Not rooted';
        });
      } else if (Platform.isIOS) {
        setState(() {
          _result = isJailBroken ? 'Jailbroken' : 'Not jailbroken';
        });
      } else {
        setState(() {
          _result = '-';
        });
      }
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: const Text('Woolha.com Flutter Tutorial'),
          backgroundColor: Colors.teal,
        ),
        body: SizedBox(
          width: double.infinity,
          child: Center(
            child: Text(_result ?? '-'),
          ),
        ),
      );
    }
  }

Summary

Checking whether the device is rooted or jailbroken using Flutter can be done with the help of safe_device package. Just install the package and access the SafeDevice.isJailBroken getter.

You can also read about: