Android Tutorial: Developing Android Apps with Android X86 and Virtual Box
In this tutorial you will learn how to develop Android application with the Android SDK without a device and without using the SDK’s emulator. What? Yes it’s possible with Android X86 and a virtual machine (in our case we take Virtual Box) to setup your machine in order to test and implement your own Android apps. Learn more about this technique!
Before starting this tutorial I highly suggest to download following elements, so you can directly start to setup your programming
- Android x86 ISO generic from
http://www.android-x86.org/download (choose the android-x86-2.2)
- Virtual Box
- A correct installation of the Android SDK and ADT in Eclipse
Setting up Virtual Box for the installation of Android x86
Once you have downloaded the iso file of Android x86, it’s time to launch it. You have the choice either to boot it as a Live CD or to install it on the Virtual Hard Drive of Virtual Box.
Step 1: Installing the Virtual Box
• Install VirtualBox
• Launch VirtualBox and press on the New button
• A dialog box appears
• Specifiy Other for OS Type and Version
Step 2: We want some memory!
- Leave the Memory settings (min 512 MB)
- Create the Virtual Hard Disk (2GB)
- Click on Next till HD creation is completed
- Click on Finish
- The VM for Android was created
Step 3: Launch Android X86
The first time you launch your (by Pressing on the Start Button and having the Android line selected) VM (Virtual Machine) you will be asked if you want to boot with a CD or from an .iso image.
We are going to select our android-x86-2.2.iso file and press OK to confirm. After a short reboot you should get following picture:
- Click on Finish
Select Live CD – Run Android-x86 without installation HDPI if you want to test Android x86 or of course you can install it completely ( recommend if you are aiming to develop and store information and settings in the VM, otherwise all data are lost each time you reboot your VM).
- After a few seconds you will get following screen:
Congratulations! Now you can test and interact with Android X86!
Note: If you encounter problems such as delays while interacting with the mouse or slow animations, then adjust the memory settings in Virtual Box.
Step 4: Setting up the VM for ADB
Here’s the trickiest part of the configuration. In order for the ADB (Android Debug Bridge) to connect to our VM and so to be able to install APKs in it, we need to setup the correct settings of our Virtual Box.
- Under Settings > Network select Bridged Adapter
- Only works, if PC connected to the internet via a Router/DSL Router, otherwise no connection is possible.
You can also choose Host_Only_Adapter if you want to develop without an internet connection.
Note: The settings of the Network in Virtual Box may vary. It’s not really a big deal to change the settings, but please note that you will need to restart the Android x86 VM each time you will modify these network settings.
Step 5: Getting the Android’s VM IP address for ADB
As you certainly know, to connect to adb we need to get the ip address of the mobile phone and the port number from which we’d like to debug to.
In Virtual Box (Android x86 is started):
- Press ALT + F1 (Android Console Mode)
- Type : netcfg
- Note the eth0 ip address
- With cmd command under Windows or Terminal with MacOS type following:
adb connect 192.168.11.12
- You have now a connection over adb to your VM!
To return to the Android UI, press ALT + F7
Believe me this was the most complex part of this tutorial 😉 The connection between your PC (by extension your development themes) and the VM is now realized. Through this connection we are going to send the compiled APK file to our VM.
Step 6: Eclipse, APK and adb again…
I assume that you are using Eclipse to develop your Android applications. Let’s start with the simplest app in the world: HelloWorld. In order to install this app (and to debug it) we need to generate an APK. If you check the Eclipse project explorer you will unfortunately note that there is no APK. In fact Eclipse hides this file under the /bin/ of your projects. The solution? Go with Windows Explorer or terminal to your project’s directory and check the presence of the APK file.
Now what’s pretty cool with Eclipse, is that each time you save your files, it compiles an APK in the background and places it in the /bin directory. Nice isn’t it ? No command lines needed to compile the APK 😉
Step 7 (option 1): Installing and Debugging (with Eclipse)
The interesting thing with the solution of launching and connecting the VM with adb is that we are “faking” a real Android device. In fact the adb connection does not check if a genuine Android hardware is connect; it will only check if there is a server responding to the adb commands. This means that we can launch the Debug from Eclipse and we will get the Android Device Chooser.
You will notice a connected device with the IP Address of your VM. This means that there is a running adb connection. Now you can press on OK and your application will be automatically installed into the Android’s VM. It couldn’t be easier!
Step 7 (option 2): Installing and Debugging (by command line)
To send your APK to the VM, we have our magic command adb, do you remember ?
So now go (with cmd or Terminal) to /bin and type following commands:
adb install -r HelloWorld.apk
The -r parameter is needed to uninstall a previously installed version of HelloWorld.
You should now directly see a new icon in your VM
and the cmd should output something like this (if you have not connected your adb before):
C:UsersMatDworkspaceHelloWorldbin>adb connect 192.168.11.12 connected to 192.168.11.12:5555
C:UsersMatDworkspaceHelloWorldbin>adb install -r HelloWorld.apk 149 KB/s (13292 bytes in 0.087s) pkg: /data/local/tmp/HelloWorld.apk Success
So now we need to debug our application. How to do that? That’s also pretty easy.
Let’s assume you have following code in your application (Ok, ok not the best debug message 😉 ):
Log.v("AndroidApp", "Hello Mat-D.com visitors!");
- Launch ddms.exe (you find it under /android-sdk/tools/ddms.exe)
- Launch your HelloWorld application from your VM and check the entry in the ddms panel
You will notice that our application was correctly started and the log file was generated
Step 8: And now?
You have learned how to develop and to debug your Android apps without a device with the VM. To increase your development work-flow, you can use some of these tricks:
- Turn off the Standby and Screen Lock option in Android (Settings -> Location and Security -> Set up screen lock)
- Launch script
Have you noticed how Eclipse does it with the emulator? Its automatically starts the application once it is compiled. We are doing the same thing by writing a small .bat file that will include the compilation and the launch of the activity:
adb install -r HelloWorld.apk adb shell am start -n com.test/.HelloWorld
com.test is obviously your package name and HelloWorld the name of your activity
Note: Please store the .bat in a safe place, as Eclipse each time it starts will clean the /bin directory of your project.
Although this, this setup obviously (mainly due to the hardware restrictions of your PC, that is not a mobile phone 😉 ) has some limitations. GPS, Sensor and Bluetooth connectivity are impossible to configure over the VM.
The most frequently asked question when I’m presenting this tutorial to Android beginners is if it is possible to use Android x86 in portrait mode: the answer is : …. well yes you could… but you need to compile Android x86 by yourself because the .iso only contains the landscape mode. Some cool guys at androidx86.org are trying to solve this problem, but if you have programmed well you application, you should support both landscape and portrait mode ;-), so it’s not really a problem.
Of course this tutorial is only meant for debugging and programming; if you plan to program Android apps you should consider buying a real Android phone as some features like GPS, multitouch, sensors etc… are not supported over the VM.
|Print article||This entry was posted by MatD on April 22, 2011 at 12:29 pm, and is filed under Android, Mobile, Techzone. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed.|