Probably for the most of situations your application will not require root access to the system. But, who knows why, sometimes you maybe need it. When developing my new application someone asked for a special feature: save the WiFi password which you are connected to. Well, you can get some of the wireless SSID infomation by calling the correct API functions, but not the password. The password is, so far, storaged into the wpa_supplicant.conf file and to have access to it you will need root privileges. Of course someone can simple type in the password and it’s done.
So, for this case, more than run away from the solution I was curious in how could I get root access from my application. After dinging a while I got a solution by watching a Youtube video from “Marijsoft sviluppo software”, that he was kind for provide new links for the source code, that I tested on XE8 and DX Seattle and worked as expected.
By reading the files (Androidapi.JNI.Runtime, Androidapi.JNI.Stream2 and FMX.Android.Runtime) we will discover that source belongs to 天轶_黄金 🙂
I needed to do a little fix on it (?) so I was able to make it work as I expected. I guess someone else can really improve this code, but for my purpose it is enough now. You can download the required files here (fixed by me), and the original one here (while it exists there). Basically you will find 2 functions:
- RunAndroidCmd(string, boolean, TStrings)
The first will invoke an internal attempt to run “su” and returning true if success. The second one you could use to reboot the device, if you want, example: RunAndroidCmd(‘su’ + sLineBreak + ‘reboot’, true). This command function will allow you run any Android linux compatible commands, such chmod, rm, kill applications etc., so you can use it to make a party on your device, so be careful.
And so, when I need to read the Wifi password I should check first for root (HaveRoot()) and my SuperSU or equivalent will ask permission for my application gain root access. Once allowed by SuperSu, my application can have root access. Here I made a simple program to read the wpa_supplicant.conf (Memo1.Lines.ReadFromFile(<fullfilename> was enough) and voilà!
At this point you application can handle the device with root privileges and now can manipulate whatever you want. Does I told you to be careful already?