Anbox (简体中文)

From ArchWiki
Jump to navigation Jump to search

Tango-preferences-desktop-locale-modified.png这篇文章或章节的翻译不反映原文。Tango-preferences-desktop-locale-modified.png

原因:部分内容可能由机器翻译。(在 Talk:Anbox (简体中文)# 中讨论)
翻译状态:本文是 Anbox翻译。上次翻译日期:2021-08-02。如果英文版本有所更改,则您可以帮助同步翻译。

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

准备工作

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

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

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

您有多种选择:

使用 Linux-Zen

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

编译内核

当然,您也可以自己重新编译 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 specifies the name of the connection to be anbox-net when it appears in NetworkManager. You can change this if you wish.
  • ipv4.method shared instructs NetworkManager to create a NAT network and route outgoing packets according to the system routing rules. For that, the dnsmasq package is required. dnsmasq does not needs to be configured or be started as systemd service, it will be used behind the scenes by NetworkManager — if it is not available, this step will fail silently. You can leave this and the ipv4.addresses parameter out if you wish to attach the Anbox container directly to a specific network, see Network bridge#With NetworkManager. If you choose this option, you must also change the network configuration of the container in anbox-container-manager.service, see the next bullet point.
  • 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

Installing apps

Unless you picked an image with Houdini, Anbox does not have support for ARM applications. So apps must have a x86_64 architecture.

Through adb

To install /path/to/app.apk

$ adb install /path/to/app.apk

To get the list of installed applications

$ adb shell pm list packages

Note that output will be similar to package:app.name, where app.name is different from the one displayed in the Anbox container.

To uninstall app.name

$ adb uninstall app.name

If app.name is a system app

$ adb uninstall --user 0 app.name

Through apps stores

Apps can be easily installed through apps stores. In anbox-image-gappsAUR PlayStore is included.

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