Co-debugging hello-jni with Eclipse and Visual Studio

This tutorial shows how to debug the hello-jni project with both Eclipse and Visual Studio:

  • Eclipse will be used to debug the Java part of the project
  • Visual Studio will be used to debug the C/C++ part of the project

Both debuggers (Eclipse and Visual Studio) will be attached to the application at the same time allowing you to debug both Java and C++ code simultaneously. For simplicity we’ll use the hello-jni project provided by Android NDK.

Before you begin, please install VisualGDB 2.0 or later (ensure that “Android Edition” is checked in the installer) and Eclipse with the ADT plugin.

  1. First, copy the <NDK_directory>\samples\hello-jni folder to a new location (e.g. e:\projects\android\hello-jni). We will use this copy for debugging leaving the original sample untouched.
  2. Start Eclipse. Select File->New->Project. Then select Android->Android Project.01-newprj
  3. Select “create project from existing source”. Specify the project name (hello-jni) and the location where you have copied the hello-jni sample (e.g. e:\projects\android\hello-jni):02-importprj
  4. In the next step select the “Android 1.5” platform:03-android3
  5. Ensure that the application name and the package name match the names specified in AndroidManifest.xml:04-prjnames
  6. Press Finish to import the project to Eclipse.
  7. From now on you can build and debug the Java part of the example. Now let’s setup the C/C++ part.
  8. Open Visual Studio, select File->New->Project. Select Visual VisualGDB->Android Project Wizard. Enter an arbitrary location for the Visual Studio project. The project folder won’t contain any source files. Instead it will link to the hello-jni source code you have copied in the first step. In this example we create the HelloJniWrapper subdirectory in the project directory.05-newprj
  9. In VisualGDB wizard select “Import existing project“:06-importprj
  10. In the next step specify the location where you have copied the hello-jni code (step 1). It should be the same location you used when creating the Eclipse project. Ensure that the “build the project in its original location” is selected.07-prjpath
  11. On the next step select the same target platform you specified when creating Eclipse project.08-platform
  12. Press Finish to create the Visual Studio project.
  13. Build the C part of the project by selecting Build->Build solution in Visual Studio.09-build
  14. VisualGDB will also build the Java code using Ant, however, you can ignore it when using Eclipse.
  15. Start the AVD manager (Android simulator):10-avd
  16. Create a virtual Android device. It is recommended to use Android 2.3.3 or later. Start the newly created virtual device and wait till it starts up.
  17. Go to Eclipse. Open HelloJni.java and set a breakpoint on the line containing the stringFromJNI() call.11-bkpt
  18. Start debugging by clicking at the arrow to the right from the bug icon and selecting “hello-jni”:12-dbg
  19. If everything goes well, the app will be installed to the simulator and your breakpoint will be trigerred.13-jhitIf anything goes wrong here, try restarting the debugging, rebuilding the app and restarting Eclipse.
  20. Now it’s the time to connect a native (C/C++) debugger. Go to Visual Studio, set a breakpoint on the line creating the string and select Android->Debug Android App.14-cdbg
  21. VisualGDB will notice that the app is already running (as it’s being debugged by Eclipse) and will show a prompt:15-already
  22. As we want to co-debug the code with Eclipse, please select “Attach to the running instance“. You can also set the “Remember my decision” flag.
  23. If you are using the simulator, it might take some time for C/C++ debugger to attach. VisualGDB will show a progress window:16-start
  24. Once the debugger is attached, your Visual Studio window will look similar to this:17-cdbgstart
  25. Go back to Eclipse and select “Step over” so that the stringFromJNI() function gets executed.18-stepover
  26. The Visual Studio breakpoint will be triggered:19-cbkpt
  27. You can use the Visual Studio debugger to explore local variable values (e.g. hover the mouse over thiz). When done, just press F5 to continue.
  28. Eclipse will complete the Step Over command and stop:20-jcont
  29. Press Resume in Eclipse to continue program execution. You can switch to the simulator window and ensure that the string is displayed correctly:21-sim
  30. To stop debugging, disconnect the Eclipse debugging session and then terminate the Visual Studio session by pressing Shift+F5.