version 2.4
IntroductionWe 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.
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!
Version history
2.4
- Correctly detecting if the device is rooted
- Stream optimizations and various bugfixes
- Complete refactoring and package renamed to SuperUser
- Added: InstallApk and UninstallApk methods. Can be used to silently install/uninstall apks
- 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.
- Initial version
Last edited: