Anbox

来自 Arch Linux 中文维基

这篇文章或章节的翻译不反映原文。

原因:部分内容可能由机器翻译。Translation in progress since page creation 2021-08-01, only a few paragraphs have been translated on 2022-04-18 but nothing since.(在 Talk:Anbox# 中讨论)
注意: Anbox 已经停止开发,开发者建议使用 Waydroid

Anbox 是一个可以在 GNU/Linux 发行版上运行 Android App 的一个 容器

准备工作[编辑 | 编辑源代码]

注意: Anbox 的 dkms 模块在内核版本 ≥5.7 下不会工作。请按照以下说明进行操作。 对于较老的内核请参阅 较老的内核[损坏的链接:无效的章节]

您需要运行带有 ashmem 和 binder 模块的内核。但是它们不是 Arch Linux 的默认内核 ( linux ) 的一部分,所以您需要安装一个包含这些模块的内核。

您可能还需要配置引导加载程序以使用不同的内核。

您有多种选择:

使用 Linux-Zen[编辑 | 编辑源代码]

linux-zen 内核自带 Anbox 需要的模块。这可能是最简单的方式,因为您不必编译内核并且版本会定期更新。

警告: 根据 linux 内核中的这个commit,从 5.18 开始的版本中 ashmem 被 memfd 取代。 然而,anbox 开发人员并没有修复他们的软件以使用 memfd,因此 anbox 很可能在一段时间内根本无法与 linux-zen 内核一起工作。

编译内核[编辑 | 编辑源代码]

当然,您也可以自己重新编译 linux 内核。请阅读 Kernel#编译 and 配置模块

您还可以从AUR里构建一个已经包含特定补丁的内核包, 请参阅 AUR search "linux+anbox".

配置模块[编辑 | 编辑源代码]

模块同样也可以被编译进内核里 (y), 模块里 (m), 或者不编译 (n).此外,并非配置中的所有组合都是可能的,某些选项将需要其他选项。

下面的的内核配置将把 ashmem 和 binder 编译为模块,而最后一个选项指定在 /dev/ 加载 binder 模块时在目录中创建三个设备。

CONFIG_ASHMEM=m 
CONFIG_ANDROID=y
CONFIG_ANDROID_BINDER_IPC=m
CONFIG_ANDROID_BINDERFS=n
CONFIG_ANDROID_BINDER_DEVICES="binder,hwbinder,vndbinder"

如果您要从 AUR 构建内核,那么您可以参考下面的步骤:

  1. 运行 makepkg --nobuild, 它将下载源代码,验证并提取。最后运行 prepare() 函数.
  2. 编辑位于内核目录底部的 .config 文件。
  3. prepare() 函数的末尾可能是一个命令,它使用来自配置的信息重新生成 makefile,也可能是 make olddefconfig. 将它移动到 build() 函数内,或自己执行一遍。
  4. 运行 makepkg --noextract, 它将在 makepkg --nobuild 停止的地方继续。

使用 binderfs 进行配置[编辑 | 编辑源代码]

并不是每个人都对 Linux 中的 binder 模块感到满意。为了解决这些问题,binderfs诞生了。编译内核时必须在新旧两种方式之间进行选择。比如下面的选项就是针对于 binderfs 的。

内核源代码还提供了一个简单的脚本来设置配置选项。它不会进行依赖项检查。当您在 .config 文件所在的同一目录中时,执行以下命令:

scripts/config --module  CONFIG_ASHMEM
scripts/config --enable  CONFIG_ANDROID
scripts/config --enable  CONFIG_ANDROID_BINDER_IPC
scripts/config --enable  CONFIG_ANDROID_BINDERFS
scripts/config --set-str CONFIG_ANDROID_BINDER_DEVICES ""

当您正在从从 AUR 构建内核时,这个脚本很方便,因为只需要在 PKGBUILD 中的正确位置插入这些命令就足够了。

启动新内核[编辑 | 编辑源代码]

请参阅引导加载程序的 wiki 页面了解如何使用新内核引导。您应该在启动 Anbox 之前切换到新的内核。

安装[编辑 | 编辑源代码]

加载模块[编辑 | 编辑源代码]

Anbox 不会自动加载模块,但是如果您在使用 linux-zen,就可以跳过此步骤。

要临时加载,请使用:

# modprobe -a binder-linux ashmem-linux

要始终在启动时加载它们,可以通过 systemd-modules-load.service 文件来实现。 为此,请在 /etc/modules-load.d/ 创建一个包含以下内容的 anbox.conf文件:

/etc/modules-load.d/anbox.conf
ashmem_linux
binder_linux

挂载 binderfs[编辑 | 编辑源代码]

注意: linux-zen 内核必须挂载 binderfs。

如果您的内核正在使用 binderfs,则还有一个步骤要做:挂载 binderfs 文件。

首先,您需要一个挂载点。默认情况下,Anbox 将使用 /dev/binderfs。 您可以直接创建该目录,但它会在启动时被删除。所以这里建议使用 systemd-tmpfiles 实现在启动时创建 /dev/binderfs。为此,您需要在 /etc/tmpfiles.d/ 创建一个包含以下内容的文件:

/etc/tmpfiles.d/anbox.conf
d! /dev/binderfs 0755 root root

其次您需要挂载 binder 文件系统。 这可以通过:

# mount -t binder none /dev/binderfs

如果想要在启动时挂载它,只需要在 fstab 中添加下面这一行。

/etc/fstab
none                         /dev/binderfs binder   nofail  0      0
警告: 添加 nofail 参数后,当您启动没有 binderfs 支持的内核时,您将无法进入恢复 shell。

安装 Android 镜像[编辑 | 编辑源代码]

注意: 现在的镜像均已过时 (基于 Android 7.1). 目前,上游没有可用的更新的镜像。另请参阅 Talk:Anbox#Anbox-Images outdated.

选择一个镜像来安装:

提示:
  • 通常,在 x86_64 计算机上运行 ARM 应用程序需要 Houdini。
  • 如果您使用 GMS,OpenGApps 是一个不错的选择。

安装 Anbox[编辑 | 编辑源代码]

安装 anbox-gitAUR

然后, start/enable anbox-container-manager.service.

恭喜您现在已经完成了使用 Anbox 所需的所有步骤!在您的桌面环境菜单中,您应该会在 Others 类别中找到 anbox。 第一次启动将花费很长的时间,anbox session-manager将会被自动打开。当然,您也可以手动执行 anbox session-manager 。如果 anbox 崩溃并且您想报告或修复这个错误,这将非常有用。只需启动它,然后等到它崩溃。

还有一个给用户使用的systemd unit,可用于在开机时启动 session-manager;如果你想这么干,只需要 start/enable anbox-session-manager.service。该 unit 的一个优点是可以在崩溃时找到日志:

$ journalctl --user -b -u anbox-session-manager

但是请记住,如果您打开一个 Android app 时崩溃崩溃了,它将会再启动一个独立于systemed的 session-manager。

网络[编辑 | 编辑源代码]

使用 NetworkManager[编辑 | 编辑源代码]

如果您正在使用 NetworkManager ,则可以使用它来配置网络。

执行以下命令来创建 bridge connection:

$ nmcli con add type bridge ifname anbox0 -- connection.id anbox-net ipv4.method shared ipv4.addresses 192.168.250.1/24
  • ifname anbox0 指定网桥接口名称,在本例中为 anbox0. 不要改变这个名称,因为 Anbox 仅会在名为 anbox0 的网桥接口。
  • connection.id anbox-net 当其运行于 NetworkManager时使用 anbox-net 这个名称. 你可随愿更改.
  • ipv4.method shared 指示 NetworkManager 来创建一个NAT网络,并根据系统的路由规则对出站数据包进行路由。为此,在 dnsmasq 包完备. dnsmasq 不需要配置,也不需要作为systemd服务启动, 其将作为NetworkManager的后端使用 — 如果它不可用,这一步将无提示地失败。. 如果你想让Anbox直接连接一个特定的网络,你也可以让这一个参数和ipv4.addresses参数留空,参见 Network bridge#With NetworkManager。如果你想这么做,你还必须把在anbox-container-manager.service的容器网络配置修改掉,详见下一个要点。
  • ipv4.addresses 192.168.250.1/24 specifies the default gateway and subnet of the NAT network. If you wish to change this (e.g. to 192.168.42.1/24) you must also indicate the new subnet to anbox in the anbox-container-manager.service using: --container-network-address=192.168.42.2/24 --container-network-gateway=192.168.42.1

NetworkManager 将在每次重新启动时自动设置桥接,因此您只需要执行一次命令。

Via systemd-networkd[编辑 | 编辑源代码]

The package anbox-gitAUR provides configuration files for systemd-networkd in /usr/lib/systemd/network/ to enable networking in anbox.

Therefore, you can start/enable systemd-networkd before starting anbox-container-manager.service.

Via anbox-bridge script[编辑 | 编辑源代码]

Alternatively you can use the anbox-bridge script used by the project.

You must execute anbox-bridge every time before starting anbox-container-manager.service in order to get network working in Anbox. The easiest solution for that is to create a drop-in file for the service.

/etc/systemd/system/anbox-container-manager.service.d/enable-anbox-bridge.conf
[Service]
ExecStartPre=/usr/bin/anbox-bridge start
ExecStopPost=/usr/bin/anbox-bridge stop

Usage[编辑 | 编辑源代码]

You can run the Android applications on your desktop's launcher on Other category.

If you want to use adb to debug, install android-tools. The anbox session-manager must already be running when launching it.

$ adb shell

安装应用[编辑 | 编辑源代码]

除非你用 Houdini 挑选了一个镜像, 否则 Anbox 不支持 ARM 应用. 所以应用必须是 x86_64 架构.

通过 adb[编辑 | 编辑源代码]

安装 /path/to/app.apk

$ adb install /path/to/app.apk

获取已经安装的应用列表

$ adb shell pm list packages

注意:输出会与 package:app.name 相似, 此 app.name 会与Anbox容器内显示的不同。

卸载 app.name

$ adb uninstall app.name

如果 app.name 是一个系统应用

$ adb uninstall --user 0 app.name

通过应用商店[编辑 | 编辑源代码]

应用商店可以轻松安装应用,包括 anbox-image-gappsAUR PlayStore商店.

Sensor data[编辑 | 编辑源代码]

Via dbus different sensors can be set. Documentation on that can be found at dbus.md.

Temperature data[编辑 | 编辑源代码]

That is the example from the author (PRs #1522 & #1540):

$ dbus-send --session --dest=org.anbox --print-reply /org/anbox org.freedesktop.DBus.Properties.Set string:org.anbox.Sensors string:Temperature variant:double:25.1

GPS data[编辑 | 编辑源代码]

(introduced by PR #1606)

GPS sensor data can also be manipulated.

If your PC has a WWAN card, you can use gpsd and the code from the PR to feed Anbox with GPS data. You do not need to have a SIM-Card for GPS.

Otherwise, you can also look at the PR to learn how to feed it fake data with the help of [1].

Root shell[编辑 | 编辑源代码]

With this script from the Anbox project one can get a root shell inside the Android container.

It is not part of the anbox-gitAUR package, and it also does not use adb.

Tips and tricks[编辑 | 编辑源代码]

Android developer options[编辑 | 编辑源代码]

Some extra steps need to be done besides unlocking them the same way you do on an android phone. When installing the android image[损坏的链接:无效的章节], some modifications to products/anbox.xml are required:

  • <unavailable-feature name="android.hardware.usb.host" /> is the reason why they are not available.
  • <feature name="android.software.backup" /> will be needed too, to avoid a NullPointerException.

(reference: Github issue #444)

Getting debugging information[编辑 | 编辑源代码]

Obviously, it is helpful to have debugging symbols in the Anbox build. For that, when compiling Anbox[损坏的链接:无效的章节], add options=('!strip') to the PKGBUILD, as by default they are removed. And, use either -DCMAKE_BUILD_TYPE=RelWithDebInfo or -DCMAKE_BUILD_TYPE=Debug in the cmake call.

But there is more to it! Anbox uses backward-cpp. If you do not delete the build files for Anbox, it will print pretty stack traces when crashing, which point out the places in the source code.

Also see the remarks in Install Anbox[损坏的链接:无效的章节].

Troubleshooting[编辑 | 编辑源代码]

If you run into issues, take a look at the official Issue Tracker: [2]

Old CPUs[编辑 | 编辑源代码]

Anbox requires support for SSE 4.1/4.2 and SSSE 3, because Android wants that too. Some older CPUs do not provide that, so you probably cannot use Anbox, see: Anbox Github Issue 499.

Old kernels[编辑 | 编辑源代码]

Before the kernel modules were mainlined, they were out of tree modules, which needed to be installed seperately from the kernel (Like it is the case for the nvidia kernel modules). They are not compatible with kernel 5.7 or newer.

It is still possible to use that approach with the linux-lts or other old kernels. The package to install the modules via DKMS is not available in the AUR anymore, but can be retrieved with git clone https://aur.archlinux.org/anbox-modules-dkms, or resurected from [3].

Secure Boot error[编辑 | 编辑源代码]

If you get this error message:

modprobe: ERROR: could not insert 'ashmem_linux': Operation not permitted

Secure Boot is likely blocking the module. You can either disable Secure Boot or sign the ashmem module yourself.

More info can be found in the Anbox Github Docs.

See also[编辑 | 编辑源代码]