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.