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[編輯 | 編輯原始碼]