Difference between revisions of "Android"

From ArchWiki
Jump to navigation Jump to search
(Added note about hidpi and android studio)
(update interlanguage links)
Tag: wiki-scripts
 
(285 intermediate revisions by 82 users not shown)
Line 1: Line 1:
 +
[[Category:Android]]
 
[[Category:Development]]
 
[[Category:Development]]
[[Category:Mobile devices]]
+
[[es:Android]]
 
[[it:Android]]
 
[[it:Android]]
 
[[ja:Android]]
 
[[ja:Android]]
[[ru:Android]]
+
[[zh-hans:Android]]
[[zh-CN:Android]]
 
 
{{Related articles start}}
 
{{Related articles start}}
{{Related|Android notifier}}
 
 
{{Related|Android tethering}}
 
{{Related|Android tethering}}
 +
{{Related|Android Debug Bridge}}
 
{{Related articles end}}
 
{{Related articles end}}
 +
== Transferring files ==
  
== Exploring Android device ==
+
There are various ways to transfer files between a computer and an Android device:
  
There are few methods of exploring your device:
+
*  USB cable
 +
** [[Media Transfer Protocol]] for modern Android devices
 +
** [[Wikipedia:USB Mass Storage|USB mass storage]] for older devices
 +
** [[Android Debug Bridge]]
 +
* special USB sticks / regular USB stick with adapter
 +
* [[Bluetooth]]
 +
* Arch Linux software with Android counterparts
 +
** client or server for protocols that can be used to transfer files (eg. [[SSH]], [[FTP]], [[Samba]] or HTTP)
 +
** [[KDE Connect]] ({{Pkg|kdeconnect}}) – integrates your Android device with the KDE desktop (featuring synced notifications & clipboard, multimedia control, and file/URL sharing).
 +
** [[cloud synchronization clients]]
 +
** [[Syncthing]]
 +
** {{AUR|sendanywhere}} – cross-platform file sharing
  
*[[MTP]] over USB for files transferring.
+
== App development ==
*[[#Alternative connection methods|Alternative methods]] (such as FTP, SSH).
 
  
For more advanced usage, development, flashing and restore:
+
The officially supported way to build Android apps is to use [[#Android Studio]].[https://developer.android.com/training/basics/firstapp/creating-project]
*[[#Connecting to a real device - Android Debug Bridge (ADB)|ADB]] mostly for development purposes.
 
*[[#Restoring Android|Restoring Android]] for flashing and restoring Android firmwares. (Includes Fastboot)
 
  
== Android development ==
+
=== Android Studio ===
  
There are 3 steps that need to be performed before you can develop Android applications on your Arch Linux box:
+
[https://developer.android.com/studio/index.html Android Studio] is the official Android development environment based on [[Wikipedia:IntelliJ IDEA|IntelliJ IDEA]]. It provides integrated Android developer tools for development and debugging.
  
# Install the Android SDK core component,
+
You can [[install]] it with the {{AUR|android-studio}} package.
# Install one or several Android SDK Platform packages,
 
# Install one of the IDEs compatible with the Android SDK.
 
  
=== Android SDK core components ===
+
{{Note|1=<nowiki></nowiki>
 +
* Make sure you properly [[Java#Change_default_Java_environment|set the Java environment]] otherwise android-studio will not start.
 +
* If Android Studio shows up as a blank window try [[export]]ing {{ic|1=_JAVA_AWT_WM_NONREPARENTING=1}}, see [https://code.google.com/p/android/issues/detail?id=57675 issue #57675].
 +
}}
 +
 
 +
The Android Studio Setup Wizard installs the required [[#SDK packages]] and places the SDK by default in {{ic|~/Android/Sdk}}.
 +
 
 +
To build apps from the command-line (using e.g. {{ic|./gradlew assembleDebug}}) set the [https://developer.android.com/studio/command-line/variables#android_sdk_root ANDROID_SDK_ROOT] [[environment variable]] to your SDK location.
  
{{Note|First, if you are running a 64-bit system, make sure the [[multilib]] repository is enabled in [[Pacman#Repositories|pacman.conf]]. Otherwise errors of the type: "error: target not found: lib32-zlib" will plague your installation attempt.}}
+
=== SDK packages ===
  
Before developing Android applications, you need to install the Android SDK, which is made of 3 distinct packages, all installable from [[AUR]]:
+
Android SDK packages can be installed directly from upstream using [[#Android Studio]]'s [https://developer.android.com/studio/intro/update#sdk-manager SDK Manager] or the [https://developer.android.com/studio/command-line/sdkmanager sdkmanager] command line tool (part of the Android SDK Tools). Some Android SDK packages are also available as [[AUR]] packages, they generally install to {{ic|/opt/android-sdk/}}.
  
# {{AUR|android-sdk}}
+
The [https://developer.android.com/studio/intro/update#recommended required SDK packages] are:
# {{AUR|android-sdk-platform-tools}}
 
# {{AUR|android-sdk-build-tools}}
 
  
Android-sdk will be installed on {{ic|/opt/android-sdk}}. This folder has root permissions, so keep in mind to run sdk manager as root, otherwise you will not be able to install/update/modify anything on /opt/android-sdk. However, if you intend to use it as a regular user, create an android sdk users group (or use any group name you want):
+
{| class="wikitable"
# groupadd sdkusers
+
! Android SDK Package !! SDK-style path !!  AUR package !! AUR dummy !! CLI tools
 +
|-
 +
| [https://developer.android.com/studio/releases/sdk-tools SDK Tools] || tools || {{AUR|android-sdk}} || {{AUR|android-sdk-dummy}} || sdkmanager, apkanalizer, avdmanager, mksdcard, proguard
 +
|-
 +
| [https://developer.android.com/studio/releases/build-tools SDK Build-Tools] || build-tools;''version'' || {{AUR|android-sdk-build-tools}} || {{AUR|android-sdk-build-tools-dummy}} || apksigner, zipalign
 +
|-
 +
| [https://developer.android.com/studio/releases/platform-tools SDK Platform-Tools] || platform-tools || {{AUR|android-sdk-platform-tools}} || {{AUR|android-sdk-platform-tools-dummy}} || [[adb]], [[#Fastboot|#fastboot]] and systrace
 +
|-
 +
| [https://developer.android.com/studio/releases/platforms SDK Platform] || platforms;android-''level'' || {{AUR|android-platform}}, [https://aur.archlinux.org/packages/?K=android-platform- older versions] || unnecessary
 +
|}
  
Add your user into this group:
+
The {{Pkg|android-tools}} package provides [[adb]], [[#Fastboot|#fastboot]], {{ic|e2fsdroid}} and {{ic|mke2fs.android}} from the SDK Platform-Tools along with {{ic|mkbootimg}} and {{ic|ext2simg}}.
# gpasswd -a <user> sdkusers
 
  
Change folder's group.
+
{{Note|1=&nbsp;
# chown -R :sdkusers /opt/android-sdk/
+
* Since the Android SDK contains 32-bit binaries, you must enable the [[multilib]] repository. Otherwise you will get {{ic|error: target not found: lib32-*}} error messages.
 +
* If you choose to directly install SDK packages from upstream, install the AUR packages of the ''AUR dummy'' column to pull in the required dependencies.}}
  
Change permissions of the folder so the user that was just added to the group will be able to write in it:
+
==== Android Emulator ====
# chmod -R g+w /opt/android-sdk/
 
  
{{Note|As an alternative to a global install with the [[AUR]] packages, the SDK can be installed to a user's home directory via [https://developer.android.com/sdk/index.html the upstream instructions].}}
+
The [https://developer.android.com/studio/run/emulator Android Emulator] is available as the {{ic|emulator}} SDK package, the {{AUR|android-emulator}} package. And there's also a dummy package for it: {{AUR|android-emulator-dummy}}.
  
=== Android SDK platform API ===
+
To run the Android Emulator you need an Intel or ARM System Image. You can install them through the AUR[https://aur.archlinux.org/packages/?K=android-+system+image], with the sdkmanager or using Android Studio's [https://developer.android.com/studio/run/managing-avds AVD Manager].
  
Install the desired Android SDK Platform package from the [[AUR]]:
+
==== Other SDK packages in the AUR ====
  
* {{aur|android-platform}} (latest)
+
The [https://developer.android.com/topic/libraries/support-library/ Android Support Library] is now available online from [https://developer.android.com/studio/build/dependencies#google-maven Google's Maven repository].
* {{aur|android-platform-22}}
+
You can also install it offline through the {{ic|extras;android;m2repository}} SDK package (also available as {{AUR|android-support-repository}}).
* {{aur|android-platform-21}}
 
* {{aur|android-platform-20}}
 
* {{aur|android-platform-19}}
 
* {{aur|android-platform-18}}
 
* {{aur|android-platform-17}}
 
* {{aur|android-platform-16}}
 
* {{aur|android-platform-15}}
 
* {{aur|android-platform-14}}
 
* {{aur|android-platform-13}}
 
* {{aur|android-platform-12}}
 
* {{aur|android-platform-11}}
 
* {{aur|android-platform-10}}
 
* {{aur|android-platform-9}}
 
* {{aur|android-platform-8}}
 
* {{aur|android-platform-7}}
 
* {{aur|android-platform-6}}
 
* {{aur|android-platform-5}}
 
* {{aur|android-platform-4}}
 
* {{aur|android-platform-3}}
 
* {{aur|android-platform-2}}
 
  
=== Development environment ===
+
==== Making /opt/android-sdk group-writeable ====
  
Android Studio is the new official Android development environment based on IntelliJ IDEA. Alternatively, you can use [[Eclipse]] with the official but deprecated ADT plugin, or [[Netbeans]] with the NBAndroid plugin. All are described below.
+
The AUR packages install the SDK in {{ic|/opt/android-sdk/}}. This directory has root [[permissions]], so keep in mind to run sdk manager as root. If you intend to use it as a regular user, create the Android sdk users group, add your user.
  
==== Android Studio ====
+
# groupadd android-sdk
 +
# gpasswd -a <user> android-sdk
  
[https://developer.android.com/sdk/index.html Android Studio] is the official Android development environment based on [https://www.jetbrains.com/idea/ IntelliJ Idea]. Android Studio replaces the older [https://developer.android.com/tools/help/adt.html Eclipse Android Developer Tools] and provides integrated Android developer tools for development and debugging.
+
Set an access control list to let members of the newly created group write into the android-sdk folder. As running sdkmanager can also create new files, set the ACL as default ACL. the X in the default group entry means "allow execution if executable by the owner (or anyone else)"
  
You can download and install it with the {{AUR|android-studio}} package from the [[AUR]]. If you get an error about a missing SDK, refer to the section Getting Android SDK platform API above.
+
# setfacl -R -m g:android-sdk:rwx /opt/android-sdk
 +
# setfacl -d -m g:android-sdk:rwX /opt/android-sdk 
  
{{Note|1=If you are using other tiling window manager than i3wm, you may need to apply one of the fixes mentioned in [https://code.google.com/p/android/issues/detail?id=57675 this] issue page.}}
+
Re-login or as <user> log your terminal in to the newly created group:
  
{{Note|1=Bad font rendering in Android Studio can be fixed by installing the [[Infinality#Installation_2|infinality-bundle]] and using infinality patched openJDK 7 ({{AUR|jdk7-openjdk-infinality}}) or openJDK 8 ({{AUR|jdk8-openjdk-infinality}}) from the AUR as mentioned in [https://youtrack.jetbrains.com/issue/IDEA-57233#comment=27-876236 this] issue page. Patched OpenJDK8 is also available from [[Unofficial user repositories#infinality-bundle|Infinality unofficial repository]]. }}
+
$ newgrp android-sdk
  
{{Note|1=Bad HiDPI scaling and text overflowing widget boundaries can be fixed by adding {{ic|1=-Dhidpi=true}} to {{ic|~/.AndroidStudio1.2/studio64.vmoptions}}.}}
+
=== Other IDEs ===
  
Normally, apps are built through the Android Studio GUI. To build apps from the commandline (using e.g. {{ic|./gradlew assembleDebug}}), add the following to your {{ic|~/.bashrc}}:
+
Android Studio is the official Android development environment based on IntelliJ IDEA. Alternatively, you can use [[Netbeans]] with the NBAndroid-V2. All are described below.
  
export ANDROID_HOME=/opt/android-sdk
+
==== Netbeans ====
  
==== Eclipse ====
+
If you prefer using [[Netbeans]] as your IDE and want to develop Android applications, use [https://github.com/NBANDROIDTEAM/NBANDROID-V2 NBAndroid-V2] .
 +
 
 +
Install {{AUR|android-sdk}} package
 +
 
 +
Add the following URL by going to ''Tools > Plugins > Settings'':
 +
 
 +
{| class="wikitable"
 +
! Netbeans version !! URL
 +
|-
 +
| Netbeans 8.1 || http://server.arsi.sk/nbandroid81/updates.xml
 +
|-
 +
| Netbeans 8.2 || http://server.arsi.sk/nbandroid82/updates.xml
 +
|-
 +
|}
  
{{Note|Since 2014-12-08, the ADT plugin is officially considered deprecated and Android Studio is now the official IDE.}}
+
Then go to ''Available Plugins'' and install the ''Gradle-Android-support'' plugin.
  
Most stuff required for Android development in Eclipse is already packaged in AUR:
+
==== Eclipse ====
  
Official plugin by Google &ndash; [http://developer.android.com/sdk/eclipse-adt.html Eclipse ADT]:
+
{{Note|The Eclipse ADT plugin is no longer supported. Google recommends to use Android Studio instead.[https://android-developers.googleblog.com/2016/11/support-ended-for-eclipse-android.html]}}
# {{AUR|eclipse-android}}
 
  
Dependencies:
+
The official, but deprecated, [http://developer.android.com/sdk/eclipse-adt.html Eclipse ADT] plugin can be installed with the {{AUR|eclipse-android}} package.
# {{AUR|eclipse-emf}}
 
# {{AUR|eclipse-gef}}
 
# {{AUR|eclipse-wtp}}
 
  
 
{{Note|
 
{{Note|
Line 121: Line 132:
 
}}
 
}}
  
Enter the path to the Android SDK Location in
+
Enter the path to the Android SDK Location in ''Windows > Preferences > Android''.
  
Windows -> Preferences -> Android
+
{{Note|If the plugins do not show up in Eclipse after the AUR package has been upgraded, then eclipse probably has out-of-date caches. Running {{ic|sudo eclipse -clean}} once should clear them. If the problem persists, uninstall eclipse and all the plugins, delete {{ic|/usr/share/eclipse}}, and reinstall everything.}}
  
{{Note|
+
== Building ==
If the plugins do not show up in Eclipse after the AUR package has been upgraded, then eclipse probably has out-of-date caches. Running {{ic|sudo eclipse -clean}} once should clear them. If the problem persists, uninstall eclipse and all the plugins, delete {{ic|/usr/share/eclipse}}, and reinstall everything.
 
}}
 
  
==== Netbeans ====
+
Please note that these instructions are based on the [https://source.android.com/source/building official AOSP build instructions]. Other Android-derived systems such as LineageOS will often require extra steps.
  
If you prefer using [[Netbeans]] as your IDE and want to develop Android applications, download the [http://www.nbandroid.org NBAndroid] by going to:
+
=== Required packages ===
  
Tools -> Plugins -> Settings
+
To build any version of Android, you need to install these packages:
  
Add the following URL: http://nbandroid.org/updates/updates.xml
+
* {{Pkg|lib32-gcc-libs}} {{Pkg|git}} {{Pkg|gnupg}} {{Pkg|flex}} {{Pkg|bison}} {{Pkg|gperf}} {{Pkg|sdl}} {{Pkg|wxgtk2}} {{Pkg|squashfs-tools}} {{Pkg|curl}} {{Pkg|ncurses}} {{Pkg|zlib}} {{Pkg|schedtool}} {{Pkg|perl-switch}} {{Pkg|zip}} {{Pkg|unzip}} {{Pkg|libxslt}} {{Pkg|python2-virtualenv}} {{Pkg|bc}} {{Pkg|rsync}} {{Aur|ncurses5-compat-libs}} {{Pkg|lib32-zlib}} {{Pkg|lib32-ncurses}} {{Pkg|lib32-readline}} {{Aur|lib32-ncurses5-compat-libs}}
  
Then go to '''Available Plugins''' and install the '''Android''' and '''JUnit''' plugins. Once you have installed go to:
+
The {{Aur|aosp-devel}} metapackage provides them all for simple installation.
  
Tools -> Options -> Miscellaneous -> Android
+
Additionally, LineageOS requires the following packages: {{AUR|xml2}}, {{Pkg|lzop}}, {{Pkg|pngcrush}}, {{Pkg|imagemagick}}
  
and select the path where the SDK is installed (/opt/android-sdk by default). That is it, now you can create a new Android project and start developing using Netbeans.
+
They can be installed with the {{Aur|lineageos-devel}} metapackage.
  
=== Connecting to a real device - Android Debug Bridge (ADB) ===
+
{{Accuracy|The note below is not clear and probably incomplete.}}
 +
{{Note|1=Installing both {{Pkg|maven}} and {{Pkg|gradle}} to build LineageOS may result in a build speed improvement as the build process will prefer the system's }}
  
{{Tip|For some devices, you may have to enable MTP on the device, before ADB will work.}}
+
=== Java Development Kit ===
  
To get ADB to connect to a real device or phone under Arch, you must:
+
The [https://source.android.com/source/requirements required JDK version] depends on the Android version you are building:
  
* Install {{Pkg|android-tools}}.
+
* For Android 7 and 8 (Nougat and Oreo), OpenJDK 8 is required, which is available with the {{Pkg|jdk8-openjdk}} package.  
* Enable USB Debugging on your phone or device:
+
* For Android 5 and 6 (Lollipop and Marshmallow), OpenJDK 7 is required, which is available with the {{Pkg|jdk7-openjdk}} package.
** Jelly Bean (4.2) and newer: Go to {{ic|Settings --> About Phone}} tap “Build Number” until you get a popup that you have become a developer (about 10 times). Then go to {{ic|Settings --> Developer --> USB debugging}} and enable it.
 
** Older versions: This is usually done from {{ic|Settings --> Applications --> Development --> USB debugging}}. Reboot the phone after checking this option to make sure USB debugging is enabled.
 
* install {{Pkg|android-udev}} to connect the device to the proper {{ic|/dev/}} entries.
 
* Add yourself to the ''adbusers'' group. ({{ic|gpasswd -a ''username'' adbusers}})
 
  
If [[#Does it work?|ADB recognizes your device]] (it is visible and accessible in IDE), you are done. Otherwise see instructions below.
+
Older versions require a working '''Oracle JDK''' installed on your build system. It '''will not''' work with OpenJDK.
  
==== Figure out device IDs ====
+
* For Gingerbread through KitKat (2.3 - 4.4), Java 6 is required, which is available as {{AUR|jdk6}} from the AUR.
 +
* For Cupcake through Froyo (1.5 - 2.2), Java 5 is required, which is available as {{AUR|jdk5}} from the AUR.
  
Each Android device has a USB vendor/product ID. An example for HTC Evo is:
+
{{Note|1=
 +
Android expects Java in {{ic|/usr/lib/jvm/java-''version''-openjdk-amd64}}.
 +
Set JAVA_HOME to avoid this requirement and match the Arch Linux installation path.
 +
Example:
  
  vendor id: 0bb4
+
  $ export JAVA_HOME=/usr/lib/jvm/java-''version''-openjdk
product id: 0c8d
 
  
Plug in your device and execute:
+
This change will be valid only for the current terminal session.
 +
}}
  
$ lsusb
+
=== Setting up the build environment ===
  
It should come up something like this:
+
[[Install]] the {{Pkg|repo}} package.
  
Bus 002 Device 006: ID 0bb4:0c8d High Tech Computer Corp.
+
Create a directory to build.
  
==== Adding udev Rules ====
+
$ mkdir ~/android
 +
$ cd ~/android
  
Use the rules from [http://source.android.com/source/initializing.html#configuring-usb-access Android developer] or you can use the following template for your udev rules, just replace [VENDOR ID] and [PRODUCT ID] with yours. Copy these rules into {{ic|/etc/udev/rules.d/51-android.rules}}:
+
The Android build process expects {{ic|python}} to be python2. Prepend it to the {{ic|PATH}}:
  
{{hc|/etc/udev/rules.d/51-android.rules|2=<nowiki>SUBSYSTEM=="usb", ATTR{idVendor}=="[VENDOR ID]", MODE="0666", GROUP="adbusers"
+
$ mkdir bin
SUBSYSTEM=="usb",ATTR{idVendor}=="[VENDOR ID]",ATTR{idProduct}=="[PRODUCT ID]",SYMLINK+="android_adb"
+
$ ln -s /bin/python2 bin/python
SUBSYSTEM=="usb",ATTR{idVendor}=="[VENDOR ID]",ATTR{idProduct}=="[PRODUCT ID]",SYMLINK+="android_fastboot"</nowiki>}}
+
$ export PATH=$PWD/bin:$PATH
  
Then, to reload your new udev rules, execute:
+
Alternatively, create a python2 virtual environment and activate it:
# udevadm control --reload-rules
 
  
==== Configuring adb ====
+
$ virtualenv2 --system-site-packages venv
 +
$ source venv/bin/activate
  
Instead of using udev rules, you may create/edit {{ic|~/.android/adb_usb.ini}} which contains a list of vendor IDs.
+
{{Note|
 +
* This activation is only active for the current terminal session. The virtual env will be kept in the {{ic|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've 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:
  
  $ cat ~/.android/adb_usb.ini
+
  $ ln -s /usr/lib/python2.7/* ~/android/venv/lib/python2.7/
0x27e8
 
  
==== Does it work? ====
+
or (assuming build directory Data/Android_Build):
  
After you have setup the udev rules, unplug your device and replug it.
 
 
After running:
 
 
$ adb devices
 
 
you should see something like:
 
 
List of devices attached
 
HT07VHL00676    device
 
 
You can now use adb to transfer files between the device and your computer. To transfer files to the device, use
 
$ adb push ''<what-to-copy>'' ''<where-to-place>''
 
 
To transfer files from the device, use
 
$ adb pull ''<what-to-pull>'' ''<where-to-place>''
 
 
If you do not have the '''adb''' program, it means you have installed neither [[#Android SDK platform API|platform tools]](usually available in {{Ic|/opt/android-sdk/platform-tools/}}) nor {{Pkg|android-tools}}(available in {{Ic|/usr/bin/}}).
 
 
If you are getting an empty list (your device is not there), it may be because you have not enabled USB debugging on your device. You can do that by going to Settings => Applications => Development and enabling USB debugging. On Android 4.2 (Jelly Bean) the Development menu is hidden; to enable it go to Settings => About phone and tap Build number 7 times.
 
 
If there are still problems such as ''adb'' displaying {{ic|???????? no permissions}} under devices, try restarting the adb server as root.
 
# adb kill-server
 
# adb start-server
 
 
=== NVIDIA Tegra platform ===
 
 
If you target your application at NVIDIA Tegra platform, you might also want to install tools, samples and documentation provided by NVIDIA. In [http://developer.nvidia.com/category/zone/mobile-development NVIDIA Developer Zone for Mobile] there are two tools:
 
 
# The [http://developer.nvidia.com/tegra-resources Tegra Android Development Pack] provides tools (NVIDIA Debug Manager) related to [http://developer.android.com/sdk/eclipse-adt.html Eclipse ADT] and their documentation.
 
# The [http://developer.nvidia.com/tegra-resources Tegra Toolkit] provides tools (mostly CPU and GPU optimization related), samples and documentation.
 
 
Both are currently not available in the [[AUR]] anymore, because NVIDIA requires a registration/login for the download.
 
 
== Building Android ==
 
 
Please note that these instructions are based on the [http://source.android.com/source/building.html official AOSP build instructions]. Other Android-derived systems such as CyanogenMod will often require extra steps.
 
 
=== OS bitness ===
 
 
Android 2.2.x (Froyo) and below are the only versions of Android that will build on a 32-bit system. For 2.3.x (Gingerbread) and above, you will need a 64-bit installation.
 
 
=== Required packages ===
 
 
To build any version of Android, you need to install these packages:
 
 
* 32-bit and 64-bit systems: {{Pkg|gcc}} {{Pkg|git}} {{Pkg|gnupg}} {{Pkg|flex}} {{Pkg|bison}} {{Pkg|gperf}} {{Pkg|sdl}} {{Pkg|wxgtk}} {{Pkg|squashfs-tools}} {{Pkg|curl}} {{Pkg|ncurses}} {{Pkg|zlib}} {{Pkg|schedtool}} {{Pkg|perl-switch}} {{Pkg|zip}} {{Pkg|unzip}} {{Pkg|libxslt}} {{Pkg|python2-virtualenv}} {{Pkg|bc}}
 
 
* 64-bit systems only: {{Pkg|gcc-multilib}} {{Pkg|lib32-zlib}} {{Pkg|lib32-ncurses}} {{Pkg|lib32-readline}}
 
 
* AUR Packages: {{Aur|libtinfo}}
 
 
=== Java Development Kit ===
 
 
Android 5 (Lollipop) can be built with {{Pkg|jdk7-openjdk}}.
 
 
Older versions [http://source.android.com/source/initializing.html require] a working '''Oracle JDK''' installed on your build system. It '''will not''' work with OpenJDK.
 
*For Gingerbread through KitKat (2.3 - 4.4), Java 6 is required, which is available as {{AUR|jdk6}} from the AUR. See [[Java]] if you want to use it besides another (newer) JDK version.
 
*For Cupcake through Froyo (1.5 - 2.2), Java 5 is required, which is no longer available for Arch Linux.
 
 
=== Setting up the build environment ===
 
 
Download the {{ic|repo}} utility per [https://source.android.com/source/downloading.html Android Downloading the Source guide].
 
 
$ mkdir ~/bin
 
$ export PATH=~/bin:$PATH
 
$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
 
$ chmod a+x ~/bin/repo
 
 
Create a directory to build.
 
 
$ mkdir ~/android
 
$ cd ~/android
 
 
You will need to change the default Python from version 3 to version 2:
 
 
$ virtualenv2 venv # Creates a directory, venv/, containing the Virtualenv
 
 
{{Note|During build you may receive error pertaining to missing python modules. A quick and dirty fix is to symlink /usr/lib/python2.7/* to ~/android/venv/python2.7/ (Change ~/android to reflect your build directory if different than above).
 
Example:
 
 
  $ ln -s /usr/lib/python2.7/* /Data/Android_Build/venv/lib/python2.7/
 
  $ ln -s /usr/lib/python2.7/* /Data/Android_Build/venv/lib/python2.7/
}}
 
Activate the Virtualenv, which will update $PATH to point at Python 2.
 
  
{{Note|this activation is only active for the current terminal session.
 
 
}}
 
}}
 
$ source venv/bin/activate
 
  
 
=== Downloading the source code ===
 
=== Downloading the source code ===
Line 290: Line 219:
 
* You will need to specify a '''branch''' (release of Android) to check out with the {{ic|-b}} switch. If you leave the switch out, you will get the so-called '''master branch'''.
 
* You will need to specify a '''branch''' (release of Android) to check out with the {{ic|-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
+
<pre>
$ repo sync -j4
+
$ repo init -u https://android.googlesource.com/platform/manifest -b master
 +
$ repo sync -j4
 +
</pre>
  
 
{{Note|To further decrease sync times, you can utilize the -c switch with the repo command as such:
 
{{Note|To further decrease sync times, you can utilize the -c switch with the repo command as such:
 +
 
  $ repo sync -j8 -c
 
  $ repo sync -j8 -c
 +
 
The {{ic|-c}} switch will only sync the branch which is specified in the manifest, which in turn is determined by the branch specified with the {{ic|-b}} switch, or the default branch set by the repository maintainer.
 
The {{ic|-c}} switch will only sync the branch which is specified in the manifest, which in turn is determined by the branch specified with the {{ic|-b}} switch, or the default branch set by the repository maintainer.
 
}}
 
}}
  
Wait a long time. Just the uncompiled source code, along with the {{ic|.repo}} and {{ic|.git}} directories that are used to keep track of it, are well over 10 GB.
+
Wait a long time. Just the uncompiled source code, along with the {{ic|.repo}} and {{ic|.git}} directories that are used to keep track of it, are well over 10 GB. As of Android 6.0.1, the entire codebase totals 40 GB.
  
 
{{Note|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:
 
{{Note|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
 
  $ repo sync
 +
 
}}
 
}}
  
Line 316: Line 251:
 
The build takes a very long time.
 
The build takes a very long time.
  
{{Note|Make sure you have enough RAM.
+
{{Note|
Android will use the /tmp directory heavily. By default the size of the partition the /tmp folder is mounted on is half the size of your RAM. If it fills up, the build will fail. 4GB of RAM or more is recommended.
+
* Make sure you have enough RAM. Android will use the {{ic|/tmp}} directory heavily. By default the size of the partition the {{ic|/tmp}} folder is mounted on is half the size of your RAM. If it fills up, the build will fail. 4GB of RAM or more is recommended. Alternatively, you can get rid of the tmpfs from [[fstab]] all together.
* Alternatively, you can get rid of the tmpfs from [[fstab]] all together.  
+
* From the [https://source.android.com/source/building#build-the-code Android Building and Running guide]:
 +
"GNU make can handle parallel tasks with a {{ic|-jN}} argument, and it's common to use a number of tasks N that's 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 {{ic|make -j16}} and {{ic|make -j32}}."
 
}}
 
}}
  
{{Note|From the [https://source.android.com/source/building-running.html#build-the-code Android Building and Running guide]:
 
 
"GNU make can handle parallel tasks with a -jN argument, and it's common to use a number of tasks N that's 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 ===
 
=== Testing the build ===
  
Line 332: Line 263:
 
  $ emulator
 
  $ emulator
  
=== Creating a Flashable Image ===
+
=== Creating a flashable Image ===
 +
 
 
To create an image that can be flashed it is necessary to:
 
To create an image that can be flashed it is necessary to:
  
 
  make -j8 updatepackage
 
  make -j8 updatepackage
  
This will create a zip image under '''out/target/product/hammerhead''' (hammerhead being the device name) that can be flashed.
+
This will create a zip image under {{ic|'''out/target/product/hammerhead'''}} (hammerhead being the device name) that can be flashed.
 
 
== Restoring Android ==
 
  
{{Expansion}}
+
== 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 [http://forum.xda-developers.com/ XDA forums].
 
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 [http://forum.xda-developers.com/ XDA forums].
Line 347: Line 277:
 
=== Fastboot ===
 
=== Fastboot ===
  
Fastboot (as well as [[#Connecting_to_a_real_device_-_Android_Debug_Bridge_.28ADB.29|ADB]]) comes together with a package {{Pkg|android-tools}} from the [[official repositories]].
+
Fastboot (as well as [[ADB]]) is included in the {{Pkg|android-tools}} package.
  
 
{{Note|Restoring firmwares using {{ic|fastboot}} can be quite tricky, but you might want to browse [http://www.xda-developers.com/ XDA developers forums] for a stock firmware, which is mostly a {{ic|*.zip}} file, but inside of it, comes with the firmware files and {{ic|flash-all.sh}} script. For example, [https://developers.google.com/android/nexus/images Google Nexus] firmwares include {{ic|flash-all.sh}} script or another example could be for OnePlus One - [http://forum.xda-developers.com/oneplus-one/general/guide-return-opo-to-100-stock-t2826541 XDA thread], where you can find firmwares with included {{ic|flash-all.sh}} script.}}
 
{{Note|Restoring firmwares using {{ic|fastboot}} can be quite tricky, but you might want to browse [http://www.xda-developers.com/ XDA developers forums] for a stock firmware, which is mostly a {{ic|*.zip}} file, but inside of it, comes with the firmware files and {{ic|flash-all.sh}} script. For example, [https://developers.google.com/android/nexus/images Google Nexus] firmwares include {{ic|flash-all.sh}} script or another example could be for OnePlus One - [http://forum.xda-developers.com/oneplus-one/general/guide-return-opo-to-100-stock-t2826541 XDA thread], where you can find firmwares with included {{ic|flash-all.sh}} script.}}
  
=== Samsung ===
+
=== Samsung devices ===
  
Samsung does not support fastboot in any way. Using Odin is safer, easier and more popular than Heimdall, but it's up to you.
+
Samsung devices can't be flashed using '''Fastboot''' tool. Alternatives are only '''Heimdall''' and '''Odin''' (by using Windows and VirtualBox).
  
 
==== Heimdall ====
 
==== Heimdall ====
  
[http://glassechidna.com.au/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 [http://odindownload.com/ Odin]. It can be installed as {{AUR|heimdall}} or {{AUR|heimdall-git}}.
+
[http://glassechidna.com.au/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 [http://odindownload.com/ Odin]. It can be installed as {{Pkg|heimdall}}.
  
The flashing instructions can be found on Heimdall's [https://github.com/Benjamin-Dobell/Heimdall/tree/master/Linux GitHub page] or on [http://forum.xda-developers.com/showthread.php?t=1922461 XDA forums].
+
The flashing instructions can be found on Heimdall's [https://gitlab.com/BenjaminDobell/Heimdall/tree/master/Linux GitLab repository] or on [http://forum.xda-developers.com/showthread.php?t=1922461 XDA forums].
  
 
==== Odin (Virtualbox) ====
 
==== Odin (Virtualbox) ====
  
It is also possible to restore stock Android on the Samsung devices using [http://odindownload.com/ Odin], but inside the [[VirtualBox]]. For more information, see [http://forum.xda-developers.com/showthread.php?t=758634 XDA thread].
+
{{Note|1=This section only covers preparation and '''not''' flashing instructions. Search [http://www.xda-developers.com XDA developers forums] to find flashing instructions for a specific device. For example, [https://forum.xda-developers.com/showthread.php?t=2265477 Samsung Galaxy S4].}}
 +
 
 +
It is also possible to restore [http://www.sammobile.com/firmwares/ firmware (Android)] on the Samsung devices using [http://odindownload.com/ Odin], but inside the [[VirtualBox]].
  
Arch Linux related steps:
+
Arch Linux (host) preparation:
# Install [[VirtualBox]] together with it's [[VirtualBox#Extension_pack|extension pack]]. Optionally, install [[VirtualBox#Guest_additions_disc|guest additions]].
 
# Install your preferred, but compatible with Odin, Windows operating system into a virtual hard drive using VirtualBox. Optionally, install guest additions to the Windows operating system.
 
# Open VirtualBox settings of your Windows operating system, navigate to '''USB''', then tick (or make sure it's ticked) '''Enable USB 2.0 (EHCI) Controller'''.
 
# At VirtualBox running Windows operating system, click in the menu bar '''Devices''', then '''USB Devices''', then click on your Samsung mobile device connected to your computer via USB.
 
  
Windows related links:
+
# Install [[VirtualBox]] together with its [[VirtualBox#Extension_pack|extension pack]] and [[VirtualBox#Guest_additions_disc|guest additions]].
# Samsung drivers can be downloaded from [http://androidxda.com/download-samsung-usb-drivers here].
+
# Install your preferred, but compatible with Odin, Windows operating system (with VirtualBox guest additions) into a virtual hard drive using VirtualBox.
# Odin can be downloaded from [https://www.androidfilehost.com/?fid=23501681358557126 here].
+
# 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'''.
# Samsung Android firmwares can be downloaded from [http://www.sammobile.com/firmwares/ here].
+
# 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:
 +
 
 +
# Install [http://androidxda.com/download-samsung-usb-drivers Samsung drivers].
 +
# Install [http://odindownload.com/ Odin].
 +
# Download required [http://www.sammobile.com/firmwares/ Samsung firmware (Android)] for your smartphone model.
 +
 
 +
Check if configuration is working:
 +
 
 +
# Turn your device into Download mode and connect to your Linux machine.
 +
# In virtual machine toolbar, select ''Devices > USB > ...Samsung...'' device.
 +
# Open Odin. The white box (a big one at the bottom-left side) named '''Message''', should print a line similar to this:
  
If you want to make sure that everything is working and ready, connect your Samsung device turned on into a Download mode, and open Odin. The white box (a big one at the bottom-left) named '''Message''', should print a line similar to this:
 
 
  <ID:0/003> Added!!
 
  <ID:0/003> Added!!
which means that your device is visible to Odin and is ready to be flashed.
 
  
{{Note|There are no general instructions of restoring stock firmware on Samsung mobile devices. You have to use [https://www.google.com Google] and [http://www.xda-developers.com XDA developers forums] to find a flashing instructions for specific device. For example, this is how the [http://goo.gl/cZLyF8 thread] about the Samsung Galaxy S4 looks like}}
+
which means that your device is visible to Odin & Windows operating system and is ready to be flashed.
  
== Alternative connection methods ==
+
== Troubleshooting ==
  
=== AirDroid ===
+
=== Android Studio: Android Virtual Devices show 'failed to load'. ===
  
[http://goo.gl/EZQ9GQ AirDroid] is an Android app to access files from your web browser.
+
Make sure you've exported the variable {{ic|ANDROID_HOME}} as explained in [[#Android Studio]].
  
=== FTP ===
+
=== Android Studio: 'failed to create the SD card' ===
  
You run a FTP server on Arch and connect to it from your phone, or the other way around: run a FTP server on your phone and connect to it from Arch.
+
If you try to run an AVD (Android Virtual Device) under x86_64 Arch and get the error above, install the {{Pkg|lib32-gcc-libs}} package from the [[multilib]] repository.
  
See [[List of applications/Internet#FTP]]. There are a lot of FTP clients/servers available for Android.
+
=== Eclipse: During Debugging "Source not found" ===
 +
 
 +
Most probably the debugger wants to step into the Java code. As the source code of Android does not come with the Android SDK, this leads to an error. The best solution is to use step filters to not jump into the Java source code. Step filters are not activated by default. To activate them: ''Window > Preferences > Java > Debug > Step Filtering''.
 +
Consider to select them all. If appropriate you can add the android.* package. See the forum post for more information: http://www.eclipsezone.com/eclipse/forums/t83338.rhtml
  
=== SSH Server ===
+
=== ValueError: unsupported pickle protocol ===
  
There are many SSH servers available for Android, it allows you to transfer files using {{ic|scp}} command. See also [[SSH]].
+
One fix is to issue:
  
=== Samba ===
+
$ rm ~/.repopickle_.gitconfig
  
See [[Samba]].
+
If that does not work, then try this:
  
== Tips & Tricks ==
+
$ find /path/to/android-root -name .repopickle_config -delete
  
=== During Debugging "Source not found" ===
+
=== libGL error: failed to load driver: swrast OR AVD doesn't load and no error message displayed ===
  
Most probably the debugger wants to step into the Java code. As the source code of Android does not come with the Android SDK, this leads to an error. The best solution is to use step filters to not jump into the Java source code. Step filters are not activated by default. To activate them:  
+
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.
  Window -> Preferences -> Java -> Debug -> Step Filtering
+
 
Consider to select them all. If appropriate you can add the android.* package. See the forum post for more information: http://www.eclipsezone.com/eclipse/forums/t83338.rhtml
+
The AVD loads an incorrect version of libstdc++, you can remove the folder libstdc++ from {{ic|~/.android-sdk/emulator/lib64}} (for 64-bit) or {{ic|~/.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: [https://developer.android.com/studio/command-line/variables.html#studio_jdk Android Studio user guide]
  
=== Linux distribution on the sdcard ===
+
Fix for the .desktop file might be achieved by using env command, prefixing the Exec line [[Desktop entries#Modify environment variables]]
  
You can install Debian like in this [http://forum.xda-developers.com/showthread.php?t=631389 thread]. Excellent guide to installing Arch in chroot (in parallel with Android) can be found on [http://archlinuxarm.org/forum/viewtopic.php?f=27&t=1361&start=40 archlinuxarm.org forum].
+
env ANDROID_EMULATOR_USE_SYSTEM_LIBS=1
  
== Troubleshooting ==
+
=== sh: glxinfo: command not found===
  
=== aapt: No such file or directory ===
+
Here's the full error:
  
The build tools include 32-bit binaries. For this reason they require 32-bit libraries. If you happened to install the SDK manually, you will additionally need to install
+
Cannot launch AVD in emulator.
'''multilib/lib32-libstdc++5''' and '''multilib/lib32-zlib'''.
+
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
  
=== ValueError: unsupported pickle protocol ===
+
You can try to install glxinfo ({{Pkg|mesa-demos}}) but if your computer has enough power you could simply use software to render graphics.  To do so, go to ''Tools > Android > AVD Manager'', edit the ''AVD'' (click the pencil icon), then select ''Software - GLES 2.0'' for ''Emulated Performance > Graphics''.
  
One fix is to issue:
+
=== Android Emulator: no keyboard input in xfwm4 ===
  
rm ~/.repopickle_.gitconfig
+
In xfwm4, the vertical toolbar buttons window that is on the right of the emulator takes focus from the emulator and consumes keyboard events.
 +
([https://issuetracker.google.com/issues/37094173 bug report])
  
If that does not work, then try this:
+
You can use the workaround described in [https://stackoverflow.com/a/42720450/1366471 this Stack Overflow answer]:
  
rm `find /path/to/android-root -name .repopickle_config`
+
# Open the xfwm4 settings.
 +
# Switch to the Focus tab.
 +
# Change the Focus Model to "Focus follow mouse".
 +
# Disable ''Automatically raise windows when they receive focus'' option below.

Latest revision as of 17:54, 9 October 2019

Transferring files

There are various ways to transfer files between a computer and an Android device:

App development

The officially supported way to build Android apps is to use #Android Studio.[1]

Android Studio

Android Studio is the official Android development environment based on IntelliJ IDEA. It provides integrated Android developer tools for development and debugging.

You can install it with the android-studioAUR package.

Note:

The Android Studio Setup Wizard installs the required #SDK packages and places the SDK by default in ~/Android/Sdk.

To build apps from the command-line (using e.g. ./gradlew assembleDebug) set the ANDROID_SDK_ROOT environment variable to your SDK location.

SDK packages

Android SDK packages can be installed directly from upstream using #Android Studio's SDK Manager or the sdkmanager command line tool (part of the Android SDK Tools). Some Android SDK packages are also available as AUR packages, they generally install to /opt/android-sdk/.

The required SDK packages are:

Android SDK Package SDK-style path AUR package AUR dummy CLI tools
SDK Tools tools android-sdkAUR android-sdk-dummyAUR sdkmanager, apkanalizer, avdmanager, mksdcard, proguard
SDK Build-Tools build-tools;version android-sdk-build-toolsAUR android-sdk-build-tools-dummyAUR apksigner, zipalign
SDK Platform-Tools platform-tools android-sdk-platform-toolsAUR android-sdk-platform-tools-dummyAUR adb, #fastboot and systrace
SDK Platform platforms;android-level android-platformAUR, older versions unnecessary

The android-tools package provides adb, #fastboot, e2fsdroid and mke2fs.android from the SDK Platform-Tools along with mkbootimg and ext2simg.

Note:  
  • Since the Android SDK contains 32-bit binaries, you must enable the multilib repository. Otherwise you will get error: target not found: lib32-* error messages.
  • If you choose to directly install SDK packages from upstream, install the AUR packages of the AUR dummy column to pull in the required dependencies.

Android Emulator

The Android Emulator is available as the emulator SDK package, the android-emulatorAUR package. And there's also a dummy package for it: android-emulator-dummyAUR.

To run the Android Emulator you need an Intel or ARM System Image. You can install them through the AUR[2], with the sdkmanager or using Android Studio's AVD Manager.

Other SDK packages in the AUR

The Android Support Library is now available online from Google's Maven repository. You can also install it offline through the extras;android;m2repository SDK package (also available as android-support-repositoryAUR).

Making /opt/android-sdk group-writeable

The AUR packages install the SDK in /opt/android-sdk/. This directory has root permissions, so keep in mind to run sdk manager as root. If you intend to use it as a regular user, create the Android sdk users group, add your user.

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

Set an access control list to let members of the newly created group write into the android-sdk folder. As running sdkmanager can also create new files, set the ACL as default ACL. the X in the default group entry means "allow execution if executable by the owner (or anyone else)"

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

Re-login or as <user> log your terminal in to the newly created group:

$ newgrp android-sdk

Other IDEs

Android Studio is the official Android development environment based on IntelliJ IDEA. Alternatively, you can use Netbeans with the NBAndroid-V2. All are described below.

Netbeans

If you prefer using Netbeans as your IDE and want to develop Android applications, use NBAndroid-V2 .

Install android-sdkAUR package

Add the following URL by going to Tools > Plugins > Settings:

Netbeans version URL
Netbeans 8.1 http://server.arsi.sk/nbandroid81/updates.xml
Netbeans 8.2 http://server.arsi.sk/nbandroid82/updates.xml

Then go to Available Plugins and install the Gradle-Android-support plugin.

Eclipse

Note: The Eclipse ADT plugin is no longer supported. Google recommends to use Android Studio instead.[3]

The official, but deprecated, Eclipse ADT plugin can be installed with the eclipse-androidAUR package.

Note:
  • if you get a message about unresolvable dependencies, install Java manually and try again.
  • as an alternative, you can install the ADT via eclipse's built in "add new software" command (see instructions on ADT site).
  • if you are in real trouble, it is also possible to download Android SDK and use the bundled Eclipse. This usually works without problems.
  • if you need to install extra SDK plugins not found in the AUR, you must change the file ownership of /opt/android-sdk first. You can do this with # chgrp -R users /opt/android-sdk ; chmod -R 0775 /opt/android-sdk (see File Permissions for more details).

Enter the path to the Android SDK Location in Windows > Preferences > Android.

Note: If the plugins do not show up in Eclipse after the AUR package has been upgraded, then eclipse probably has out-of-date caches. Running sudo eclipse -clean once should clear them. If the problem persists, uninstall eclipse and all the plugins, delete /usr/share/eclipse, and reinstall everything.

Building

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.

Required packages

To build any version of Android, you need to install these packages:

The aosp-develAUR metapackage provides them all for simple installation.

Additionally, LineageOS requires the following packages: xml2AUR, lzop, pngcrush, imagemagick

They can be installed with the lineageos-develAUR metapackage.

Tango-inaccurate.pngThe factual accuracy of this article or section is disputed.Tango-inaccurate.png

Reason: The note below is not clear and probably incomplete. (Discuss in Talk:Android#)
Note: 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 7 and 8 (Nougat and Oreo), OpenJDK 8 is required, which is available with the jdk8-openjdk package.
  • For Android 5 and 6 (Lollipop and Marshmallow), OpenJDK 7 is required, which is available with the jdk7-openjdk package.

Older versions require a working Oracle JDK installed on your build system. It will not work with OpenJDK.

  • For Gingerbread through KitKat (2.3 - 4.4), Java 6 is required, which is available as jdk6AUR from the AUR.
  • For Cupcake through Froyo (1.5 - 2.2), Java 5 is required, which is available as jdk5AUR from the AUR.
Note: Android expects Java 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
Note:
  • 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've 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 (release of Android) 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
Note: To further decrease sync times, you can utilize 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 well over 10 GB. As of Android 6.0.1, the entire codebase totals 40 GB.

Note: 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.

Note:
  • Make sure you have enough RAM. Android will use the /tmp directory heavily. By default the size of the partition the /tmp folder is mounted on is half the size of your RAM. If it fills up, the build will fail. 4GB of RAM or more is recommended. 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's common to use a number of tasks N that's 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.

Note: 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.

Samsung devices

Samsung devices can't be flashed using Fastboot tool. Alternatives are only Heimdall and Odin (by using Windows and VirtualBox).

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 GitLab repository or on XDA forums.

Odin (Virtualbox)

Note: 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.

Troubleshooting

Android Studio: Android Virtual Devices show 'failed to load'.

Make sure you've exported the variable ANDROID_HOME as explained in #Android Studio.

Android Studio: 'failed to create the SD card'

If you try to run an AVD (Android Virtual Device) under x86_64 Arch and get the error above, install the lib32-gcc-libs package from the multilib repository.

Eclipse: During Debugging "Source not found"

Most probably the debugger wants to step into the Java code. As the source code of Android does not come with the Android SDK, this leads to an error. The best solution is to use step filters to not jump into the Java source code. Step filters are not activated by default. To activate them: Window > Preferences > Java > Debug > Step Filtering. Consider to select them all. If appropriate you can add the android.* package. See the forum post for more information: http://www.eclipsezone.com/eclipse/forums/t83338.rhtml

ValueError: unsupported pickle protocol

One fix is to issue:

$ rm ~/.repopickle_.gitconfig

If that does not work, then try this:

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

libGL error: failed to load driver: swrast OR AVD doesn't 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

Here's the full error:

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

You can try to install glxinfo (mesa-demos) but if your computer has enough power you could simply use software to render graphics. To do so, go to Tools > Android > AVD Manager, edit the AVD (click the pencil icon), then select Software - GLES 2.0 for 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 this Stack Overflow answer:

  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.