Sunday, 25 October 2015

Using WRITE_SECURE_SETTINGS permission on non system apps

I happen to frequently use an app that doesn't work if USB Debugging - ADB (Android Debug Bridge) is enabled. It is very cumbersome to go to settings and enable or disable the setting every time I need to use the app.
I checked if there are any widgets that could do this in a click and returned empty handed. So, I did what every developer would do - build it myself!

In order to change the adb settings, I had to use WRITE_SECURE_SETTINGS permission. But there was one major problem! This permission is not available for non system apps!!

The easiest solution to this was to move the apk to /system partition. Except that I didn't want to do this as I frequently keep flashing /system partition.

Another solution was to use pm and grant permission to the app like this:

adb pm grant <package name> android.permission.WRITE_SECURE_SETTINGS

This was great! However I didn't want users who were gonna use the app to go through all this pain. I had to figure out a way to do the same programmatically. So I did the next best thing! To run the command after opening a root shell in the code.

Here is how I did it.
This requires root. But that's ok.

No comments:

Post a Comment