Android: Difference between revisions

From ArchWiki
(→‎Adding udev Rules: Point towards the AUR package that installs these rules for you)
m (Link correction.)
 
(319 intermediate revisions by 98 users not shown)
Line 1: Line 1:
[[Category:Android]]
[[Category:Development]]
[[Category:Development]]
[[Category:Mobile devices]]
[[fi: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}}
Plug your phone to your computer using a USB cable adapted for your device.
Make sure this USB cable has a data line (not all cables do).


== Exploring Android device ==
On your smartphone, you should see the charging icon.
If this is the case, go to your notifications, scroll to the bottom until you see the notification saying that your phone is connected in ''Charging mode''.


There are few methods of exploring your device:
Click on the notification, and then set it to ''File transfer mode'', ''MTP'' or something similar.


*[[MTP]] over USB for files transferring.
You should now see your phone being detected by most desktop environments.
*[[#Alternative connection methods|Alternative methods]] (such as FTP, SSH).


For more advanced usage, development, flashing and restore:
== Synchronization ==
*[[#Android Debug Bridge (ADB)|ADB]] mostly for development purposes.
*[[#Restoring Android|Restoring Android]] for flashing and restoring Android firmwares (includes fastboot).


== Android development ==
There are various applications to transfer files, synchronize notifications and more.


There are 3 steps that need to be performed before you can develop Android applications on your Arch Linux box:
=== All-in-one ===


# Install the Android SDK core component,
* [[KDE Connect]] ({{Pkg|kdeconnect}}) – integrates your Android device with the KDE or Gnome desktop (featuring synced notifications & clipboard, multimedia control, and file/URL sharing).
# Install one or several Android SDK Platform packages,
# Install one of the IDEs compatible with the Android SDK.


=== Android SDK core components ===
=== Synchronized notifications ===


{{Note|If you are running a 64-bit system, make sure the [[multilib]] repository is enabled to avoid "error: target not found: lib32-zlib" error messages.}}
* {{AUR|a2ln}} – provides notification synchronization over LAN featuring authentication, encryption and more


{{Note|If you plan to install [[#Android Studio]] and want the IDE to manage your SDK installation, you do not need to install these packages}}
=== Transferring files ===


Before developing Android applications, you need to install the Android SDK, which is made of 3 distinct packages, all installable from [[AUR]]:
*  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)
** [[cloud synchronization clients]]
** [[Syncthing]]
** {{AUR|sendanywhere}} – cross-platform file sharing
** {{AUR|qrcp}} – transfer files over wifi from your computer to your mobile device by scanning a QR code


# {{AUR|android-sdk}}
== App development ==
# {{AUR|android-sdk-platform-tools}}
# {{AUR|android-sdk-build-tools}}


{{AUR|android-support}} is also required if supporting older devices.
The officially supported way to build Android apps is to use [[#Android Studio]].[https://developer.android.com/training/basics/firstapp/creating-project]


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 modify anything in this directory. If you intend to use it as a regular user, create the Android sdk users group:
=== Android Studio ===
# groupadd sdkusers


Add your user into this group:
[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.
# gpasswd -a <user> sdkusers


Change folder's group.
You can [[install]] it with the {{AUR|android-studio}} package.
# chown -R :sdkusers /opt/android-sdk/
For the Beta branch, install the {{AUR|android-studio-beta}} package.
For the Canary branch, install the {{AUR|android-studio-canary}} package.


Change permissions of the folder so the user that was just added to the group will be able to write in it:
Android Studio creates a {{ic|.android}} directory in home directory. To reset Android Studio, this directory can be removed.
# chmod -R g+w /opt/android-sdk/


Re-login or as <user> log your terminal in to the newly created group:
{{Note|1=<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].
}}


$ newgrp sdkusers
The Android Studio Setup Wizard installs the required [[#SDK packages]] and places the SDK by default in {{ic|~/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]. You may also use the android-*-dummy packages in the [[AUR]] to handle the system dependencies.}}
To build apps from the command-line (using e.g. {{ic|./gradlew assembleDebug}}) set the [https://developer.android.com/studio/command-line/variables#android_home ANDROID_HOME] [[environment variable]] to your SDK location.


=== Android SDK platform API ===
=== SDK packages ===


{{Note|If you plan to install [[#Android Studio]] and want the IDE to handle your SDK, you don't need to install these packages}}
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/}}.


Install the desired Android SDK Platform package from the [[AUR]]:
The [https://developer.android.com/studio/intro/update#required required SDK packages] are:


* {{aur|android-platform}} (latest)
{| class="wikitable"
* {{aur|android-platform-23}}
! Android SDK Package !! SDK-style path !!  AUR package !! AUR dummy !! CLI tools
* {{aur|android-platform-22}}
|-
* {{aur|android-platform-21}}
| [https://developer.android.com/studio/releases/cmdline-tools Command-Line Tools] || tools || {{AUR|android-sdk-cmdline-tools-latest}} || {{AUR|android-sdk-cmdline-tools-latest-dummy}} || apkanalyzer, avdmanager, lint, retrace, screenshot2, sdkmanager
* {{aur|android-platform-20}}
|-
* {{aur|android-platform-19}}
| [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}} || aapt, aapt2, aidl, apksigner, bcc_compat, d8, dexdump, dx, lld, llvm-rs-cc, mainDexClases, split-select, zipalign
* {{aur|android-platform-18}}
|-
* {{aur|android-platform-17}}
| [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]], dmtracedump, e2fsdroid, etc1tool, [[#Fastboot|#fastboot]], hprof-conv, make_f2fs, make_f2fs_casefold, mke2fs, sload_f2fs, sqlite3, systrace
* {{aur|android-platform-16}}
|-
* {{aur|android-platform-15}}
| [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] || {{AUR|android-platform-dummy}} (unnecessary)
* {{aur|android-platform-14}}
|}


=== Development environment ===
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}}.


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.
{{Note|1=<nowiki/>
* 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.
* If you get a {{ic|java.lang.NoClassDefFoundError}} exception when attempting to run {{ic|sdkmanager}}, use OpenJDK 8's JRE temporarily, by installing the package {{Pkg|jre8-openjdk}} and [[Java#Switching between JVM|switching the Java environment]]. See [https://stackoverflow.com/questions/47150410/failed-to-run-sdkmanager-list-with-java-9 Failed to run sdkmanager --list with Java 9]
}}


==== Android Studio ====
==== Android Emulator ====


[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.
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 is also a dummy package for it: {{AUR|android-emulator-dummy}}.


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.
To run the Android Emulator you need an Intel or ARM System Image. You can install them [https://aur.archlinux.org/packages/?K=android-+system+image through the AUR], with the sdkmanager or using Android Studio's [https://developer.android.com/studio/run/managing-avds AVD Manager].


{{Note|1=If you are using a tiling window manager other 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.}}
If Wayland is used, make sure to read [[Wayland#Qt]], as it might be the case that the emulator complains about Wayland.
{{Note|1=Make sure you properly [[Java#Change_default_Java_environment|set the Java environment]] otherwise android-studio will not start.}}
{{Note|1=Bad font rendering in Android Studio can be fixed by installing the [[Infinality#Installation|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]]. }}


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}}:
==== Other SDK packages in the AUR ====


export ANDROID_HOME=/opt/android-sdk
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].
You can also install it offline through the {{ic|extras;android;m2repository}} SDK package (also available as {{AUR|android-support-repository}}).


==== Eclipse ====
==== Making /opt/android-sdk group-writeable ====


{{Note|Since 2014-12-08, the ADT plugin is officially considered deprecated and Android Studio is now the official IDE.}}
{{Accuracy|Do not do this. Using another package manager (Android SDK manager) to manage pacman-installed files is a bad idea. This renders package manager useless.}}


The official, but deprecated, [http://developer.android.com/sdk/eclipse-adt.html Eclipse ADT] plugin can be installed with the {{AUR|eclipse-android}} package.
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.


{{Note|
# groupadd android-sdk
* if you get a message about unresolvable dependencies, install [[Java]] manually and try again.
# gpasswd -a <user> android-sdk
* 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 {{ic|# 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
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)"


  Windows -> Preferences -> Android
  # setfacl -R -m g:android-sdk:rwx /opt/android-sdk
# setfacl -d -m g:android-sdk:rwX /opt/android-sdk 


{{Note|
Re-login or as <user> log your terminal in to the newly created group:
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 ====
$ newgrp android-sdk


If you prefer using [[Netbeans]] as your IDE and want to develop Android applications, download the [http://www.nbandroid.org NBAndroid] by going to:
=== Other IDEs ===


Tools -> Plugins -> Settings
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.


Add the following URL: http://nbandroid.org/release81/updates/updates.xml
==== Netbeans ====


Then go to '''Available Plugins''' and install the '''Android''' and '''JUnit''' plugins. Once you have installed go to:
If you prefer using [[Netbeans]] as your IDE and want to develop Android applications, use [https://github.com/NBANDROIDTEAM/NBANDROID-V2 NBAndroid-V2] .


Tools -> Options -> Miscellaneous -> Android
Install {{AUR|android-sdk}} package and follow the instructions from the [https://github.com/NBANDROIDTEAM/NBANDROID-V2/blob/master/README.md NBANDROID README].


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.
==== Vim / Neovim ====


=== Android Debug Bridge (ADB) ===
It is possible to write {{ic|flutter}} applications for Android and iOS using (Neo)vim like an IDE. Install [https://github.com/neoclide/coc.nvim coc] using a [[Vim#Using a plugin manager|Vim plugin manager]]. Also install the [https://github.com/iamcco/coc-flutter coc-flutter] extension for autocompletion (like in Android Studio) and to load the code into an Android emulator.


{{Tip|For some devices, you may have to enable MTP on the device, before ADB will work. Some other devices require enable PTP mode to work.}}
==== Emacs ====
{{Tip|Many devices' udev rules are included in {{Pkg|libmtp}}, so if you have this installed, the following steps may not be necessary.}}


==== Connect device ====
To develop a mobile {{ic|flutter}} application using Emacs, as the official instruction at [https://flutter.dev/docs/get-started/editor?tab=emacs flutter.dev] suggests, install [https://emacs-lsp.github.io/lsp-dart/ lsp-dart].
To connect to a real device or phone via ADB under Arch, you must:


# Install {{Pkg|android-tools}}. In addition, you might want to install {{Pkg|android-udev}} if you wish to connect the device to the proper {{ic|/dev/}} entries.
=== Other Tools ===
# Enable USB Debugging on your phone or device:
#* 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 (7 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.
# Add yourself to the ''adbusers'' group:
# gpasswd -a ''username'' adbusers


If [[#Detect the device|ADB recognizes your device]] (it is visible and accessible in IDE), you are done. Otherwise see instructions below.
==== Marvin ====


==== Figure out device IDs ====
Marvin is a tool which helps beginners set up an Android development environment. Installing {{AUR|marvin_dsc}} helps you set up the following things: JDK, Android SDK, IDE(s), and AVD.


== Building ==


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.


Each Android device has a USB vendor/product ID. An example for HTC Evo is:
{{Out of date|The upstream [https://android.googlesource.com/platform/build/+/refs/heads/master/envsetup.sh envsetup.sh] script uses python3, not python2 as described in this section.}}


vendor id: 0bb4
=== Required packages ===
product id: 0c8d


Plug in your device and execute:
{{Note|Before doing the following steps, the [[multilib]] repository must be enabled in {{ic|/etc/pacman.conf}}.}}


$ lsusb
To build AOSP 13 you need a TTF font installed (e.g. {{Pkg|ttf-dejavu}}) and the dependencies of the {{AUR|aosp-devel}} metapackage.


It should come up something like this:
Additionally, LineageOS (as well as other many Android distributions like ArrowOS,PixelExperience etc)  requires the following dependencies of the {{AUR|lineageos-devel}} metapackage.


Bus 002 Device 006: ID 0bb4:0c8d High Tech Computer Corp.
=== Java Development Kit ===


==== Adding udev Rules ====
The [https://source.android.com/source/requirements required JDK version] depends on the Android version you are building:


Use the rules from {{Aur|android-udev-git}}, install them manually from [http://source.android.com/source/initializing.html#configuring-usb-access Android developer], or 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}}:
* 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 {{Pkg|jdk8-openjdk}} package.


{{hc|/etc/udev/rules.d/51-android.rules|2=<nowiki>SUBSYSTEM=="usb", ATTR{idVendor}=="[VENDOR ID]", MODE="0660", GROUP="adbusers"
{{Note|1=
SUBSYSTEM=="usb",ATTR{idVendor}=="[VENDOR ID]",ATTR{idProduct}=="[PRODUCT ID]",SYMLINK+="android_adb"
For older Android versions, where Java is not included, Java is expected to be in {{ic|/usr/lib/jvm/java-''version''-openjdk-amd64}}.
SUBSYSTEM=="usb",ATTR{idVendor}=="[VENDOR ID]",ATTR{idProduct}=="[PRODUCT ID]",SYMLINK+="android_fastboot"</nowiki>}}
Set JAVA_HOME to avoid this requirement and match the Arch Linux installation path.
Example:


Then, to reload your new udev rules, execute:
  $ export JAVA_HOME=/usr/lib/jvm/java-''version''-openjdk
  # udevadm control --reload-rules


Make sure you are member of {{ic|adbusers}} [[group]] to access {{ic|adb}} devices.
This change will be valid only for the current terminal session.
}}


==== Configuring adb ====
=== Setting up the build environment ===


Instead of using udev rules, you may create/edit {{ic|~/.android/adb_usb.ini}} which contains a list of vendor IDs.
[[Install]] the {{Pkg|repo}} package.  


$ cat ~/.android/adb_usb.ini
Create a directory to build.
0x27e8


==== Detect the device ====
$ mkdir ~/android
$ cd ~/android


After you have setup the udev rules, unplug your device and replug it.
{{Note|Do not build under a directory where you do not have read/write access.}}


After running:
=== Downloading the source code ===


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


you should see something like:
* The {{ic|repo}} has a {{ic|-j}} switch that operates similarly to the one used with {{ic|make}}. Since it controls the number of simultaneous downloads, you should adjust the value depending on downstream network bandwidth.


List of devices attached
* You will need to specify a '''branch''' ([https://android.googlesource.com/platform/manifest/+refs list of branches]) to check out with the {{ic|-b}} switch. If you leave the switch out, you will get the so-called '''master branch'''.
HT07VHL00676    device


==== General usage ====
{{bc|
$ repo init -u https://android.googlesource.com/platform/manifest -b master
$ repo sync -j4
}}


You can now use adb to transfer files between the device and your computer. To transfer files to the device, use
{{Note|To further decrease sync times, you can use the {{ic|-c}} switch with the ''repo'' command as such:
$ adb push ''<what-to-copy>'' ''<where-to-place>''


To transfer files from the device, use
  $ repo sync -j8 -c
  $ adb pull ''<what-to-pull>'' ''<where-to-place>''


==== Notes & Troubleshooting ====
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.
}}


* '''ADB''' can also be installed via [[#Android SDK platform API|platform tools]](usually available in {{Ic|/opt/android-sdk/platform-tools/}}), so it might not be necesarry to install {{Pkg|android-tools}} (available in {{Ic|/usr/bin/}}).
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 very large. As of Android 10, at least 250 GB of free disk space is [https://source.android.com/setup/build/requirements required].


* 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.
{{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:


* If there are still problems such as ''adb'' displaying {{ic|???????? no permissions}} under devices, try restarting the adb server as root.
  $ repo sync
  # adb kill-server
# adb start-server


* On moto e it could happen to have a different vendor/product id while you are on sideload or fastboot, verify again lsusb if you get no permission.
}}


=== NVIDIA Tegra platform ===
=== Building the code ===


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:  
This should do what you need for AOSP:


# 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.
$ source build/envsetup.sh
# The [http://developer.nvidia.com/tegra-resources Tegra Toolkit] provides tools (mostly CPU and GPU optimization related), samples and documentation.
$ lunch full-eng
$ make -j4


Both are currently not available in the [[AUR]] anymore, because NVIDIA requires a registration/login for the download.
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.


== Building Android ==
The build takes a very long time.


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.
{{Note|
* Make sure you have enough RAM. Android will use the {{ic|/tmp}} directory heavily. By default the size of {{ic|/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 {{ic|/tmp}} is not large enough, you can [[tmpfs#Examples|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 [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 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 {{ic|make -j16}} and {{ic|make -j32}}.
}}


=== OS bitness ===
=== Testing the build ===


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.  
When finished, run/test the final image(s).


=== Required packages ===
$ emulator


To build any version of Android, you need to install these packages:
=== Creating a flashable Image ===


* 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}}
To create an image that can be flashed it is necessary to:


* 64-bit systems only: {{Pkg|gcc-multilib}} {{Pkg|lib32-zlib}} {{Pkg|lib32-ncurses}} {{Pkg|lib32-readline}}
make -j8 updatepackage


* AUR Packages 32-bit and 64-bit systems: {{Aur|ncurses5-compat-libs}}
This will create a zip image under {{ic|'''out/target/product/hammerhead'''}} (hammerhead being the device name) that can be flashed.


* AUR Packages 64-bit systems only: {{Aur|lib32-ncurses5-compat-libs}}
== Flashing ==


{{Note|The PGP signatures for {{Aur|ncurses5-compat-libs}} and {{Aur|lib32-ncurses5-compat-libs}} may cause errors, that can be solved by manually importing the needed signature:
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 [https://forum.xda-developers.com/ XDA forums].
$ gpg --recv-keys 702353E0F7E48EDB
}}


To build Android 6+, you need to install these additional packages:
=== Fastboot ===


* 32-bit and 64-bit systems: {{Pkg|rsync}}
Fastboot (as well as [[ADB]]) is included in the {{Pkg|android-tools}} package.


{{Note|1=You must now also install {{Pkg|maven}} to build CyanogenMod since, from cm-13.0, they are using maven artifacts}}
{{Note|
* Restoring firmwares using {{ic|fastboot}} can be quite tricky, but you might want to browse [https://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 - [https://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.
* If you get a {{ic|no permissions}} error or execution just hangs with {{ic|< waiting for any device >}} then you need to run {{ic|fastboot}} as the root user. Alternatively you can install {{Pkg|android-udev}} or the AUR package {{aur|android-udev-git}} and reconnect your device.
}}


=== Java Development Kit ===
=== Samsung devices ===


Android 7 (Nougat) can be built with {{Pkg|jdk8-openjdk}}.[https://source.android.com/source/requirements.html]
Samsung devices cannot be flashed using '''Fastboot''' tool. Alternatives are [[#Heimdall|Heimdall]] and [[#Odin (Virtualbox)|Odin]] (by using Windows and VirtualBox).
*For Android 5 and 6 (Lollipop and Marshmallow), OpenJDK 7 is required, which is available with the {{Pkg|jdk7-openjdk}} package.


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.
==== samloader ====
*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 available as {{AUR|jdk5}} from the AUR.


=== Setting up the build environment ===
To download original Samsung firmware, a platform independent script, [https://github.com/nlscc/samloader samloader] can be used.


[[Install]] the {{Pkg|repo}} package, then:
==== Heimdall ====


$ mkdir ~/bin
[https://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 [https://odindownload.com/ Odin]. It can be installed as {{Pkg|heimdall}}, however, it is no longer maintained: an actively maintained fork can be installed as {{AUR|heimdall-grimler-git}}.
$ export PATH=~/bin:$PATH
$ curl <nowiki>https://storage.googleapis.com/git-repo-downloads/repo</nowiki> > ~/bin/repo
$ chmod a+x ~/bin/repo


Create a directory to build.
The flashing instructions can be found on Heimdall's [https://github.com/Benjamin-Dobell/Heimdall/tree/master/Linux GitHub repository] or on [https://forum.xda-developers.com/showthread.php?t=1922461 XDA forums].


$ mkdir ~/android
==== Odin (Virtualbox) ====
$ cd ~/android


You will need to change the default Python from version 3 to version 2:
{{Note|1=This section only covers preparation and '''not''' flashing instructions. Search [https://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].}}


$ virtualenv2 venv # Creates a directory, venv/, containing the Virtualenv
It is also possible to restore [https://www.sammobile.com/firmwares/ firmware (Android)] on the Samsung devices using [https://odindownload.com/ Odin], but inside the [[VirtualBox]].


{{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).
Arch Linux (host) preparation:
Example:
$ 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.
# Install [[VirtualBox]] together with its [[VirtualBox#Extension pack|extension pack]] and [[VirtualBox#Guest additions|guest additions]].
}}
# Install your preferred, but compatible with Odin, Windows operating system (with VirtualBox guest additions) into a virtual hard drive using VirtualBox.
# 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'''.
# 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.


$ source venv/bin/activate
Windows (guest) preparation:


=== Downloading the source code ===
# Install [https://androidxda.com/download-samsung-usb-drivers Samsung drivers].
# Install [https://odindownload.com/ Odin].
# Download required [https://www.sammobile.com/firmwares/ Samsung firmware (Android)] for your smartphone model.


This will clone the repositories. You '''only''' need to do this the first time you build Android, or if you want to switch branches.
Check if configuration is working:


* The {{ic|repo}} has a {{ic|-j}} switch that operates similarly to the one used with {{ic|make}}. Since it controls the number of simultaneous downloads, you should adjust the value depending on downstream network bandwidth.
# 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:


* 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'''.
<ID:0/003> Added!!


$ repo init -u https://android.googlesource.com/platform/manifest -b master
which means that your device is visible to Odin & Windows operating system and is ready to be flashed.
$ repo sync -j4


{{Note|To further decrease sync times, you can utilize the -c switch with the repo command as such:
== Run Android apps on Arch Linux ==
$ 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.
}}


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.
There are several projects and methods which support running Android on Arch Linux (or other distributions). As listed below:


{{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:
* [[Wikipedia:Containerization_(computing)|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:
$ repo sync
** [[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.
}}


=== Building the code ===
* 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.
** [https://archon-runtime.github.io/ ARChon] is an unmaintained open source extension to run android apps on Chromium-based browsers. As of July 2023, the sample app does not work because [https://support.google.com/chrome/?p=chrome_app_deprecation "Old versions of Chrome apps won't open on Linux devices after 2022"].


This should do what you need for AOSP:
* 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:
** [[#Android Emulator|Android studio's built-in emulator]], as earlier mentioned in this article.
** [https://www.genymotion.com/ Genymotion] is a pay for Android emulation/testing suite.


$ source build/envsetup.sh
{{Tip|If you just want to be able to use Android apps on x86 and do not mind a separate OS you can also use [https://www.android-x86.org/ Android-x86]: a direct port of Android for the x86 architecture.}}
$ 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.
== Troubleshooting ==


The build takes a very long time.
=== Android Studio: Android Virtual Devices show 'failed to load'. ===


{{Note|Make sure you have enough RAM.
Make sure you have exported the variable {{ic|ANDROID_HOME}} as explained in [[#Android Studio]].
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.  
}}


{{Note|From the [https://source.android.com/source/building-running.html#build-the-code Android Building and Running guide]:
=== Android Studio: 'failed to create the SD card' ===


"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."
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.


}}
=== Eclipse: During Debugging "Source not found" ===
=== Testing the build ===


When finished, run/test the final image(s).
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 [https://help.eclipse.org/latest/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fviews%2Fdebug%2Fref-usestepfilters.htm Use Step Filters].


$ emulator
=== ValueError: unsupported pickle protocol ===


=== Creating a Flashable Image ===
One fix is to issue:
To create an image that can be flashed it is necessary to:


  make -j8 updatepackage
  $ rm ~/.repopickle_.gitconfig


This will create a zip image under '''out/target/product/hammerhead''' (hammerhead being the device name) that can be flashed.
If that does not work, then try this:
 
== Restoring Android ==
 
{{Expansion}}
 
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].
 
=== Fastboot ===


Fastboot (as well as [[#Android Debug Bridge (ADB)|ADB]]) comes together with a package {{Pkg|android-tools}} from the [[official repositories]].
$ find /path/to/android-root -name .repopickle_config -delete


{{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.}}
=== libGL error: failed to load driver: swrast OR AVD does not load and no error message displayed ===


=== Samsung devices ===
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.


Samsung devices can't be flashed using '''Fastboot''' tool. Alternatives are only '''Heimdall''' and '''Odin''' (by using Windows and VirtualBox).
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.:


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


[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}} or {{AUR|heimdall-git}}.
Note that in versions before Android Studio 3.0, this directory was in a different location:


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].
$ rm -r ~/Android/Sdk/emulator/lib64/libstdc++


==== Odin (Virtualbox) ====
Alternatively you can set and export ANDROID_EMULATOR_USE_SYSTEM_LIBS in ~/.profile as:


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].
export ANDROID_EMULATOR_USE_SYSTEM_LIBS=1


Arch Linux related steps:
Reference: [https://developer.android.com/studio/command-line/variables.html#studio_jdk Android Studio user guide]
# Install [[VirtualBox]] together with its [[VirtualBox#Extension_pack|extension pack]] and [[VirtualBox#Guest_additions_disc|guest additions]].
# Install your preferred, but compatible with Odin, Windows operating system (with VirtualBox guest additions) into a virtual hard drive using VirtualBox
# 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'''.
# 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:
Fix for the .desktop file might be achieved by using env command, prefixing the Exec line [[Desktop entries#Modify environment variables]]
* Samsung drivers can be downloaded from [http://androidxda.com/download-samsung-usb-drivers here].
* Odin can be downloaded from [https://www.androidfilehost.com/?fid=23501681358557126 here].
* Samsung Android firmware can be downloaded from [http://www.sammobile.com/firmwares/ here].


If you want to make sure that everything is working and ready:
  env ANDROID_EMULATOR_USE_SYSTEM_LIBS=1
# Turn your device into Download mode and connect to your Linux machine.
# In virtual machine toolbar, select {{ic|devices}} --> {{ic|USB}} --> {{ic|...Samsung...}} device.
# 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 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}}
=== sh: glxinfo: command not found ===


== Alternative connection methods ==
Here is the full error:


=== adb-sync ===
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


[https://github.com/google/adb-sync adb-sync] (available in {{AUR|adb-sync-git}}) is a tool to synchronize files between a PC and an Android device using the ADB protocol.
You can try to install glxinfo ({{Pkg|mesa-utils}}) 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''.


=== AirDroid ===
=== Android Emulator: no keyboard input in xfwm4 ===


[http://goo.gl/EZQ9GQ AirDroid] is an Android app to access files from your web browser.
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])


=== AndroidScreencast ===
You can use the workaround described in [https://stackoverflow.com/a/42720450]:


[http://xsavikx.github.io/AndroidScreencast AndroidScreencast] was developed to view and control your android device from a PC (using ADB).
# 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.\


=== FTP ===
=== Android Emulator: Window is shaking and blinking when used in WM tiled mode ===


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.
When using Tiled Window Manager like [[dwm]], Android Emulator will shake and blink. You can use the workaround described in [https://github.com/esjeon/krohnkite/issues/72#issuecomment-613789987 krohnkite issue 72] (window floating is induced by {{ic|Alt+f}} in [[dwm]]).


See [[List of applications/Internet#FTP]]. There are a lot of FTP clients/servers available for Android.
=== Android Emulator: Segmentation fault (core dumped) ===


=== KDE Connect ===
When using [[Nouveau]] drivers try to disable gpu hardware acceleration.


{{Pkg|kdeconnect}} integrates your android device with the KDE desktop. Its features include synced notifications, clipboard sync, multimedia control, and file/URL sharing.
In some devices it can only be done by editing {{ic|$HOME/.avd/''device_name''.avd/config.ini}}.[https://stackoverflow.com/a/58376934]


=== SSH Server ===
# Set {{ic|1=hw.gpu.enabled=no}}
# Set {{ic|1=hw.gpu.mode=off}}


There are many SSH servers available for Android, it allows you to transfer files using {{ic|scp}} command. See also [[SSH]].
=== Android Emulator: Not launching / qemu-system: address resolution failed ===


=== Samba ===
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:


See [[Samba]].
$ emulator -avd $(emulator -list-avds)


== Tips & Tricks ==
If on any line, it says anything similar to:


=== During Debugging "Source not found" ===
qemu-system-x86_64 : address resolution failed for ::1:46189: Name or service not known


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:
you may try disabling IPv6:
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


=== Linux distribution on the sdcard ===
$ sysctl net.ipv6.conf.all.disable_ipv6=1


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].
If this solves the issue and the virtual device shows up in android-studio, you may consider a permanent change:


== Troubleshooting ==
{{hc|/etc/sysctl.d/99-sysctl.conf|2=
net.ipv6.conf.all.disable_ipv6=1
}}


=== Android Studio: Android Virtual Devices show 'failed to load'. ===
=== Android Emulator: Emulator terminated with exit code 134 ===
Make sure you've exported the variable {{ic|ANDROID_HOME}} as explained in [[#Android Studio]].


=== aapt: No such file or directory ===
If running Wayland, a graphical error like {{ic|The emulator process for AVD Pixel_4_API_33 has terminated}} may be worked around by forcing X11 with {{ic|1=QT_QPA_PLATFORM=xcb}}.


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
=== adb: sideload connection failed: insufficient permissions for device ===
'''multilib/lib32-libstdc++5''' and '''multilib/lib32-zlib'''.


=== ValueError: unsupported pickle protocol ===
If you get the errors:


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


rm ~/.repopickle_.gitconfig
You might be able to solve it by restarting the adb server:


If that does not work, then try this:
  $ adb kill-server
  # adb start-server


rm `find /path/to/android-root -name .repopickle_config`
Alternatively, make sure you have installed the Android udev rules. See [[#Fastboot]].

Latest revision as of 19:31, 17 March 2024

Plug your phone to your computer using a USB cable adapted for your device. Make sure this USB cable has a data line (not all cables do).

On your smartphone, you should see the charging icon. If this is the case, go to your notifications, scroll to the bottom until you see the notification saying that your phone is connected in Charging mode.

Click on the notification, and then set it to File transfer mode, MTP or something similar.

You should now see your phone being detected by most desktop environments.

Synchronization

There are various applications to transfer files, synchronize notifications and more.

All-in-one

  • KDE Connect (kdeconnect) – integrates your Android device with the KDE or Gnome desktop (featuring synced notifications & clipboard, multimedia control, and file/URL sharing).

Synchronized notifications

  • a2lnAUR – provides notification synchronization over LAN featuring authentication, encryption and more

Transferring files

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. For the Beta branch, install the android-studio-betaAUR package. For the Canary branch, install the android-studio-canaryAUR package.

Android Studio creates a .android directory in home directory. To reset Android Studio, this directory can be removed.

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_HOME 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
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)

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.
  • If you get a java.lang.NoClassDefFoundError exception when attempting to run sdkmanager, use OpenJDK 8's JRE temporarily, by installing the package jre8-openjdk and switching the Java environment. See Failed to run sdkmanager --list with Java 9

Android Emulator

The Android Emulator is available as the emulator SDK package, the android-emulatorAUR package, and there is 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, with the sdkmanager or using Android Studio's AVD Manager.

If Wayland is used, make sure to read Wayland#Qt, as it might be the case that the emulator complains about Wayland.

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 factual accuracy of this article or section is disputed.

Reason: Do not do this. Using another package manager (Android SDK manager) to manage pacman-installed files is a bad idea. This renders package manager useless. (Discuss in Talk:Android)

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 and follow the instructions from the NBANDROID README.

Vim / Neovim

It is possible to write flutter applications for Android and iOS using (Neo)vim like an IDE. Install coc using a Vim plugin manager. Also install the coc-flutter extension for autocompletion (like in Android Studio) and to load the code into an Android emulator.

Emacs

To develop a mobile flutter application using Emacs, as the official instruction at flutter.dev suggests, install lsp-dart.

Other Tools

Marvin

Marvin is a tool which helps beginners set up an Android development environment. Installing marvin_dscAUR helps you set up the following things: JDK, Android SDK, IDE(s), and AVD.

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.

This article or section is out of date.

Reason: The upstream envsetup.sh script uses python3, not python2 as described in this section. (Discuss in Talk:Android)

Required packages

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

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 /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.

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.
  • 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, however, it is no longer maintained: an actively maintained fork can be installed as heimdall-grimler-gitAUR.

The flashing instructions can be found on Heimdall's GitHub 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.

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:
    • 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:
  • 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:
Tip: 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.

Troubleshooting

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

Make sure you have 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 Use Step Filters.

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 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

Here is 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-utils) 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 [2]:

  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 Emulator: Window is shaking and blinking when used in WM tiled mode

When using Tiled Window Manager like dwm, Android Emulator will shake and blink. You can use the workaround described in krohnkite issue 72 (window floating is induced by Alt+f in dwm).

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.[3]

  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:

/etc/sysctl.d/99-sysctl.conf
net.ipv6.conf.all.disable_ipv6=1

Android Emulator: Emulator terminated with exit code 134

If running Wayland, a graphical error like The emulator process for AVD Pixel_4_API_33 has terminated may be worked around by forcing X11 with QT_QPA_PLATFORM=xcb.

adb: sideload connection failed: insufficient permissions for device

If you get the errors:

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

or

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

You might be able to solve it by restarting the adb server:

 $ adb kill-server
 # adb start-server

Alternatively, make sure you have installed the Android udev rules. See #Fastboot.