How to set up a debugging and development environment for Android on Linux
This post describes how to set up a debugging and development environment for
Android based systems on an Ubuntu machine, specially for flashing custom
recoveries and ROMs, but not for coding. I’m basically pulling together
information from other sources across the web, be sure to check out the
References and other external links for more details. You only
need admin privileges to configure the permissions of the USB device with
udev
rules (see Configure device group ownership and
permissions).
Although some information is specific to the LG Nexus 4 and Cyanogenmod operating system, with a few modifications those instructions can also be used for other devices, custom ROMs and development systems; for example, I successfully followed most of the steps on a Fedora 21 system.
Installing customs ROMs on a Nexus device is easier than on other devices since by default it is very simple to unlock the bootloader on all Nexus phones/tablets. In general, the Nexus family of devices are developer friendly.
Note: I’m omitting values particular to my system (like ids, serial numbers
and ports) with the notation <variable>
.
Warning: Following this procedure will result in loss of user and system data. Always backup your data.
Contents:
- Install the Android SDK Tools
- Configure the Nexus device for development
- Unlock the bootloader
- Install TWRP recovery
- Install custom ROM / Cyanogenmod
- Notes
- References
Install the Android SDK Tools
Host system is Ubuntu 14.04
I hear that the OpenJDK packages available in the Ubuntu repositories should work with the Android SDK Tools. However, I prefer to use Oracle’s JDK so I’m able to use the latest Java 8, and also because it’s quite simple to install it without root/admin permissions.
Download latest Java SDK from Oracle’s site, which is JDK 8u45 at the time of this writing. Verify package integrity with the MD5 checksums published by Oracle
Untar the downloaded file, and add to your PATH the bin
subdirectoy
Confirm the Java SDK you just installed
Go to Android SDK Download page, and look for “SDK Tools Only”. Download the package for Linux systems. Latest version is 24.3.3 at the time of this writing. Verify the package integrity with the SHA-1 Checksum shown beside the download link.
Untar and launch the Android SDK Manager:
Install the Android SDK Platform-tools package. Uncheck other packages that
are automatically selected when the GUI launches. When done, the directory
android-sdk-linux
will now contain the subdirectory platforms-tools
with
the executables adb
and fastboot
. Add this directory to your PATH:
Configure the Nexus device for development
On the Nexus 4, go to Settings -> About phone. Tap 7 times on the Build number to enable development. Then enable USB debugging: go to Settings -> Developer Options -> USB Debugging and tap the USB Debugging checkbox.
Connect the device and get bus and device numbers:
The Nexus 4 is connected to the bus <Bus>
with device number <Device>
and
its id is XXXX:YYYY
. Check permissions of USB device
The device owner and group are root:root
, others can only read it. In other
words, admin permissions are required to write to the device.
Configure device group ownership and permissions
What we need to do is to change the device ownership and permissions. By using the udev system we can set up an udev rule that matches the device and updates its attributes, including the device ownership and permissions. With the device information we gathered previously we’ll match the device, update its group ownership and make it group-writable.
In Ubuntu systems there is already a group created for device management,
called plugdev
. My user is already a member of such group. Unplug the device
from the host computer and run below as root:
Edit the file 99-android-debug.rules
. Note that the values of the match
keys
ATTR{idVendor}
and ATTR{idProduct}
match the id XXXX:YYYY
previously
obtained:
Reload the udev
rules and restart the system service.
Plug the device to the host computer. Verify the file group is plugdev
or
whatever other group you configured in the udev
rule. The device and/or bus
number may have changed, in which case you have to get them again. Subsequent
steps do not need admin permissions.
Confirm ownership and allow adb
to access the device
Run adb
to show the device
The Nexus 4 will ask you to confirm USB debugging from the host Linux system. Click OK if the RSA key fingerprint shown by the phone matches the output of the below command (code snippet copied from this forum thread).
Now the device shows as authorized to adb
.
Unlock the bootloader
Boot into bootloader:
Once in fastboot mode, verify fastboot
correctly sees the device.
Unlock bootloader (accept the disclaimer the device displays when entering below command, then you will see the output shown below)
If the device doesn’t reboot by itself, reboot it from the menu.
Install TWRP recovery
Enable USB debugging on the device, again. Verify again that the RSA key fingerprint shown on the device matches the output of the command in a previous step.
Then download TWRP 2.8.7.0 for Nexus 4 and verify file integrity. With the device connected, reboot into fastboot mode.
In fastboot mode, flash the TWRP recovery image downloaded in the previous step
Boot into bootloader
Start in recovery mode using the device volume keys to navigate to Recovery mode and the power key to select. TWRP recovery starts. Swipe to allow modifications.
Install custom ROM / Cyanogenmod
Tap Backup and select the 4 partitions Boot, System, Data and Cache. Swipe to start backup, and when done reboot into recovery mode again. Then tap Wipe and swipe to start Factory Reset.
Download Cyanogenmod. At the time of this writing the most recent nightly build for the Nexus 4 is Cyanogenmod 12.1-20150704. Verify package integrity.
Copy CM distribution zip into device
Go back to TWRP main menu and tap Install. Search in the file system for the zip just copied and tap it. Tap checkbox Zip file signature verification and swipe to confirm flash. The message Successfull should show. Tap Reboot System and the device will now boot into CyanogenMod.
Relock bootloader
This step is optional. Enable USB debugging again and reboot into fastboot mode.
Then lock bootloader with fastboot
.
In the end you have a Nexus 4 device with Cyanogenmod 12.1, TWRP 12.8.7.0 recovery and locked bootloader.
Notes
- If something doesn’t work as expected when using
adb
, try restarting the server.
- If at any point when booting into either the stock ROM or Cyanogenmod you seem to get the boot loop (boot animation never stops for more than 30 minutes), power off device, start in recovery mode and wipe the user and cache data partitions. Then reboot.