Android

出自 Arch Linux 中文维基

同步[編輯 | 編輯原始碼]

有各種應用程式可以用來傳輸文件,同步通知等。

多合一[編輯 | 編輯原始碼]

  • KDE Connect (kdeconnect) – 將安卓設備整合到 KDE/Gnome 桌面,具有通知同步、剪貼板同步、多媒體控制和文件/ URL 共享等功能。

同步通知[編輯 | 編輯原始碼]

  • a2lnAUR – 通過局域網提供通知同步,具有認證、加密等功能。

傳輸文件[編輯 | 編輯原始碼]

應用開發[編輯 | 編輯原始碼]

官方支持的構建 Android 應用程式的方式是使用 #Android Studio[1]

Android Studio[編輯 | 編輯原始碼]

Android Studio 是基於 IntelliJ IDEA 的官方 Android 開發環境。它為開發和調試提供了集成的 Android 開發者工具。

通過 android-studioAUR 進行安裝。

Android Studio在主目錄下創建了一個.android 目錄。要重置 Android Studio ,可以刪除此目錄。

注意:
  • 請正確設置 Java 環境,否則無法啟動 android-studio。
  • 如果 Android Studio 顯示為一個空白窗口,請嘗試 exporting _JAVA_AWT_WM_NONREPARENTING=1, 見 issue #57675.

Android Studio 設置嚮導會安裝所需的 #SDK packages ,默認的 SDK 目錄是 ~/Android/Sdk

要從命令行構建應用程式 (使用例如 ./gradlew assembleDebug) 將 ANDROID_HOME 環境變量 設置為你的SDK位置。

SDK 軟件包[編輯 | 編輯原始碼]

Android SDK 包可以使用 #Android StudioSDK Managersdkmanager 命令行工具(Android SDK工具的一部分)直接從上游安裝。 一些 Android SDK 包也可以使用 AUR 包, 它們通常安裝在 /opt/android-sdk/中。

必須的軟件包 :

Android SDK Package SDK-style path AUR package AUR dummy CLI tools
Command-Line Tools tools android-sdk-cmdline-tools-latestAUR android-sdk-cmdline-tools-latest-dummyAUR apkanalyzer, avdmanager, lint, retrace, screenshot2, sdkmanager
SDK Build-Tools build-tools;version android-sdk-build-toolsAUR android-sdk-build-tools-dummyAUR aapt, aapt2, aidl, apksigner, bcc_compat, d8, dexdump, dx, lld, llvm-rs-cc, mainDexClases, split-select, zipalign
SDK Platform-Tools platform-tools android-sdk-platform-toolsAUR android-sdk-platform-tools-dummyAUR adb, dmtracedump, e2fsdroid, etc1tool, #fastboot, hprof-conv, make_f2fs, make_f2fs_casefold, mke2fs, sload_f2fs, sqlite3, systrace
SDK Platform platforms;android-level android-platformAUR, older versions android-platform-dummyAUR (unnecessary)

android-tools 軟件包提供了 adb, #fastboote2fsdroidmke2fs.android, SDK Platform-Tools along 提供了 mkbootimgext2simg

注意:
  • 由於Android SDK包含32位的二進制文件, 你必須啟用 multilib 倉庫。否則你會得到 error: target not found: lib32-* 錯誤信息。
  • 如果你選擇直接從上游安裝 SDK 包,請安裝 AUR dummy 列的 AUR 包來拉入所需的依賴。
  • 如果你在試圖運行 sdkmanager 時得到一個 java.lang.NoClassDefFoundError 異常,暫時使用 OpenJDK 8 的JRE,通過安裝 jre8-openjdk 並且 切換 java 環境。 詳見 Failed to run sdkmanager --list with Java 9

Android 模擬器[編輯 | 編輯原始碼]

如下方式可以安裝 Android 模擬器emulator SDK 包、android-emulatorAUR 軟件包。如果 android-emulator-dummyAUR 可以作為佔位空軟件包使用。

運行安卓模擬器需要英特爾或 ARM 系統鏡像。可以通過 AUR 安裝[2]sdkmanager 或者 AVD Manager 進行安裝。

Making /opt/android-sdk group-writeable[編輯 | 編輯原始碼]

本文或本章節的事實準確性存在爭議。

原因: 請不要這樣做。使用另一個軟件包管理器 (Android SDK manager) 來管理 pacman 安裝的文件是一個壞主意。這將使軟件包管理器變得毫無用處。(在 Talk:Android 中討論)


AUR 包將 SDK 安裝在 /opt/android-sdk/。 這個目錄有root 權限,所以請注意 sdk 管理器正以 root 身份運行。如果你打算以普通用戶身份使用它,創建 android-sdk 用戶組,並添加你的用戶。

# groupadd android-sdk
# gpasswd -a <user> android-sdk

設置一個訪問控制列表,讓新創建的組的成員可以寫入 android-sdk 文件夾中。由於運行 sdkmanager 也可以創建新的文件,將 ACL 設置為默認 ACL 。默認組項中的 X 意味着 "如果所有者(或其他任何用戶)可執行,允許其執行"

# setfacl -R -m g:android-sdk:rwx /opt/android-sdk
# setfacl -d -m g:android-sdk:rwX /opt/android-sdk  

重新登錄或以 <user> 身份登錄你的終端到新創建的組:

$ newgrp android-sdk

其他的 IDEs[編輯 | 編輯原始碼]

Android Studio 是基於 IntelliJ IDEA 的官方 Android 開發環境。另外,你也可以使用 Netbeans 的 NBAndroid-V2。介紹如下。

Netbeans[編輯 | 編輯原始碼]

如果你喜歡使用 Netbeans 作為你的 IDE,並想開發 Android 應用程式,請使用 NBAndroid-V2

安裝 android-sdkAUR 包 並遵循 NBANDROID README 的指示。

Vim / Neovim[編輯 | 編輯原始碼]

可以使用 (Neo)vim 像 IDE 一樣為 Android 和 iOS 編寫 flutter 應用程式。使用 Vim插件管理器 安裝 coc 。同時安裝 coc-flutter 擴展,用於代碼補全(像在 Android Studio 中),並將代碼加載到 Android 模擬器中。

Emacs[編輯 | 編輯原始碼]

要使用 Emacs 開發一個移動的 flutter 應用程式,正如 flutter.dev 的官方說明所建議的,安裝 lsp-dart

其他工具[編輯 | 編輯原始碼]

Marvin[編輯 | 編輯原始碼]

Marvin 是一個幫助初學者建立 Android 開發環境的工具。安裝 marvin_dscAUR 可以幫助你設置以下東西:JDK、Android SDK、IDE(s) 和 AVD。

編譯[編輯 | 編輯原始碼]

Please note that these instructions are based on the official AOSP build instructions. Other Android-derived systems such as LineageOS will often require extra steps.

本文內容或本節內容已經過期。

原因: The upstream envsetup.sh script uses python3, not python2 as described in this section. (在Talk:Android討論)

Required packages[編輯 | 編輯原始碼]

注意: Before doing the following steps, the multilib repository must be enabled in /etc/pacman.conf.

To build AOSP 13 you need a TTF font installed (e.g. ttf-dejavu) and the dependencies of the aosp-develAUR metapackage.

Additionally, LineageOS (as well as other many Android distributions like ArrowOS,PixelExperience etc) requires the following dependencies of the lineageos-develAUR metapackage.

Java Development Kit[編輯 | 編輯原始碼]

The required JDK version depends on the Android version you are building:

  • For Android 9 (Pie) and up, Java is included with the Android source and no separate installation is needed.
  • For Android 7 and 8 (Nougat and Oreo), OpenJDK 8 is required, which is available with the jdk8-openjdk package.
注意: For older Android versions, where Java is not included, Java is expected to be in /usr/lib/jvm/java-version-openjdk-amd64.

Set JAVA_HOME to avoid this requirement and match the Arch Linux installation path. Example:

$ export JAVA_HOME=/usr/lib/jvm/java-version-openjdk
This change will be valid only for the current terminal session.

Setting up the build environment[編輯 | 編輯原始碼]

Install the repo package.

Create a directory to build.

$ mkdir ~/android
$ cd ~/android
注意: Do not build under a directory where you do not have read/write access.

Downloading the source code[編輯 | 編輯原始碼]

This will clone the repositories. You only need to do this the first time you build Android, or if you want to switch branches.

  • The repo has a -j switch that operates similarly to the one used with make. Since it controls the number of simultaneous downloads, you should adjust the value depending on downstream network bandwidth.
  • You will need to specify a branch (list of branches) to check out with the -b switch. If you leave the switch out, you will get the so-called master branch.
$ repo init -u https://android.googlesource.com/platform/manifest -b master
$ repo sync -j4
注意: To further decrease sync times, you can use the -c switch with the repo command as such:
$ repo sync -j8 -c

The -c switch will only sync the branch which is specified in the manifest, which in turn is determined by the branch specified with the -b switch, or the default branch set by the repository maintainer.

Wait a long time. Just the uncompiled source code, along with the .repo and .git directories that are used to keep track of it, are very large. As of Android 10, at least 250 GB of free disk space is required.

注意: If you want to update your local copy of the Android source, at a later time, simply enter the build directory, load the Virtualenv, and re-sync:
$ repo sync

Building the code[編輯 | 編輯原始碼]

This should do what you need for AOSP:

$ source build/envsetup.sh
$ lunch full-eng
$ make -j4

If you run lunch without arguments, it will ask what build you want to create. Use -j with a number between one and two times number of cores/threads.

The build takes a very long time.

注意:
  • Make sure you have enough RAM. Android will use the /tmp directory heavily. By default the size of /tmp is half the size of your RAM. If it fills up, the build will fail. 4 GiB of RAM or more is recommended. If /tmp is not large enough, you can increase it. Make sure you have the combined RAM and swap space to back it. Alternatively, you can get rid of the tmpfs from fstab all together.
  • From the Android Building and Running guide:
GNU make can handle parallel tasks with a -jN argument, and it is common to use a number of tasks N that is between 1 and 2 times the number of hardware threads on the computer being used for the build. E.g. on a dual-E5520 machine (2 CPUs, 4 cores per CPU, 2 threads per core), the fastest builds are made with commands between make -j16 and make -j32.

Testing the build[編輯 | 編輯原始碼]

When finished, run/test the final image(s).

$ emulator

Creating a flashable Image[編輯 | 編輯原始碼]

To create an image that can be flashed it is necessary to:

make -j8 updatepackage

This will create a zip image under out/target/product/hammerhead (hammerhead being the device name) that can be flashed.

Flashing[編輯 | 編輯原始碼]

In some cases, you want to return to the stock Android after flashing custom ROMs to your Android mobile device. For flashing instructions of your device, please use XDA forums.

Fastboot[編輯 | 編輯原始碼]

Fastboot (as well as ADB) is included in the android-tools package.

注意:
  • Restoring firmwares using fastboot can be quite tricky, but you might want to browse XDA developers forums for a stock firmware, which is mostly a *.zip file, but inside of it, comes with the firmware files and flash-all.sh script. For example, Google Nexus firmwares include flash-all.sh script or another example could be for OnePlus One - XDA thread, where you can find firmwares with included flash-all.sh script.
  • If you get a no permissions error or execution just hangs with < waiting for any device > then you need to run fastboot as the root user. Alternatively you can install android-udev or the AUR package android-udev-gitAUR and reconnect your device.

Samsung devices[編輯 | 編輯原始碼]

Samsung devices cannot be flashed using Fastboot tool. Alternatives are Heimdall and Odin (by using Windows and VirtualBox).

samloader[編輯 | 編輯原始碼]

To download original Samsung firmware, a platform independent script, samloader can be used.

Heimdall[編輯 | 編輯原始碼]

Heimdall is a cross-platform open-source tool suite used to flash firmware (also known as ROMs) onto Samsung mobile devices and is also known as an alternative to Odin. It can be installed as heimdall.

The flashing instructions can be found on Heimdall's GitHub repository or on XDA forums.

Odin (Virtualbox)[編輯 | 編輯原始碼]

注意: This section only covers preparation and not flashing instructions. Search XDA developers forums to find flashing instructions for a specific device. For example, Samsung Galaxy S4.

It is also possible to restore firmware (Android) on the Samsung devices using Odin, but inside the VirtualBox.

Arch Linux (host) preparation:

  1. Install VirtualBox together with its extension pack and guest additions.
  2. Install your preferred, but compatible with Odin, Windows operating system (with VirtualBox guest additions) into a virtual hard drive using VirtualBox.
  3. Open VirtualBox settings of your Windows operating system, navigate to USB, then tick (or make sure it is ticked) Enable USB 2.0 (EHCI) Controller.
  4. At VirtualBox running Windows operating system, click in the menu bar Devices > USB Devices, then click on your Samsung mobile device from the list, which is connected to your computer via USB.

Windows (guest) preparation:

  1. Install Samsung drivers.
  2. Install Odin.
  3. Download required Samsung firmware (Android) for your smartphone model.

Check if configuration is working:

  1. Turn your device into Download mode and connect to your Linux machine.
  2. In virtual machine toolbar, select Devices > USB > ...Samsung... device.
  3. Open Odin. The white box (a big one at the bottom-left side) named Message, should print a line similar to this:
<ID:0/003> Added!!

which means that your device is visible to Odin & Windows operating system and is ready to be flashed.

在 Arch Linux 運行 Android 應用[編輯 | 編輯原始碼]

一些工具致力於在 Arch Linux (或其他發行版)下運行 Android 程序,如下表所示:

  • 基於容器的技術應當是最流行的。這些技術是在 Android 以外的 Linux 內核中使用 Android 應用最原生的方式,對系統的兼容性、運行性能都是最強大的。其中值得注意的有:
    • Anbox 是 Linux 最流行的 Android 容器技術。它在 Lineageos 中運行 Android 7.1 鏡像。
    • Waydroid 是 Anbox 的一個分支,正在變得流行。它更貼近硬件,也就更加高效。它基於 Lineageos 17.1 (Android 10)鏡像,提供安裝 Google Play Store 與其他開源應用的選項。在獨立窗口中運行 Android 程序,它也提供完整的 Android UI。
  • 一些 Chromium 擴展也可運行 Android 應用:
    • Arc Welder 是谷歌在 Chrome OS 中測試新應用的擴展,目前已經棄用。
    • ARChon 是在基於 Chromium 的瀏覽器中運行 Android 應用的開源擴展。
  • 當然也可以運行完整的 Android 模擬器。它的優勢是你可以在 x86 平台上運行 arm 應用,劣勢則是它的性能不佳。以下是幾個例子:
提示:如果你想運行基於 x86 的安卓應用,同時願意另外安裝作業系統,你可以使用 Android-x86:Android 在 x86 的移植。

故障排除[編輯 | 編輯原始碼]

Android Studio: Android Virtual Devices show 'failed to load'.[編輯 | 編輯原始碼]

確保按 #Android Studio 中的說明正確設置環境變量 ANDROID_HOME

Android Studio: 'failed to create the SD card'[編輯 | 編輯原始碼]

如果你試圖在 x86_64 Arch下運行AVD(Android Virtual Device)並得到上述錯誤,請從 multilib 倉庫安裝 lib32-gcc-libs 軟件包。

Eclipse: During Debugging "Source not found"[編輯 | 編輯原始碼]

最有可能的是,調試器想要跳到 Java 原始碼中。由於安卓的原始碼並不隨 Android SDK 一起提供,這就導致了錯誤。最好的解決辦法是使用步驟過濾器,不要跳到 Java 原始碼中。默認情況下,步驟過濾器沒有被激活。要激活它們:Window > Preferences > Java > Debug > Step Filtering. 考慮將它們全部選中。如果合適,你可以添加 android.* 包。參見 使用步驟過濾器

ValueError: unsupported pickle protocol[編輯 | 編輯原始碼]

一個解決方案是:

$ rm ~/.repopickle_.gitconfig

如果這不起作用,那就試試這個:

$ find /path/to/android-root -name .repopickle_config -delete

libGL error: failed to load driver: swrast OR AVD does not load and no error message displayed[編輯 | 編輯原始碼]

Sometimes, beginning to load an AVD will cause an error message similar to this to be displayed, or the loading process will appear to finish but no AVD will load and no error message will be displayed.

The AVD loads an incorrect version of libstdc++, you can remove the folder libstdc++ from ~/.android-sdk/emulator/lib64 (for 64-bit) or ~/.android-sdk/emulator/lib (for 32-bit) , e.g.:

$ rm -r ~/.android-sdk/emulator/lib64/libstdc++

Note that in versions before Android Studio 3.0, this directory was in a different location:

$ rm -r ~/Android/Sdk/emulator/lib64/libstdc++

Alternatively you can set and export ANDROID_EMULATOR_USE_SYSTEM_LIBS in ~/.profile as:

export ANDROID_EMULATOR_USE_SYSTEM_LIBS=1

Reference: Android Studio user guide

Fix for the .desktop file might be achieved by using env command, prefixing the Exec line Desktop entries#Modify environment variables

env ANDROID_EMULATOR_USE_SYSTEM_LIBS=1

sh: glxinfo: command not found[編輯 | 編輯原始碼]

以下是完整的錯誤:

Cannot launch AVD in emulator.
Output:
sh: glxinfo: command not found
sh: glxinfo: command not found
libGL error: unable to load driver: swrast_dri.so
libGL error: failed to load driver: swrast
X Error of failed request:  BadValue (integer parameter out of range for operation)
  Major opcode of failed request:  154 (GLX)
  Minor opcode of failed request:  24 (X_GLXCreateNewContext)
  Value in failed request:  0x0
  Serial number of failed request:  32
  Current serial number in output stream:  33
QObject::~QObject: Timers cannot be stopped from another thread

你可以嘗試安裝 glxinfo(mesa-utils),但如果你的電腦有足夠的能力,你可以簡單地使用軟件來渲染圖形。要做到這一點,進入 Tools > Android > AVD Manager ,編輯 AVD(點擊鉛筆圖標),然後選擇 Software - GLES 2.0 用於 Emulated Performance > Graphics

Android Emulator: no keyboard input in xfwm4[編輯 | 編輯原始碼]

In xfwm4, the vertical toolbar buttons window that is on the right of the emulator takes focus from the emulator and consumes keyboard events. (bug report)

You can use the workaround described in [3]:

  1. Open the xfwm4 settings.
  2. Switch to the Focus tab.
  3. Change the Focus Model to "Focus follow mouse".
  4. Disable Automatically raise windows when they receive focus option below.\

Android 模擬器: 在 WM 平鋪模式下使用時,窗口晃動和閃動[編輯 | 編輯原始碼]

當使用像 dwm 這樣的平鋪窗口管理器時,安卓模擬器會搖晃和閃爍。你可以使用 krohnkite issue 72 中描述的解決方法(窗口浮動是由 dwmAlt+f 引起的)。

Android Emulator: Segmentation fault (core dumped)[編輯 | 編輯原始碼]

When using Nouveau drivers try to disable gpu hardware acceleration.

In some devices it can only be done by editing $HOME/.avd/device_name.avd/config.ini.[4]

  1. Set hw.gpu.enabled=no
  2. Set hw.gpu.mode=off

Android Emulator: Not launching / qemu-system: address resolution failed[編輯 | 編輯原始碼]

There is an issue where no emulator-window shows up after starting a virtual device in android-studio. If this applies to you, launch the emulator from the console and inspect its output:

$ emulator -avd $(emulator -list-avds)

If on any line, it says anything similar to:

qemu-system-x86_64 : address resolution failed for ::1:46189: Name or service not known

you may try disabling IPv6:

$ sysctl net.ipv6.conf.all.disable_ipv6=1

If this solves the issue and the virtual device shows up in android-studio, you may consider a permanent change:

$ echo "net.ipv6.conf.all.disable_ipv6=1" | sudo tee -a /etc/sysctl.d/99-sysctl.conf

adb: sideload connection failed: insufficient permissions for device[編輯 | 編輯原始碼]

如果得到如下錯誤:

adb: sideload connection failed: insufficient permissions for device
See [https://developer.android.com/tools/device.html] for more information

或者

adb: trying pre-KitKat sideload method...
adb: pre-KitKat sideload connection failed: insufficient permissions for device
See [https://developer.android.com/tools/device.html] for more information

嘗試重啟 adb 伺服器,看能否解決這個問題:

 $ adb kill-server
 # adb start-server

或者,確保已經安裝了 Android 的 udev 規則。 參見 #Fastboot.