Bypass android usb host permission confirmation dialog on Android 9
Asked Answered
F

1

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://mcmap.net/q/339172/-bypass-android-usb-host-permission-confirmation-dialog https://mcmap.net/q/339172/-bypass-android-usb-host-permission-confirmation-dialog

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?

Flail answered 7/8, 2019 at 8:56 Comment(1)
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.Angelia
F
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/)

Flail answered 23/8, 2019 at 8:40 Comment(18)
@HoseinHaqiqian yes, should beFlail
Sorry root or signed systeam app? or just root and not signed system app? This work on android 10?Brunell
@Brunell it can be debug signed app, doesn't matterFlail
@sounds good, unsigned system apk work well, i correct understand?Brunell
Confused.so the apk must be moved to /system/priv-app/ ?Catapult
@Yeung, yes, what confuse you?...Flail
To make it work. The apk must place on /system/priv-app/?Catapult
@Catapult yes, it's written in the answer...Flail
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) @FlailShellashellac
@Shellashellac did you check developer.android.com/distribute/best-practices/develop/…Flail
Yes, I have checked that part. I am working on android 11 which is generating this error. @Flail Did anyone checked in the android 11 or higher version?Shellashellac
@Shellashellac so did you just check or you did allow reflection as well (using ADB)?Flail
@Flail 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).Shellashellac
@Shellashellac just allow reflection using ADBFlail
@Flail 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?Shellashellac
@Shellashellac the commands are in my answer, you need a rooted device for thatFlail
@Flail 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 ?Amends
@MarkWalter the device has to be rooted and the app has to by systemizedFlail

© 2022 - 2024 — McMap. All rights reserved.