是否可以从 adb 启用 android 多用户?

Is it possible to enable android multi-user from adb?

official document 中所述,可以在构建时启用 android 多用户功能,但没有关于在现有 android 图像上启用它的文档。考虑到 root 访问可用,有没有办法在现有设备上启用多用户(例如使用 adb)?

将 属性 fw.max_users 设置为您想要的值可以启用 multi-user 支持。类似于 setprop fw.max_users $MAX_USERS。实际上 android 本身会读取此 属性 以了解设备是否支持 multi-user。可以参考UserManger源码了解android如何读取最大用户数。以下片段是从 UserManager 源复制而来的:

     /**
     * Returns the maximum number of users that can be created on this device.
 A return value of 1 means that it is a single user device.
     * @hide
     * @return a value greater than or equal to 1
     */

    @UnsupportedAppUsage
    public static int getMaxSupportedUsers() {
        // Don't allow multiple users on certain builds
        if (android.os.Build.ID.startsWith("JVP")) return 1;
        return SystemProperties.getInt("fw.max_users",
                Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
    }

可以在 root 设备上编辑 /system/build.prop 并添加 multi-user 支持,然后重新启动:

fw.max_users=3
fw.show_multiuserui=1