Difference between revisions of "Android"

From ArchWiki
Jump to: navigation, search
m (Tethering)
(Odin (Virtualbox): a windows anecdote doesn't classify as a tip)
 
(202 intermediate revisions by 86 users not shown)
Line 1: Line 1:
 
[[Category:Development]]
 
[[Category:Development]]
 +
[[Category:Mobile devices]]
 
[[it:Android]]
 
[[it:Android]]
 +
[[ja:Android]]
 
[[ru:Android]]
 
[[ru:Android]]
 
[[zh-CN:Android]]
 
[[zh-CN:Android]]
== Android Development on Arch ==
+
{{Related articles start}}
 +
{{Related|Android notifier}}
 +
{{Related|Android tethering}}
 +
{{Related articles end}}
  
There are three parts to set up:
+
== Exploring Android device ==
# Android SDK core component,
+
# Android SDK Platform packages
+
# Development environment
+
  
=== Install Android SDK core components ===
+
There are few methods of exploring your device:
Before developing android applications, you need to install at least one Android platform. Install core SDK components from [[Arch User Repository|AUR]]:
+
 
 +
*[[MTP]] over USB for files transferring.
 +
*[[#Alternative connection methods|Alternative methods]] (such as FTP, SSH).
 +
 
 +
For more advanced usage, development, flashing and restore:
 +
*[[#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 3 steps that need to be performed before you can develop Android applications on your Arch Linux box:
 +
 
 +
# Install the Android SDK core component,
 +
# Install one or several Android SDK Platform packages,
 +
# Install one of the IDEs compatible with the Android SDK.
 +
 
 +
=== Android SDK core components ===
 +
 
 +
{{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.}}
 +
 
 +
{{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}}
 +
 
 +
Before developing Android applications, you need to install the Android SDK, which is made of 3 distinct packages, all installable from [[AUR]]:
  
 
# {{AUR|android-sdk}}
 
# {{AUR|android-sdk}}
 
# {{AUR|android-sdk-platform-tools}}
 
# {{AUR|android-sdk-platform-tools}}
 +
# {{AUR|android-sdk-build-tools}}
  
Typical installation location is {{Ic|/opt/android-sdk}}.
+
{{AUR|android-support}} is also required if supporting older devices.
  
{{Note|If you are running a 64-bit system, you will have to enable the [[multilib]] repository, to be able to to install the required dependencies using [[pacman]].}}
+
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:
 +
# groupadd sdkusers
  
=== Getting Android SDK Platform packages ===
+
Add your user into this group:
And then install the Android SDK Platform packages which can be done either automatically, from the [[Arch User Repository|AUR]], or manually.
+
# gpasswd -a <user> sdkusers
  
==== Automatic installation ====
+
Change folder's group.
{{accuracy|This involves very bad practices. Giving users write access to directories in the PATH of root and other users breaks down any difference between them.}}
+
# chown -R :sdkusers /opt/android-sdk/
Automatic installation is done via the Android SDK and device manager, which is accessible by invoking (assuming that the {{Ic|$PATH}} [[Environment Variables|variable]] contains the path to the Android SDK {{ic|tools}} directory):
+
$ android
+
or alternatively:
+
$ <path_to_android-sdk>/tools/android
+
  
If you get this error 'Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list-2.xml, reason: peer not authenticated' you need to go into the options and make sure to check 'Force https:// sources to be fetched using http://'
+
Change permissions of the folder so the user that was just added to the group will be able to write in it:
 +
# chmod -R g+w /opt/android-sdk/
  
If the automatic installation errors out, then you must do '''one''' of the following:
+
Re-login or as <user> log your terminal in to the newly created group:
  
* run the android tool with heightened privileges
+
  $ newgrp sdkusers
  # android
+
  
* set your user account as the owner of the directory. To change the owner ID for all SDK directories, run the following command as root:
+
{{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.}}
# chown -R USER /opt/android-sdk
+
  
* change the group ID instead (recommended for multiple users), first create the group, perhaps called ''android'', and add your user account to it:
+
=== Android SDK platform API ===
# groupadd android
+
# gpasswd -a USER android
+
Next, change the directory permissions:
+
# chgrp -R android /opt/android-sdk
+
# chmod -R g+w /opt/android-sdk
+
# find /opt/android-sdk -type d -exec chmod g+s {} \;
+
The final command sets the ''setgid'' bit on all subdirectories so that any new files created within them will inherit the proper group ID.
+
Then rerun
+
$ android
+
  
For step-by-step automatic installation, see: [http://developer.android.com/sdk/adding-components.html Installing SDK Components].
+
{{Note|If you plan to install [[#Android Studio]] and want the IDE to handle your SDK, you don't need to install these packages}}
  
==== Getting from AUR ====
+
Install the desired Android SDK Platform package from the [[AUR]]:
AUR currently contains multiple packages with Android platforms sometimes duplicating each other and/or having incorrect file permissions set.
+
  
==== Manual installation ====
+
* {{aur|android-platform}} (latest)
For manual installation:
+
* {{aur|android-platform-23}}
# Download the platform you want to develop on. [http://qdevarena.blogspot.com/2010/05/download-android-sdk-standalone-for.html This site] provides online links to several Android SDK components.
+
* {{aur|android-platform-22}}
# Extract the tarball to {{ic|/<path_to_android-sdk>/platforms}}.
+
* {{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}}
  
Now, you should see the platform of your choice installed in the Installed Packages window of the Android SDK and device manager.
+
=== Development environment ===
  
=== Setting up Development Environment ===
+
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.
 
+
Android Studio is a new (and still experimental!) Android development environment based on IntelliJ IDEA. The more traditional IDE is [[Eclipse]] with the ADT plugin and related packages. Alternatively you can use [[Netbeans]] for development after installing the plugin as described below.
+
  
 
==== Android Studio ====
 
==== Android Studio ====
[http://developer.android.com/sdk/installing/studio.html Android Studio] is a new (and still experimental!) Android development environment based on IntelliJ IDEA. Similar to Eclipse with the ADT Plugin, Android Studio provides integrated Android developer tools for development and debugging.
 
  
You can download and install it with the {{AUR|android-studio}} package from the AUR.
+
[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.
 +
 
 +
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.
 +
 
 +
{{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.}}
 +
{{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}}:
 +
 
 +
export ANDROID_HOME=/opt/android-sdk
  
 
==== Eclipse ====
 
==== Eclipse ====
Most stuff required for Android development in Eclipse is already packaged in AUR:
 
  
Official plugin by Google &ndash; [http://developer.android.com/sdk/eclipse-adt.html Eclipse ADT]:
+
{{Note|Since 2014-12-08, the ADT plugin is officially considered deprecated and Android Studio is now the official IDE.}}
# {{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|
 
* if you get a message about unresolvable dependencies, install [[Java]] manually and try again.
 
* 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).
 
* 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 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
 
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.
 +
}}
  
 
==== Netbeans ====
 
==== Netbeans ====
If you prefer using [[Netbeans]] as your IDE and want to develop Android applications, download the [http://www.nbandroid.org nbandroid] by going to:
+
 
 +
If you prefer using [[Netbeans]] as your IDE and want to develop Android applications, download the [http://www.nbandroid.org NBAndroid] by going to:
  
 
  Tools -> Plugins -> Settings
 
  Tools -> Plugins -> Settings
  
Add the following URL: http://kenai.com/projects/nbandroid/downloads/download/updatecenter/updates.xml
+
Add the following URL: http://nbandroid.org/release81/updates/updates.xml
  
Then go to '''Available Plugins''' and install the '''Android''' and '''Android Test Runner''' plugins for your IDE version. Once you have installed go to:
+
Then go to '''Available Plugins''' and install the '''Android''' and '''JUnit''' plugins. Once you have installed go to:
 
+
if you have problem with netbeans 7.2 "org.netbeans.modules.gsf.testrunner was needed and not found." Please remove the the '''Android''' and '''Android Test Runner''' , then change the url of nbandroid to: http://nbandroid.org/release72/updates/updates.xml , update the sources and you just need to install the '''Android''' only.
+
  
 
  Tools -> Options -> Miscellaneous -> Android
 
  Tools -> Options -> Miscellaneous -> Android
  
and select the path where the SDK is installed. That's it, now you can create a new Android project and start developing using Netbeans.
+
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.
 +
 
 +
=== Android Debug Bridge (ADB) ===
 +
 
 +
{{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.}}
 +
{{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 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.
 +
# 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.
 +
 
 +
==== Figure out device IDs ====
  
=== Connecting to a real device - Android Debug Bridge (ADB) ===
 
To get ADB to connect to a real device or phone under Arch, you must install the [[udev]] rules to connect the device to the proper {{Ic|/dev/}} entries.
 
  
==== Using existing rules ====
 
Install the AUR package {{AUR|android-udev}} to get a common list of vendor IDs. If [[#Does It Work?|ADB recognizes your device]] (it is visible and accessible in IDE), you are done. Otherwise see instructions below.
 
  
==== Figure Out Your Device Ids ====
 
 
Each Android device has a USB vendor/product ID. An example for HTC Evo is:
 
Each Android device has a USB vendor/product ID. An example for HTC Evo is:
 +
 
  vendor id: 0bb4
 
  vendor id: 0bb4
 
  product id: 0c8d
 
  product id: 0c8d
  
 
Plug in your device and execute:
 
Plug in your device and execute:
 +
 
  $ lsusb
 
  $ lsusb
  
 
It should come up something like this:
 
It should come up something like this:
 +
 
  Bus 002 Device 006: ID 0bb4:0c8d High Tech Computer Corp.
 
  Bus 002 Device 006: ID 0bb4:0c8d High Tech Computer Corp.
  
 
==== Adding udev Rules ====
 
==== Adding udev Rules ====
 +
 
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}}:
 
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}}:
  
{{hc|/etc/udev/rules.d/51-android.rules|2=<nowiki>SUBSYSTEM=="usb", ATTR{idVendor}=="[VENDOR ID]", MODE="0666"
+
{{hc|/etc/udev/rules.d/51-android.rules|2=<nowiki>SUBSYSTEM=="usb", ATTR{idVendor}=="[VENDOR ID]", MODE="0660", GROUP="adbusers"
 
SUBSYSTEM=="usb",ATTR{idVendor}=="[VENDOR ID]",ATTR{idProduct}=="[PRODUCT ID]",SYMLINK+="android_adb"
 
SUBSYSTEM=="usb",ATTR{idVendor}=="[VENDOR ID]",ATTR{idProduct}=="[PRODUCT ID]",SYMLINK+="android_adb"
 
SUBSYSTEM=="usb",ATTR{idVendor}=="[VENDOR ID]",ATTR{idProduct}=="[PRODUCT ID]",SYMLINK+="android_fastboot"</nowiki>}}
 
SUBSYSTEM=="usb",ATTR{idVendor}=="[VENDOR ID]",ATTR{idProduct}=="[PRODUCT ID]",SYMLINK+="android_fastboot"</nowiki>}}
Line 134: Line 177:
 
  # udevadm control --reload-rules
 
  # udevadm control --reload-rules
  
Note: reloading udev rules under systemd should not be required, as any rule changes should be picked up automatically.
+
Make sure you are member of {{ic|adbusers}} [[group]] to access {{ic|adb}} devices.
  
 
==== Configuring adb ====
 
==== Configuring adb ====
Instead of using udev rules you may create/edit ~/.android/adb_usb.ini which contains list of vendor ids.
 
  
  $ cat ~/.android/adb_usb.ini  
+
Instead of using udev rules, you may create/edit {{ic|~/.android/adb_usb.ini}} which contains a list of vendor IDs.
  0x27e8
+
  
==== Does It Work? ====
+
$ cat ~/.android/adb_usb.ini
 +
0x27e8
 +
 
 +
==== Detect the device ====
  
 
After you have setup the udev rules, unplug your device and replug it.
 
After you have setup the udev rules, unplug your device and replug it.
  
 
After running:
 
After running:
 +
 
  $ adb devices
 
  $ adb devices
  
 
you should see something like:
 
you should see something like:
 +
 
  List of devices attached  
 
  List of devices attached  
 
  HT07VHL00676    device
 
  HT07VHL00676    device
  
If you do not have the '''adb''' program (usually available in {{Ic|/opt/android-sdk/platform-tools/}}), it means you have not installed the platform tools.
+
==== General usage ====
  
If you are getting an empty list (your device isn't 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.
+
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>''
  
{{Tip|Make sure that your user is added to the group:
+
To transfer files from the device, use
  # gpasswd -a ''username'' adbusers
+
  $ adb pull ''<what-to-pull>'' ''<where-to-place>''
}}
+
 
 +
==== Notes & Troubleshooting ====
 +
 
 +
* '''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/}}).
 +
 
 +
* 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 "???????? no permissions" under devices, try restarting the adb server as root.
+
* 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 kill-server
 
  # adb start-server
 
  # adb start-server
  
=== Tools specific to NVIDIA Tegra platform ===
+
* 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.
  
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 packages - [http://developer.nvidia.com/tegra-resources Tegra Android Development Pack], available from AUR as {{AUR|tegra-devpack}} and [http://developer.nvidia.com/tegra-resources Tegra Toolkit], available from AUR as {{AUR|tegra-toolkit}}.
+
=== NVIDIA Tegra platform ===
  
The {{AUR|tegra-toolkit}} package provides tools (mostly CPU and GPU optimization related), samples and documentation, while the {{AUR|tegra-devpack}} provides tools (NVIDIA Debug Manager) related to [http://developer.android.com/sdk/eclipse-adt.html Eclipse ADT] and their documentation.
+
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:
  
=== Tethering ===
+
# 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.
See [[Android Tethering]]
+
# 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 ==
 
== Building Android ==
  
To build android, you need to install these packages.
+
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 32-bit and 64-bit systems: {{Aur|ncurses5-compat-libs}}
 +
 
 +
* AUR Packages 64-bit systems only: {{Aur|lib32-ncurses5-compat-libs}}
 +
 
 +
To build Android 6+, you need to install these additional packages:
 +
 
 +
* 32-bit and 64-bit systems: {{Pkg|rsync}}
 +
 
 +
{{Note|1=You must now also install {{Pkg|maven}} to build CyanogenMod since, from cm-13.0, they are using maven artifacts}}
  
*For all 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|openjdk6}} {{Pkg|perl-switch}} {{Pkg|zip}} {{Pkg|unzip}} {{Pkg|libxslt}}
+
=== Java Development Kit ===
  
*Additional packages, only required on 64-bit systems: {{Pkg|gcc-multilib}} {{Pkg|lib32-zlib}} {{Pkg|lib32-ncurses}} {{Pkg|lib32-readline}}
+
Android 5 (Lollipop) can be built with {{Pkg|jdk7-openjdk}}.
  
And you need to change the default python from version 3 to version 2:
+
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.
  
# rm /usr/bin/python
+
=== Setting up the build environment ===
# ln -s /usr/bin/python2 /usr/bin/python
+
  
Download the '''repo''' utility.
+
[[Install]] the {{Pkg|repo}} package, then:
  
 
  $ mkdir ~/bin
 
  $ mkdir ~/bin
 
  $ export PATH=~/bin:$PATH
 
  $ export PATH=~/bin:$PATH
  $ curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo
+
  $ curl <nowiki>https://storage.googleapis.com/git-repo-downloads/repo</nowiki> > ~/bin/repo
 
  $ chmod a+x ~/bin/repo
 
  $ chmod a+x ~/bin/repo
  
Line 199: Line 276:
 
  $ cd ~/android
 
  $ cd ~/android
  
Synchronize the repositories.
+
You will need to change the default Python from version 3 to version 2:
  
  $ repo init -u https://android.googlesource.com/platform/manifest (checkout the master)
+
$ 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/
 +
}}
 +
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 ===
 +
 
 +
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 {{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.
 +
 
 +
* 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
 +
$ 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 {{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.
 +
 
 +
{{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
 +
}}
  
Wait a lot.
+
=== Building the code ===
  
When finished, start building.
+
This should do what you need for AOSP:
  
 
  $ source build/envsetup.sh
 
  $ source build/envsetup.sh
Line 212: Line 321:
 
  $ make -j4
 
  $ make -j4
  
If you run '''lunch''' without arguments, it will ask what build you want to create. Use -j with a number between the number of cores and 2 * number of cores.
+
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 lot of time.
+
The build takes a very long time.
  
 
{{Note|Make sure you have enough RAM.
 
{{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.
 
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 [https://wiki.archlinux.org/index.php/Fstab fstab] all together.  
+
* Alternatively, you can get rid of the tmpfs from [[fstab]] all together.  
 
}}
 
}}
  
When finished, run the final image.
+
{{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 ===
 +
 
 +
When finished, run/test the final image(s).
  
 
  $ emulator
 
  $ 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.
 +
 +
== 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 [[#Connecting_to_a_real_device_-_Android_Debug_Bridge_.28ADB.29|ADB]]) comes together with a package {{Pkg|android-tools}} from the [[official repositories]].
 +
 +
{{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 devices ===
 +
 +
Samsung devices can't be flashed using '''Fastboot''' tool. Alternatives are only '''Heimdall''' and '''Odin''' (by using Windows and VirtualBox).
 +
 +
==== 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 {{Pkg|heimdall}} or {{AUR|heimdall-git}}.
 +
 +
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].
 +
 +
==== 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].
 +
 +
Arch Linux related steps:
 +
# 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:
 +
* 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:
 +
# 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}}
 +
 +
== Alternative connection methods ==
 +
 +
=== adb-sync ===
 +
 +
[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.
 +
 +
=== AirDroid ===
 +
 +
[http://goo.gl/EZQ9GQ AirDroid] is an Android app to access files from your web browser.
 +
 +
=== FTP ===
 +
 +
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.
 +
 +
See [[List of applications/Internet#FTP]]. There are a lot of FTP clients/servers available for Android.
 +
 +
=== SSH Server ===
 +
 +
There are many SSH servers available for Android, it allows you to transfer files using {{ic|scp}} command. See also [[SSH]].
 +
 +
=== Samba ===
 +
 +
See [[Samba]].
  
 
== Tips & Tricks ==
 
== Tips & Tricks ==
 +
 
=== During Debugging "Source not found" ===
 
=== 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:  
 
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
 
  Window -> Preferences -> Java -> Debug -> Step Filtering
Line 232: Line 427:
  
 
=== Linux distribution on the sdcard ===
 
=== Linux distribution on the sdcard ===
 +
 
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].
 
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].
  
=== Android SDK on Arch 64 ===
+
== Troubleshooting ==
When using the Android SDK and the Eclipse plugin on a 64 bit system, and the 'emulator' always crashes with a segfault, do the following:
+
 
Provide a localtime file in /usr/share/zoneinfo/localtime e.g.:
+
=== Android Studio: Android Virtual Devices show 'failed to load'. ===
  # cp /usr/share/zoneinfo/Europe/Berlin /usr/share/zoneinfo/localtime
+
Make sure you've exported the variable {{ic|ANDROID_HOME}} as explained in [[#Android Studio]].
 +
 
 +
=== aapt: No such file or directory ===
 +
 
 +
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
 +
'''multilib/lib32-libstdc++5''' and '''multilib/lib32-zlib'''.
 +
 
 +
=== ValueError: unsupported pickle protocol ===
 +
 
 +
One fix is to issue:
 +
 
 +
rm ~/.repopickle_.gitconfig
 +
 
 +
If that does not work, then try this:
  
=== Better MTP support ===
+
rm `find /path/to/android-root -name .repopickle_config`
If you have an Android device that doesn't support UMS and you find mtpfs to be extremely slow you can install {{AUR|jmtpfs}} from the AUR. See [[MTP]] for more options.
+

Latest revision as of 10:43, 20 July 2016

Exploring Android device

There are few methods of exploring your device:

For more advanced usage, development, flashing and restore:

  • ADB mostly for development purposes.
  • Restoring Android for flashing and restoring Android firmwares (includes fastboot).

Android development

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

  1. Install the Android SDK core component,
  2. Install one or several Android SDK Platform packages,
  3. Install one of the IDEs compatible with the Android SDK.

Android SDK core components

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

Before developing Android applications, you need to install the Android SDK, which is made of 3 distinct packages, all installable from AUR:

  1. android-sdkAUR
  2. android-sdk-platform-toolsAUR
  3. android-sdk-build-toolsAUR

android-supportAUR is also required if supporting older devices.

Android-sdk will be installed on /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:

# groupadd sdkusers

Add your user into this group:

# gpasswd -a <user> sdkusers

Change folder's group.

# chown -R :sdkusers /opt/android-sdk/

Change permissions of the folder so the user that was just added to the group will be able to write in it:

# chmod -R g+w /opt/android-sdk/

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

$ newgrp sdkusers
Note: As an alternative to a global install with the AUR packages, the SDK can be installed to a user's home directory via the upstream instructions. You may also use the android-*-dummy packages in the AUR to handle the system dependencies.

Android SDK platform API

Note: If you plan to install #Android Studio and want the IDE to handle your SDK, you don't need to install these packages

Install the desired Android SDK Platform package from the AUR:

Development environment

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.

Android Studio

Android Studio is the official Android development environment based on IntelliJ Idea. Android Studio replaces the older Eclipse Android Developer Tools and provides integrated Android developer tools for development and debugging.

You can download and install it with the android-studioAUR package from the AUR. If you get an error about a missing SDK, refer to the section Getting Android SDK platform API above.

Note: If you are using a tiling window manager other than i3wm, you may need to apply one of the fixes mentioned in this issue page.
Note: Make sure you properly set the Java environment otherwise android-studio will not start.
Note: Bad font rendering in Android Studio can be fixed by installing the infinality-bundle and using infinality patched openJDK 7 (jdk7-openjdk-infinalityAUR) or openJDK 8 (jdk8-openjdk-infinalityAUR) from the AUR as mentioned in this issue page. Patched OpenJDK8 is also available from Infinality unofficial repository.

Normally, apps are built through the Android Studio GUI. To build apps from the commandline (using e.g. ./gradlew assembleDebug), add the following to your ~/.bashrc:

export ANDROID_HOME=/opt/android-sdk

Eclipse

Note: Since 2014-12-08, the ADT plugin is officially considered deprecated and Android Studio is now the official IDE.

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.

Netbeans

If you prefer using Netbeans as your IDE and want to develop Android applications, download the NBAndroid by going to:

Tools -> Plugins -> Settings

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

Then go to Available Plugins and install the Android and JUnit plugins. Once you have installed go to:

Tools -> Options -> Miscellaneous -> Android

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.

Android Debug Bridge (ADB)

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.
Tip: Many devices' udev rules are included in libmtp, so if you have this installed, the following steps may not be necessary.

Connect device

To connect to a real device or phone via ADB under Arch, you must:

  1. Install android-tools. In addition, you might want to install android-udev if you wish to connect the device to the proper /dev/ entries.
  2. Enable USB Debugging on your phone or device:
    • Jelly Bean (4.2) and newer: Go to Settings --> About Phone tap “Build Number” until you get a popup that you have become a developer (7 times). Then go to Settings --> Developer --> USB debugging and enable it.
    • Older versions: This is usually done from Settings --> Applications --> Development --> USB debugging. Reboot the phone after checking this option to make sure USB debugging is enabled.
  3. Add yourself to the adbusers group:
# gpasswd -a username adbusers

If ADB recognizes your device (it is visible and accessible in IDE), you are done. Otherwise see instructions below.

Figure out device IDs

Each Android device has a USB vendor/product ID. An example for HTC Evo is:

vendor id: 0bb4
product id: 0c8d

Plug in your device and execute:

$ lsusb

It should come up something like this:

Bus 002 Device 006: ID 0bb4:0c8d High Tech Computer Corp.

Adding udev Rules

Use the rules from 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 /etc/udev/rules.d/51-android.rules:

/etc/udev/rules.d/51-android.rules
SUBSYSTEM=="usb", ATTR{idVendor}=="[VENDOR ID]", MODE="0660", GROUP="adbusers"
SUBSYSTEM=="usb",ATTR{idVendor}=="[VENDOR ID]",ATTR{idProduct}=="[PRODUCT ID]",SYMLINK+="android_adb"
SUBSYSTEM=="usb",ATTR{idVendor}=="[VENDOR ID]",ATTR{idProduct}=="[PRODUCT ID]",SYMLINK+="android_fastboot"

Then, to reload your new udev rules, execute:

# udevadm control --reload-rules

Make sure you are member of adbusers group to access adb devices.

Configuring adb

Instead of using udev rules, you may create/edit ~/.android/adb_usb.ini which contains a list of vendor IDs.

$ cat ~/.android/adb_usb.ini 
0x27e8

Detect the device

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

General usage

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>

Notes & Troubleshooting

  • ADB can also be installed via platform tools(usually available in /opt/android-sdk/platform-tools/), so it might not be necesarry to install android-tools (available in /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 ???????? no permissions under devices, try restarting the adb server as root.
# 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

If you target your application at NVIDIA Tegra platform, you might also want to install tools, samples and documentation provided by NVIDIA. In NVIDIA Developer Zone for Mobile there are two tools:

  1. The Tegra Android Development Pack provides tools (NVIDIA Debug Manager) related to Eclipse ADT and their documentation.
  2. The 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 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:

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

  • 32-bit and 64-bit systems: rsync
Note: You must now also install maven to build CyanogenMod since, from cm-13.0, they are using maven artifacts

Java Development Kit

Android 5 (Lollipop) can be built with jdk7-openjdk.

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

Install the repo package, then:

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

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

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

Restoring Android

Tango-view-fullscreen.pngThis article or section needs expansion.Tango-view-fullscreen.png

Reason: please use the first argument of the template to provide a brief explanation. (Discuss in Talk:Android#)

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) comes together with a package android-tools from the official repositories.

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 or heimdall-gitAUR.

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

Odin (Virtualbox)

It is also possible to restore stock Android on the Samsung devices using Odin, but inside the VirtualBox. For more information, see XDA thread.

Arch Linux related steps:

  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, then USB Devices, then click on your Samsung mobile device connected to your computer via USB.

Windows related links:

  • Samsung drivers can be downloaded from here.
  • Odin can be downloaded from here.
  • Samsung Android firmware can be downloaded from here.

If you want to make sure that everything is working and ready:

  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 and is ready to be flashed.

Note: There are no general instructions of restoring stock firmware on Samsung mobile devices. You have to use Google and XDA developers forums to find a flashing instructions for specific device. For example, this is how the thread about the Samsung Galaxy S4 looks like

Alternative connection methods

adb-sync

adb-sync (available in adb-sync-gitAUR) is a tool to synchronize files between a PC and an Android device using the ADB protocol.

AirDroid

AirDroid is an Android app to access files from your web browser.

FTP

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.

See List of applications/Internet#FTP. There are a lot of FTP clients/servers available for Android.

SSH Server

There are many SSH servers available for Android, it allows you to transfer files using scp command. See also SSH.

Samba

See Samba.

Tips & Tricks

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

Linux distribution on the sdcard

You can install Debian like in this thread. Excellent guide to installing Arch in chroot (in parallel with Android) can be found on archlinuxarm.org forum.

Troubleshooting

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

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

aapt: No such file or directory

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 multilib/lib32-libstdc++5 and multilib/lib32-zlib.

ValueError: unsupported pickle protocol

One fix is to issue:

rm ~/.repopickle_.gitconfig

If that does not work, then try this:

rm `find /path/to/android-root -name .repopickle_config`