Category Archives: Android

“Where’s my phone” flowchart

When you are setting up your android development environment it’s common that you can’t detect you USB device. The following flowchart is to make troubleshooting easier. The environment is Linux host with virtualbox installed and a Linux guest. If you are not using a virtual machine you can go directly to “Does guest ‘lsusb’ detect the device”

 

usb

Worst case scenario is to not finding the device on neither host or guest.

Virtualbox – Android Development Setup

In this guide I will show how to set up a Linux guest OS for Android development.

Requirements:
Basic Linux usage.
Basic Android Studio usage.
Virtualbox installed.

Toys/Equipment:
Android Device – If you want to learn how to run your app on a device and not emulator. I’m using a Sony z1.

Time:
Around 30 min.

Table of content

Introduction


Android is based on Linux and most tools for developing is made for Linux. I used to develop using Windows but it was never as smooth as for Linux. Sometimes my work has forced me to use a Windows machine and then the first thing I do is to install a linux VM. I really hate to use Cygwin – why try imitate Linux when you can have the real thing?
Now using virtualbox for Android isn’t totally pain free since you need to use adb through USB, something that can be tricky to set up correctly, especially if your host is Linux.

Step 0 – Xubuntu


When I first started to use Ubuntu I loved it. It was not full of things I didn’t need as in Windows and it was quite user friendly. However, after a while I started to feel it was bloated and I never liked Unity. Unity throws big icons in my face and more than a few times it is incompatible and you have to do a lot of research to solve the problems.
If you are going to use your VM for Android development I instead recommend Xubuntu. It is very similar to Ubuntu, but it is stripped down and the desktop environment xfce is much more in my taste.

In this guide I will be using Xubuntu, but it should be fine to use any Ubuntu clone.
Download and install Xubuntu here:
http://xubuntu.org/

In this guide I’m using Xubuntu 14.04 64-bit. First thing to do is to is to update the guest OS. Note that for this guide ‘$’ means normal shell command, while ‘#’ means executed as super user.
#apt-get update
#apt-get upgrade

Step 1 – Why is my screen so small?!


This is probably everyone’s first question when they have installed a Linux guest.
Pressing [right ctrl + f] enter full screen mode, but still the working area is 800×600. You need to install the guest additions to get a higher resolution. The good news is that for Ubuntu it is easy (while it is somewhat more demanding for other distros such as CentOS).

Install the guest addition for x11 and reboot:
#apt-get install virtualbox-guest-x11
#reboot

Ah, that’s better!

Step 2 – Android Studio


Android Studio requires Java, and of course if you are going to develop for android you need Java 7+. Xubuntu doesn’t come with java so you have to install:
#add-apt-repository ppa:webupd8team/java
#apt-get update
#apt-get install oracle-java8-installer

java_alternatives

Check the configuration with:
#update-alternatives–config java

Download Android Studio:
https://developer.android.com/sdk/index.html#Other

Make sure you choose “All Android Packages” and not “SDK tools only”. In this guide I have used android-studio-ide-141.1980579-linux.zip.
Extract the archive. I extracted it to ~/android/android-studio

If you have 64-bit version of your guest, then you will have to install some 32-but libraries to be able to run Android Studio.
#apt-get install lib32z1 lib32ncurses5 lib32bz2-1.0 lib32stdc++6

Start android studio:
$android/android-studio/bin/studio.sh

I found Darcula them doesn’t look good in Xubuntu so I choose the default IntelliJ.
You will have to download the SDK. At some of my positions we had our own SDK but I haven’t found a way to skip this step. I installed it to ~/android/sdk

Step 3 – Android Project


Create a new Android Project with a blank activity. Connect your Android Device with USB. Run the app [shift F10]
The Choose Device dialog is empty.

Check the status of the USB on the guest:
$lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 002: ID 80ee:0021 VirtualBox USB Tablet
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

Hmm… there’s no USB device connected. Try to run the same command on your host if you run Linux, or check “This PC” if you use windows. The USB is visible at the host right?

To allow the guest to detect USB you need to add it to the VM USB Device Filters.
Open virtualbox and click USB and then on the USB with a green plus sign. You should see your device in this list. Select it and make sure it is checked and then press OK.


Only one OS (host or guest) can control an USB device at the same time. This is important to remember when you troubleshot your USB. The Device Filter control if the guest OS should use the USB or the host.
Disconnect the USB and reconnect it and check for devices on your guest.
$lsusb
Bus 001 Device 004: ID 0fce:d107 Sony Ericsson Mobile Communications AB

That’s better!
Don’t forget to check the “USB debugging” in developers option of the Android Device.

Step 4 -Troubleshooting


“Unable to run mksdcard SDK tool” when installing the SDK:
You are missing 32-bit libraries:
#apt-get install lib32z1 lib32ncurses5 lib32bz2-1.0 lib32stdc++6

I use Linux and I can’t see my USB device:
You need to add yourself to vboxusers group, or you wont have permission to access the USB devices.
#adduser $USER vboxusers

https://help.ubuntu.com/community/VirtualBox/USB

I can find my device but not use it in Android studio:
Make sure USB debugging is on on the android device.

adb devices shows ?????? as id:
adb does not have the correct premissions. Try to restart it as super user:
$adb kill-server
#adb start-server

Conclusion


Setting up an Android development environment on a guest is quite same as on a host. The only thing you need to consider is the USB support. Your key tools when trouble shooting  is lsusb and adb.  adb devices gives you information about the android device, while lsusb gives you information about USB devices.
I prefer Xubuntu, but if you want a really easy installation I recommend Linux mint http://www.linuxmint.com/download.php. It automatically installs the guest drivers (and cinnamon looks damn good).  Cooperates should really consider using mint instead of Ubuntu.