Debugging Android native C/C++ applications and libraries

This tutorial describes how to debug the hello-neon application provided with the Android Native Development Kit (NDK). It uses Eclipse with C/C++ Development Tooling (CDT), the Android Software Development Kit (SDK) Platform 2.2 and the Android emulator as the target.

It does not describe how to install any of the Android tools. See the Android Developers website for more information.

Note

Use of the Android development kits are subject to their own terms and conditions.

Show/hidePrerequisites

Before you can debug an Android package containing native C/C++ code you must:

  1. Download and install the Android SDK tools and required platforms. This enables you to build Java applications together with any native C/C++ code into an Android package with a .apk file extension.

    Note

    This example uses the Android SDK Platform 2.2.

  2. Download and install the Android NDK. This is a companion tool to the Android SDK that enables you to build performance-critical parts of your applications in native code such as C and C++ languages.

    Note

    On Windows, you must also download and install cygwin, including the make package so that you can run the scripts inside the Android NDK.

  3. Update the version of gdbserver in the Android NDK ...\toolchains\...\prebuilt directory with the required version of gdbserver provided by DS-5. You can locate this file by selecting Help → ARM Extras... from the main menu. Ensure that you rename it to gdbserver.

  4. Add the...\android-sdk\platform-tools folder to your PATH environment variable. If it is already configured then you can skip this step.

  5. Set up the Eclipse plug-in for Android:

    1. Launch Eclipse.

    2. Install the DS-5 Eclipse plug-ins.

    3. Install the Android Development Tools (ADT) Eclipse plug-ins.

    4. Select Window → Preferences → Android and click on Browse... to set the location of the Android SDK.

    5. Open the Android SDK and AVD Manager dialog box by selecting Window → Android SDK and AVD Manager.

    6. Expand the Available packages group and add SDK platforms as required. For example, Android SDK Platform Android 2.2.

    7. Create a new Android Virtual Device (AVD) and start the emulator.

Show/hideProcedure

To debug the application:

  1. Launch Eclipse.

  2. Create a new Android project:

    1. Select File → New → Project...

    2. Expand the Android group and select Android Project.

    3. Click Next.

    4. Enter a suitable project name. For example, HelloNeon.

    5. Select Create project from your existing source.

    6. Click Browse... to locate the hello-neon folder.

    7. Click Next.

    8. Select the required Build Target. For example, Android 2.2.

    9. Click Finish.

  3. Ensure that the application builds with debug information. You can do this by:

    1. Open the AndroidManifest.xml file.

    2. Click on the Application tab.

    3. Select true in the Debuggable field.

    4. Save the changes and close the file.

  4. Build the hello-neon source files with debug information using the scripts provided by the Android NDK. This tutorial uses the NDK revision 6b. For example:

    ./ndk-build -C samples/hello-neon NDK_TOOLCHAIN=arm-linux-androideabi-4.4.3 NDK_DEBUG=1
    
  5. In Eclipse, select the Android project and build the HelloNeon.apk.

  6. Connect DS-5 to the application using a gdbserver connection:

    1. Select Debug Configurations... from the Run menu.

    2. Select DS-5 Debugger from the configuration tree and then click on New to create a new configuration. Alternatively you can select an existing DS-5 Debugger configuration and then click on Duplicate from the toolbar.

    3. In the Name field, enter a suitable name for the new configuration. For example, HelloNeon.

    4. Click on the Connection tab, configure a DS-5 Debugger target connection for an Android - Generic platform and select the relevant emulator in the Connections field.

      Figure 15. Typical Connection tab settings for an Android application

      Typical Connection tab settings for an Android application

    5. In the Files tab, click on Workspace... for the Project directory field and select the hello-neon directory. This automatically populates the other fields.

      Figure 16. Typical Files tab settings for an Android application

      Typical Files tab settings for an Android application

    6. Click on the Debugger tab and select Connect only in the Run control panel.

    7. In the Paths panel, click on Workspace... and select the hello-neon directory.

      Figure 17. Typical Debugger tab settings for an Android application

      Typical Debugger tab settings for an Android application

    8. Click on Debug to connect to the target.

  7. Debugging requires the DS-5 Debug perspective. If the Confirm Perspective Switch dialog box opens, click on Yes to switch perspective.

  8. To debug the application, set breakpoints, run, and step as required.

Note

If the application exits before NDK can attach gdbserver to the native library then you might need to add a delay before launching the Java native libraries.

Show/hideSee also

Copyright © 2010, 2011 ARM. All rights reserved.ARM DUI 0478H
Non-ConfidentialID111711