kev
kev

Reputation: 1190

How to grant root access without SuperSU

I am currently able to get root access in my app on a rooted device using SuperSU. When my app requests root access a dialog from SuperSu is displayed which a user then clicks on "Grant" to allow the permissions.

I want to be able to bypass this step and let my app get root permission programmaticaly without having to click on the "Grant" button on the SuperSu dialog.

How do I go about this?

Upvotes: 2

Views: 5652

Answers (1)

Jared Rummler
Jared Rummler

Reputation: 38121

I want to be able to bypass this step and let my app get root permission programmatically without having to click on the "Grant" button on the SuperSu dialog.

You can't. The user must grant your app root access. Unless you build your own SU management app, you can't get around this initial permission request.

I'm providing a device with the app, then users use the device (which I own). Just to provide more context, when I release the device it already has the app and it has already been granted root access by SuperSu. So what I'm trying to avoid is a case whereby the app might loose the permission and then ask for it again while its out there in the field. The users are not tech savvy and they will not know what the SuperSu dialog is or what to do with it.

If you own the device then you can change the "Default access" to "Grant" in SuperSu settings. You can also change this on a per-app basis.

You mentioned in the comments that your app will already have superuser permissions. You have root access, so you could change SuperSu's shared preferences to have your app always be granted access. Again, you implied that you own the device so you shouldn't need to modify SuperSu preferences programmatically.

If you really needed to modify SuperSu's preferences then the following code should change the app's access settings to always allow and disable notifications (Using android-shell):

@WorkerThread
public static boolean tryChangingSuperSuDefaultAccess(Context context) throws Exception {
  String packageName = context.getPackageName();
  PackageManager pm = context.getPackageManager();

  // Get the preferences for SuperSu
  Context packageContext = context.createPackageContext("eu.chainfire.supersu", 0);
  SharedPreferences superSuPrefs = PreferenceManager.getDefaultSharedPreferences(packageContext);
  File superSuPrefsFile = getSharedPreferencesFile(superSuPrefs);

  // Copy SuperSu preferences to our app's shared_prefs directory
  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
  File directory = getSharedPreferencesFile(preferences).getParentFile();
  File destination = new File(directory, "eu.chainfire.supersu.xml");
  int uid = pm.getApplicationInfo(context.getPackageName(), 0).uid;
  destination.getParentFile().mkdirs();
  Shell.SU.run("cp \"" + superSuPrefsFile + "\" \"" + destination + "\"");
  Shell.SU.run("chmod 0660 \"" + destination + "\"");
  Shell.SU.run("chown " + uid + " " + uid + " \"" + destination + "\"");

  // Now we can edit the shared preferences
  superSuPrefs = context.getSharedPreferences("eu.chainfire.supersu", Context.MODE_PRIVATE);

  SharedPreferences.Editor editor = superSuPrefs.edit();
  editor.putString(String.format("config_%s_notify", packageName), "no"); // disable SuperSu notifications
  editor.putString(String.format("config_%s_access", packageName), "grant"); // Set access to grant for this app
  // noinspection all
  editor.commit();

  // Copy the edited shared preferences back
  return Shell.SU.run("cp \"" + destination + "\" \"" + superSuPrefsFile + "\"").isSuccessful();
}

private static File getSharedPreferencesFile(SharedPreferences preferences)
    throws NoSuchFieldException, IllegalAccessException {
  Field field = preferences.getClass().getDeclaredField("mFile");
  if (!field.isAccessible()) field.setAccessible(true);
  return (File) field.get(preferences);
}

I would not recommend using the code above. You should be transparent and educate your users. If you own the device, as you claimed, you can change these settings yourself using the app.

Upvotes: 10

Related Questions