B4A Library SuperUser: Acquiring root permissions, the easy way.

version 2.4
Introduction

We all have seen various apps asking for superuser permissions to do weird things with our devices and many times we wondered how they do it. Well, with SupeUser, accessing a rooted device is as simple as a function call!

Latest package (library files and samples)
https://www.dropbox.com/s/f8gy3levkrtknt8/superuser.zip

Precompiled samples (APK)
Tutorial, KitKat ScreenRecorder with Audio
The ScreenRecorder does not work in the emulator. Use a real device instead.



upload_2014-7-28_22-17-41.png



Getting started
  • Extract the contents of the package anywhere in your hard disk.
  • Copy the superuser.jar and superuser.xml to your B4A libraries folder
  • Open the tutorial project located in the samples folder

The core of the library is the SuShell class. There are 2 ways using it. The simple and the advanced way. Let's take a look at the tutorial project.

The simple way (SuShellSimple sample)

First of all, we add the appropriate permission in our manifest file
B4X:
AddPermission("android.permission.ACCESS_SUPERUSER")

Whenever you need to do something with root permissions, you can just execute a superuser command and wait for the result (without blocking) in a single line of code. Keep in mind that each time you call the SuShell.Execute method, root permissions will be requested.
B4X:
Dim Su As SuShell
Su.Execute("ls").WaitForCompletion

You can also check before executing the command if the device is rooted and proceed accordingly.
B4X:
Dim Su As SuShell
If Su.DeviceRooted Then
    If Su.ExecuteWithEvent("ls", "Su").WaitForCompletion Then
        Msgbox("Done!", "")
    Else
        Msgbox("Permissions error!", "")
    End If
Else
    Msgbox("Device not rooted!", "")
End If

There is also the possibility to receive events by using the SuShell.ExecuteWithEvent method. The raised events are: Start, Command, Stop
B4X:
Sub BtnExecute_Click
    Dim Su As SuShell
    Su.ExecuteWithEvent("ls", "Su").WaitForCompletion
End Sub

Sub Su_Start
    Log("Su: Start")
End Sub

Sub Su_Command(Command As String, Response() As String)
    Dim Lines As String
    For i=0 To Response.Length-1
        Lines = Lines & Response(i) & CRLF
    Next
    Msgbox(Lines.Trim, Command)
End Sub

Sub Su_Stop(Result As Boolean)
    Log("Su: Stop=" & Result)
End Sub


Some examples:
B4X:
'Reboot the device
Su.Execute("reboot")
Su.Execute("reboot recovery")
Su.Execute("reboot bootloader")

'Restart the GUI by executing 2 commands at once, causing a so called fast-reboot
Su.ExecuteMultiple(Array As String("stop", "start"))


The advanced way (SuShellAdvanced sample)

SuShell is driven by a multi-threaded core that allows you to have complete control of the spawned process. A typical example is this:

Declare an SuShell and an SuProcess object in Sub Process_Globals
B4X:
Sub Process_Globals
    Dim Su As SuShell
    Dim Process As SuProcess
End Sub

Acquire root permissions in Sub Activity_Resume and open a command pipe that can be used at any time without requesting permissions again.
B4X:
If Su.DeviceRooted Then
    Process = Su.Acquire("Su")
End If

If Process.Acquired Then
    Log("root access granted")
Else
    Log("root access denied")
End If

Whenever is needed, execute a superuser command using the SuProcess object
B4X:
If Process.Acquired Then
    Process.Execute(EdCmd.Text)
Else
    Msgbox("Permissions error!", "")
End If

You can also use the methods of the SuProcess class to control it.


Additional Classes

The library currently contains few more classes that are utilizing the superuser core system.

1. SuApk

Can be used to silently install/uninstall apks. Once your app gets permanent root permissions, it will be able to silently install/uninstall any apk.

B4X:
Dim Apk As SuApk
Dim Result As Boolean = Apk.Install(File.DirAssets, "tutorial.apk")
B4X:
Dim Apk As SuApk
Dim Result As Boolean = Apk.Uninstall("com.datasteam.b4a.xtraviews.dialogview.tutorial")


2. SuBrowser

Can be used to offer a Root Explorer similar functionality.

Create an SuBrowser object in Sub Globals
B4X:
Sub Globals
    Dim Browser As SuBrowser
End Sub

In Activity_Resume initialize the object
B4X:
Sub Activity_Resume
    Browser.Initialize
End Sub

Call the SuBrowser.ListFolder to get the folder contents. Pass the desired folder name as a parameter. Use "" to get the root contents. The method returns a List object that contains SuBrowserFileInfo objects.
B4X:
Dim Contents as List = Browser.ListFolder(Folder)

Now you can traverse the Contents object.
B4X:
Contents.SortType("Type", True)
For Each FileInfo As SuBrowserFileInfo In Contents
    Dim SubTitle As String
    If FileInfo.IsFolder Then
        SubTitle = "folder"
    Else
        SubTitle = FileInfo.Size & " byte(s)"
    End If
    LvBrowser.AddTwoLines2(FileInfo.Name, SubTitle, FileInfo)
Next


3. SuRecorder

This class can be used to create a fully functional screen recorder with audio.
A separate tutorial is on its way


Please test it with your devices and post your feedback!

--

That's all for now folks! :D

Version history

2.4
  • Correctly detecting if the device is rooted
2.1

  • Stream optimizations and various bugfixes
2.0
  • Complete refactoring and package renamed to SuperUser
1.2
  • Added: InstallApk and UninstallApk methods. Can be used to silently install/uninstall apks
1.1
  • Added: KeepAlive property. If set to true, the first command will keep the connection with superuser process open without the need of asking for permissions again until the app process is killed or KeepAlive property is set back to false.
1.0
  • Initial version
 
Last edited:

Suntzu

Member
Licensed User
Longtime User
Hi, Periklis
When I run the tutorial on my 4.2.2 rooted tablet, after click on "advanced" button, the asking for superuser permissions window didn't appear, and it said "root access denied", but click on "SuBrowser", it asked for superuser permissions, and work normally.
please fix this issue.

I have the same issue as dune3000.

I think su.DeviceRooted function is not working. SuBrowserSample works without any issue but not SuShellSimpleSample and SuShellAdvancedSample.

Commenting out the su.DeviceRooted's if..end if clause in the SuShellSimpleSample works:

B4X:
Sub BtnExecute_Click
    Dim su As SuShell
    'If su.DeviceRooted Then
        Dim Process As SuProcess = su.ExecuteWithEvent("ls", "Su")
        If Process.WaitForCompletion Then
            Msgbox("Process.Response.Size = " & Process.Response.Length, "Done")
        Else
            Msgbox("Permissions error!", "")
        End If
    'Else
        'Msgbox("Device not rooted!", "")
    'End If
End Sub
 

Firpas

Active Member
Licensed User
Longtime User
I'm testing superuser lib 2.30 on my S4 (android 4.3) and it not work.

What i am doing wrong????



superuser1.png
superuser2.png
superuser3.png


Thanks in advance
 

MaFu

Well-Known Member
Licensed User
Longtime User
How to root depends on phone manufacturer and model. Ask Google with your phone name and "root" as search term.
But beware: the root process for your phone may be simple or complex. And if you doing wrong, you may have a new paperweight eventually.
 

JohnD

Active Member
Licensed User
Longtime User
@Periklis: Google reports that superuser.zip is Malicious. Chrome blocked the download. I'm sure this is not malware. I know how to unblock it. I'm just giving you a heads-up in case you didn't already know about it. Don't shoot the messenger. :)
 

Attachments

  • superuser.gif
    superuser.gif
    26.5 KB · Views: 246

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User

fanfalveto

Active Member
Licensed User
Longtime User
don´t install apk
B4X:
Sub BtnInstallApk_Click
    ProgressDialogShow2("please wait", False)
    Apk.Install(File.DirAssets, "culturilla.apk")
'    Dim Result As Boolean = Apk.Install(File.DirAssets, "culturilla.apk")
'    DisplayResult(Result)
End Sub
the phone is rooted (lg l5,samsung s4)
any idea why?
 

Htar Htar Lwin

Member
Licensed User
Longtime User
Hi, Periklis
When I run the tutorial on my 2.3.6 rooted phone, after click on "execute command" button, Permission error! message box appears. I already granted superuser permissions. Why?
 

Attachments

  • Root.png
    Root.png
    11.5 KB · Views: 227
Last edited:

ivan.tellez

Active Member
Licensed User
Longtime User
there is a Bug on the Recorder.Options.ShaketoStop

Lots of apps include haptics, most of them in form of vibration. the problem is when you try to record one of this apps, the recorder is way to sensitive and stops the recording when the app makes a vibration.

One aproach is to add a threshold value to adjust in the sake detection, or make id not as sensitive as it is now.
 

BarryW

Active Member
Licensed User
Longtime User
How to use this to access com.android.providers.telephony/databases/mmssms.db. Please Help
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hello,
I use your library to silent install an apk.

I receive this error log:


B4X:
java.lang.IllegalThreadStateException: Process has not yet terminated: 6999
    at java.lang.ProcessManager$ProcessImpl.exitValue(ProcessManager.java:275)
    at com.datasteam.b4a.system.superuser.Su$1.run(Su.java:192)
    at java.lang.Thread.run(Thread.java:838)
true


** Activity (main) Pause, UserClosed = true **

What does it means?
 
Last edited:

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
don´t install apk
B4X:
Sub BtnInstallApk_Click
    ProgressDialogShow2("please wait", False)
    Apk.Install(File.DirAssets, "culturilla.apk")
'    Dim Result As Boolean = Apk.Install(File.DirAssets, "culturilla.apk")
'    DisplayResult(Result)
End Sub
the phone is rooted (lg l5,samsung s4)
any idea why?

Hello,
have you solved this problem?
 
Top