Android

来自 Arch Linux 中文维基

同步[编辑 | 编辑源代码]

有各种应用程序可以用来传输文件,同步通知等。

多合一[编辑 | 编辑源代码]

  • KDE Connect (kdeconnect) – 将你的安卓设备与 KDE 或 Gnome 桌面整合在一起(具有同步通知和剪贴板、多媒体控制和文件/ URL 共享等功能)。

同步通知[编辑 | 编辑源代码]

  • a2lnAUR – 通过局域网提供通知同步,具有认证、加密等功能。

传输文件[编辑 | 编辑源代码]

应用开发[编辑 | 编辑源代码]

官方支持的构建Android应用程序的方式是使用 #Android Studio[1]

Android Studio[编辑 | 编辑源代码]

Android Studio 是基于 IntelliJ IDEA 的官方 Android 开发环境。它为开发和调试提供了集成的 Android 开发者工具。

你可以通过 AUR 安装 android-studioAUR

Android Studio在主目录下创建了一个.android 目录。为了重置 Android Studio ,可以删除这个目录。

注意:
  • 如果 Android Studio 显示为一个空白窗口,请尝试 exporting _JAVA_AWT_WM_NONREPARENTING=1, 见 issue #57675.

Android Studio 设置向导会安装所需的 #SDK packages ,并默认将 SDK 放在 ~/Android/Sdk中。

要从命令行构建应用程序 (使用例如 ./gradlew assembleDebug) 将 ANDROID_HOME 环境变量 设置为你的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 模拟器 is available as the emulator SDK package, the android-emulatorAUR package, and there is also a dummy package for it: android-emulator-dummyAUR.

为了运行安卓模拟器,你需要一个英特尔或 ARM 系统镜像。你可以通过 AUR 安装它们[2], 使用 sdkmanager 或者使用 AVD Manager.

Making /opt/android-sdk group-writeable[编辑 | 编辑源代码]

Tango-inaccurate.png本文或本章节的事实准确性存在争议。Tango-inaccurate.png

原因: 请不要这样做。使用另一个软件包管理器 (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.

Tango-view-refresh-red.png本文内容或本节内容已经过期。Tango-view-refresh-red.png

原因: 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 either AOSP 10 or LineageOS 17.1 you need a TTF font installed (e.g. ttf-dejavu) and the dependencies of the aosp-develAUR metapackage.

Additionally, LineageOS requires the following dependencies of the lineageos-develAUR metapackage.

Tango-inaccurate.png本文或本章节的事实准确性存在争议。Tango-inaccurate.png

原因: The note below is not clear and probably incomplete.(在 Talk:Android 中讨论)


注意: Installing both maven and gradle to build LineageOS may result in a build speed improvement as the build process will prefer the system's.

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

The Android build process expects python to be python2. Prepend it to the PATH:

$ mkdir bin
$ ln -s /bin/python2 bin/python
$ export PATH=$PWD/bin:$PATH

Alternatively, create a python2 virtual environment and activate it:

$ virtualenv2 --system-site-packages venv
$ source venv/bin/activate
注意:
  • This activation is only active for the current terminal session. The virtual env will be kept in the venv folder.
  • Passing "--system-site-packages" to virtualenv2 points your virtual environment to your installed python2.7 modules. This should give you all python modules you need for the build, assuming you have installed the required dependencies such as python2-mako.
  • If during the build you still receive errors pertaining to missing python modules a quick and dirty fix might be to symlink /usr/lib/python2.7/* to ~/android/venv/lib/python2.7/ (Change ~/android to reflect your build directory if different than above).

Example:

$ ln -s /usr/lib/python2.7/* ~/android/venv/lib/python2.7/

or (assuming build directory Data/Android_Build):

$ ln -s /usr/lib/python2.7/* /Data/Android_Build/venv/lib/python2.7/

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.

Run Android apps on Arch Linux[编辑 | 编辑源代码]

There are several projects and methods which support running Android on Arch Linux (or other distributions). As listed below:

  • Container-based solutions tend to be the most popular. They are the closest you can get to run Android app natively on a non-Android Linux kernel. They tend to be the best integrated with your system and have good performance. Notable ones are:
    • Anbox is probably the most famous android container solution for Linux. It runs an Android 7.1 image based on Lineageos and is completely open source.
    • Waydroid is a fork of Anbox, and it is gaining popularity. It is more performant, because it runs closer to the hardware. It is based on a newer Lineageos 17.1 (Android 10) based image with the option to install the Google Play Store and other Open Gapps. Beside running apps in standalone windows, it can also run a full Android UI.
  • There are also a few Chromium extensions that can run android apps:
    • Arc Welder was an extension by google to test how apps would run on Chrome OS, but is now discontinued.
    • ARChon is an open source extension to run android apps on Chomium-based browsers.
  • Of course it is also possible to run a full android emulator. One upside to this is that you can run arm apps on x86. A downside is worse performance. Examples are:
提示:If you just want to be able to use Android apps on x86 and do not mind a separate OS you can also use Android-x86: a direct port of Android for the x86 architecture.

故障排除[编辑 | 编辑源代码]

Android Studio: Android Virtual Devices show 'failed to load'.[编辑 | 编辑源代码]

确保你已经导出了变量 ANDROID_HOME ,如 #Android Studio 中所描述的。

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

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.