4

Before Android 9 I could bypass android usb host permission confirmation dialog by using root, systemizing my app and using next usb classes https://stackoverflow.com/a/15378118/7767664 https://stackoverflow.com/a/19681849/7767664

But it doesn't work for newest Android version - 9

It throws java.lang.NoSuchMethodError: No interface method grantDevicePermission(Landroid/hardware/usb/UsbDevice;I)V in class Landroid/hardware/usb/IUsbManager; or its super classes (declaration of 'android.hardware.usb.IUsbManager' appears in /system/framework/framework.jar)

fun setUsbPermissionRoot(device: UsbDevice) : Boolean {
    if (BuildConfig.DEBUG) Log.i(TAG, "trying set permission")
    try {
        val pm = App.context.packageManager
        val ai = pm.getApplicationInfo(App.context.packageName, 0)
        ai?.let {
            val b = ServiceManager.getService(Context.USB_SERVICE)
            val service = IUsbManager.Stub.asInterface(b)
            service.grantDevicePermission(device, it.uid)
            try {
                service.setDevicePackage(device, App.context.packageName, it.uid)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            if (BuildConfig.DEBUG) Log.i(TAG, "permission was set usb device")
            return true
        }
    } catch (e: PackageManager.NameNotFoundException) {
        if (BuildConfig.DEBUG) e.printStackTrace()
    } catch (e: RemoteException) {
        if (BuildConfig.DEBUG) e.printStackTrace()
    }
    return false
}

Is there any way to make it work on Android 9?

user924
  • 8,146
  • 7
  • 57
  • 139
  • It seems your "IUsbManager" cannot be used on Android 9. Maybe the "grantDevicePermission()" method has been removed or it just has a different Signature on newer Android versions. – emandt Aug 07 '19 at 09:07

1 Answers1

5

We can solve it by entering:

adb shell
su
settings put global hidden_api_policy_pre_p_apps  1
settings put global hidden_api_policy_p_apps 1

Restrictions on non-SDK interfaces (Android 9): https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces

And then grantDevicePermission method will be available again through reflection on Android 9:

public static boolean grantAutomaticUsbPermissionRoot(Context context, UsbDevice usbDevice) {
    try {
        PackageManager pkgManager = context.getPackageManager();
        ApplicationInfo appInfo = pkgManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);

        Class serviceManagerClass = Class.forName("android.os.ServiceManager");
        Method getServiceMethod = serviceManagerClass.getDeclaredMethod("getService", String.class);
        getServiceMethod.setAccessible(true);
        android.os.IBinder binder = (android.os.IBinder)getServiceMethod.invoke(null, Context.USB_SERVICE);

        Class iUsbManagerClass = Class.forName("android.hardware.usb.IUsbManager");
        Class stubClass = Class.forName("android.hardware.usb.IUsbManager$Stub");
        Method asInterfaceMethod = stubClass.getDeclaredMethod("asInterface", android.os.IBinder.class);
        asInterfaceMethod.setAccessible(true);
        Object iUsbManager=asInterfaceMethod.invoke(null, binder);

        final Method grantDevicePermissionMethod = iUsbManagerClass.getDeclaredMethod("grantDevicePermission", UsbDevice.class, int.class);
        grantDevicePermissionMethod.setAccessible(true);
        grantDevicePermissionMethod.invoke(iUsbManager, usbDevice,appInfo.uid);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

p.s. you need root of course and systemize your app (move to /system/priv-app/)

user924
  • 8,146
  • 7
  • 57
  • 139
  • 1
    @HoseinHaqiqian yes, should be – user924 Jun 30 '20 at 10:18
  • Sorry root or signed systeam app? or just root and not signed system app? This work on android 10? – Fortran Jan 07 '21 at 20:13
  • @Fortran it can be debug signed app, doesn't matter – user924 Jan 07 '21 at 20:49
  • @sounds good, unsigned system apk work well, i correct understand? – Fortran Jan 07 '21 at 20:56
  • Confused.so the apk must be moved to /system/priv-app/ ? – Yeung Mar 28 '22 at 10:27
  • @Yeung, yes, what confuse you?... – user924 Mar 28 '22 at 11:12
  • To make it work. The apk must place on /system/priv-app/? – Yeung Apr 01 '22 at 10:14
  • @Yeung yes, it's written in the answer... – user924 Apr 01 '22 at 13:36
  • java.lang.NoSuchMethodError: No interface method grantDevicePermission(Landroid/hardware/usb/UsbDevice;I)V in class Landroid/hardware/usb/IUsbManager; or its super classes (declaration of 'android.hardware.usb.IUsbManager' appears in /system/framework/framework.jar!classes2.dex) @user924 – Nikhil Oct 17 '22 at 11:40
  • @Nikhil did you check https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces – user924 Oct 17 '22 at 12:50
  • Yes, I have checked that part. I am working on android 11 which is generating this error. @user924 Did anyone checked in the android 11 or higher version? – Nikhil Oct 17 '22 at 13:02
  • @Nikhil so did you just check or you did allow reflection as well (using ADB)? – user924 Oct 17 '22 at 17:01
  • @user924 I have checked for various code snippets but each of them is failing due to security reasons. As they are generating errors as mentioned above (I think the USB interface class of OS is restricted). – Nikhil Oct 18 '22 at 04:17
  • @Nikhil just allow reflection using ADB – user924 Oct 18 '22 at 07:09
  • @user924 Not able to understand what you are trying to explain. How to allow reflection using ADB? Like you are trying to do an ADB command for that? – Nikhil Oct 18 '22 at 07:11
  • @Nikhil the commands are in my answer, you need a rooted device for that – user924 Oct 19 '22 at 19:06
  • @user924 I'm getting: Error granting USB permissions: java.lang.SecurityException: Neither user xxx nor current process has android.permission.MANAGE_USB on the grantDevicePermission. I've added the MANAGE_USB permission in the manifest and the app is running as system app, what am i missing ? – Marco Ortali Nov 10 '22 at 21:57
  • @MarkWalter the device has to be rooted and the app has to by systemized – user924 Nov 11 '22 at 09:23