【问题标题】:Bypass android usb host permission confirmation dialog on Android 9在 Android 9 上绕过 android usb 主机权限确认对话框
【发布时间】:2019-12-14 20:44:14
【问题描述】:

在 Android 9 之前,我可以 bypass android usb host permission confirmation dialog 使用 root,系统化我的应用程序并使用下一个 USB 类 https://stackoverflow.com/a/15378118/7767664 https://stackoverflow.com/a/19681849/7767664

但它不适用于最新的 Android 版本 - 9

它抛出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
}

有没有办法让它在 Android 9 上运行?

【问题讨论】:

  • 您的“IUsbManager”似乎无法在 Android 9 上使用。可能“grantDevicePermission()”方法已被删除,或者它只是在较新的 Android 版本上具有不同的签名。

标签: android usb root android-usb


【解决方案1】:

我们可以通过输入来解决它:

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

对非 SDK 接口的限制 (Android 9):https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces

然后grantDevicePermission 方法将通过反射在 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;
    }
}

附言您当然需要 root 并系统化您的应用程序(移至 /system/priv-app/

【讨论】:

  • @HoseinHaqiqian 是的,应该是
  • 抱歉 root 或签名的 systeam 应用?还是只是root而不是未签名的系统应用程序?这适用于 android 10?
  • @Fortran 可以调试签名应用,没关系
  • @听起来不错,未签名的系统 apk 运行良好,我正确理解吗?
猜你喜欢
  • 2012-11-23
  • 2020-10-18
  • 1970-01-01
  • 2018-08-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-06
  • 2023-02-10
  • 2014-08-20
相关资源
最近更新 更多