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 android-x86.org website.
- Install and run VirtualBox. Click the “New” button to create a new VM:
- Select “Linux -> Other Linux (32-bit)” as the machine type:
- Proceed with the default memory size:
- Select “Create a virtual hard disk now”:
- Proceed with the default virtual disk format:
- On the next page select “dynamically allocated”:
- Finally you can customize the disk size. The default value of 8GB should be enough for most cases:
- 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:
- Go to the “Storage” tab, select the empty CD-ROM device and click the disc icon to browse for a disc image:
- Specify the path to the Android image you downloaded. It will appear in the Settings window:
- 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):
- Finally press OK to save the settings and start the VM:
- In the boot menu select “Installation” and press Enter:
- Wait until the Android OS boots:
- Select “Create/modify partitions”:
- Select “New” to create a new partition:
- Select “Primary” to create a partition that can be used to store a bootable OS:
- Proceed with the size suggested by the partition utility. By default the new partition will cover the entire disk:
- Select the “Bootable” button and press Enter to mark the partition as bootable:
- Finally select “Write” and press Enter to save the partition table to the disk:
- Type “yes” and press Enter to confirm the write:
- Now you can select the newly created partition in the partition list and press OK:
- Choose to format it with the ext3 filesystem:
- Confirm the format operation:
- Choose “Yes” to install the boot loader so that you can boot into the operating system:
- Choose to make the system directory writable as this will allow fixing permission bug described later:
- The Android OS will now be installed. Once the installation completes, choose “Reboot”:
- To avoid booting into the installer again, open VM properties and remove the virtual CD-ROM device:
- Once the OS boots, select your language and press the button to the right to continue:
- If you cannot see the mouse pointer, try toggling the mouse integration in the Input menu:
- Skip the WiFi selection as we will be using a virtual network adapter anyway:
- Enter the user name that will be associated with your virtual OS installation:
- 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:
- 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 su chmod 4750 /system/bin/run-as
Unless 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.
- 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:You can close the shell by pressing Ctrl-D and exit the command prompt window.
- 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:
- Select “Clone a sample from NDK”:
- Select the san-angeles project from the sample list:
- Select the Android OS version that matches the version you installed into VirtualBox and select “x86” as the hardware platform:
- Once the project is created, remove the app-linux.c and app-win32.c files:
- Build the project with Ctrl-Shift-B:
- 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:
- Hit F5 in Visual Studio to start debugging. You will see the san-angeles demo being rendered in the VirtualBox window:
- Set a breakpoint somewhere in the code (e.g. in the camTrack() function). It should hit as soon as the function gets executed:
- 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: