Accelerating Android App debugging with VirtualBox

This tutorial shows how to use VirtualBox to accelerate debugging of Android apps with native components. We will show how to create an Android VirtualBox VM, configure it for debugging and use VisualGDB to debug the San-Angeles project. Before you begin, download the latest VirtualBox and get an x86 Android ISO from the website.

  1. Install and run VirtualBox. Click the “New” button to create a new VM:01-virtualbox
  2. Select “Linux -> Other Linux (32-bit)” as the machine type:02-android
  3. Proceed with the default memory size:03-memory
  4. Select “Create a virtual hard disk now”:04-makedisk
  5. Proceed with the default virtual disk format:05-disktype
  6. On the next page select “dynamically allocated”:06-dynamic
  7. Finally you can customize the disk size. The default value of 8GB should be enough for most cases:07-makedisk
  8. Before you can start installing the Android OS into your VM, you need to mount the ISO file in it. Click “Settings” to open the VM settings:08-settings
  9. Go to the “Storage” tab, select the empty CD-ROM device and click the disc icon to browse for a disc image:09-disk
  10. Specify the path to the Android image you downloaded. It will appear in the Settings window:10-androidimg
  11. Go to the network settings and select either Host-only adapter (if you don’t have a local DHCP server) or Bridged mode (if your network has a DHCP server):10a-network
  12. Finally press OK to save the settings and start the VM:11-start
  13. In the boot menu select “Installation” and press Enter:12-bootmenu
  14. Wait until the Android OS boots:13-boot
  15. Select “Create/modify partitions”:14-partition
  16. Select “New” to create a new partition:15-new
  17. Select “Primary” to create a partition that can be used to store a bootable OS:16-primary
  18. Proceed with the size suggested by the partition utility. By default the new partition will cover the entire disk:  17-size
  19. Select the “Bootable” button and press Enter to mark the partition as bootable:18-boot
  20. Finally select “Write” and press Enter to save the partition table to the disk:19-write
  21. Type “yes” and press Enter to confirm the write:20-yes
  22. Now you can select the newly created partition in the partition list and press OK:21-sda1
  23. Choose to format it with the ext3 filesystem:22-ext3
  24. Confirm the format operation:23-format
  25. Choose “Yes” to install the boot loader so that you can boot into the operating system:24-grub
  26. Choose to make the system directory writable as this will allow fixing permission bug described later:25-rw
  27. The Android OS will now be installed. Once the installation completes, choose “Reboot”:26-reboot
  28. To avoid booting into the installer again, open VM properties and remove the virtual CD-ROM device:17-remove
  29. Once the OS boots, select your language and press the button to the right to continue: 27-english
  30. If you cannot see the mouse pointer, try toggling the mouse integration in the Input menu:28-mouseint
  31. Skip the WiFi selection as we will be using a virtual network adapter anyway:29-skipwifi
  32. Enter the user name that will be associated with your virtual OS installation:30-demouser
  33. Now the installation is complete and you can finally start using your OS. It is recommended to create a VM snapshot at this point so that you can return to it later if something breaks:31-desktop
  34. Open the Terminal app from the apps list and run the following commands to fix the run-as permission bug and determine the current IP address:
    ifconfig eth0
    chmod 4750 /system/bin/run-as

    32-configUnless you update the permission on the run-as tool, you won’t be able to debug native Android code, as the Android system won’t be able to launch the gdbserver under the correct user account.

  35. Open a command prompt window on your Windows machine and run the “adb connect <IP ADDRESS>:5555” command followed by “adb shell”. You should see the Android shell of your virtual device:33-shellYou can close the shell by pressing Ctrl-D and exit the command prompt window.
  36. Now we will show how to create and debug a basic OpenGL app with Visual Studio and VisualGDB. Start Visual Studio and launch the VisualGDB Android Project Wizard:34-androidprj
  37. Select “Clone a sample from NDK”:35-clone
  38. Select the san-angeles project from the sample list:36-sanangeles
  39. Select the Android OS version that matches the version you installed into VirtualBox and select “x86” as the hardware platform:37-x86
  40. Once the project is created, remove the app-linux.c and app-win32.c files:38-remove
  41. Build the project with Ctrl-Shift-B:39-build
  42. If your Android VM screen appears blank, the Android OS has most likely gone into the sleep mode and won’t react on keyboard and mouse events. Use the Machine->ACPI shutdown command to wake it up by emulating the power button press: 40-wakeup
  43. Hit F5 in Visual Studio to start debugging. You will see the san-angeles demo being rendered in the VirtualBox window:41-demo
  44. Set a breakpoint somewhere in the code (e.g. in the camTrack() function). It should hit as soon as the function gets executed:42-break
  45. You can now use the normal Visual Studio debugging techniques to debug your application. If you are using the Clang-based IntelliSense that comes with VisualGDB, you can also use advanced features like Preprocessor Lens and C++ Code Map:43-codemap