{"id":85,"date":"2014-02-04T17:39:32","date_gmt":"2014-02-04T17:39:32","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=85"},"modified":"2015-07-19T08:20:37","modified_gmt":"2015-07-19T15:20:37","slug":"android-codebug","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/android\/codebug\/","title":{"rendered":"Co-debugging hello-jni with Eclipse and Visual Studio"},"content":{"rendered":"<p>This tutorial shows how to debug the hello-jni project with both Eclipse and Visual Studio:<\/p>\n<ul>\n<li><strong>Eclipse<\/strong> will be used to debug the <strong>Java<\/strong> part of the project<\/li>\n<li><strong>Visual Studio<\/strong> will be used to debug the <strong>C\/C++<\/strong> part of the project<\/li>\n<\/ul>\n<p>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&#8217;ll use the <strong>hello-jni<\/strong> project provided by Android NDK.<\/p>\n<p>Before you begin, please install <a href=\"\/download\"> VisualGDB 2.0<\/a> or later (ensure that &#8220;Android Edition&#8221; is checked in the installer) and Eclipse with the ADT plugin.<\/p>\n<ol>\n<li>First, copy the <strong> &lt;NDK_directory&gt;\\samples\\hello-jni<\/strong> folder to a new location (e.g. <strong>e:\\projects\\android\\hello-jni<\/strong>). We will use this copy for debugging leaving the original sample untouched.<\/li>\n<li>Start Eclipse. <strong>Select File-&gt;New-&gt;Project. <\/strong>Then select <strong>Android-&gt;Android Project<\/strong>.<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/01-newprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/01-newprj.png\" alt=\"01-newprj\" width=\"525\" height=\"500\" \/><\/a><\/li>\n<li>Select &#8220;create project from existing source&#8221;. Specify the project name (hello-jni) and the location where you have copied the hello-jni sample (e.g. <strong> e:\\projects\\android\\hello-jni<\/strong>):<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/02-importprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/02-importprj.png\" alt=\"02-importprj\" width=\"525\" height=\"447\" \/><\/a><\/li>\n<li>In the next step select the &#8220;Android 1.5&#8221; platform:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/03-android3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/03-android3.png\" alt=\"03-android3\" width=\"525\" height=\"447\" \/><\/a><\/li>\n<li>Ensure that the application name and the package name match the names specified in AndroidManifest.xml:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/04-prjnames.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/04-prjnames.png\" alt=\"04-prjnames\" width=\"525\" height=\"447\" \/><\/a><\/li>\n<li>Press <strong>Finish<\/strong> to import the project to Eclipse.<\/li>\n<li>From now on you can build and debug the Java part of the example. Now let&#8217;s setup the C\/C++ part.<\/li>\n<li>Open Visual Studio, select <strong>File-&gt;New-&gt;Project<\/strong>. Select Visual <strong>VisualGDB-&gt;Android Project Wizard<\/strong>. Enter an arbitrary location for the Visual Studio project. The project folder won&#8217;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 <strong> HelloJniWrapper<\/strong> subdirectory in the project directory.<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/05-newprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/05-newprj.png\" alt=\"05-newprj\" width=\"700\" height=\"394\" \/><\/a><\/li>\n<li>In VisualGDB wizard select &#8220;<strong>Import existing project<\/strong>&#8220;:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/06-importprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/06-importprj.png\" alt=\"06-importprj\" width=\"700\" height=\"616\" \/><\/a><\/li>\n<li>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 &#8220;build the project in its original location&#8221; is selected.<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/07-prjpath.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/07-prjpath.png\" alt=\"07-prjpath\" width=\"700\" height=\"543\" \/><\/a><\/li>\n<li>On the next step select the same target platform you specified when creating Eclipse project.<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/08-platform.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/08-platform.png\" alt=\"08-platform\" width=\"700\" height=\"543\" \/><\/a><\/li>\n<li>Press Finish to create the Visual Studio project.<\/li>\n<li>Build the C part of the project by selecting Build-&gt;Build solution in Visual Studio.<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/09-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/09-build.png\" alt=\"09-build\" width=\"681\" height=\"621\" \/><\/a><\/li>\n<li>VisualGDB will also build the Java code using Ant, however, you can ignore it when using Eclipse.<\/li>\n<li>Start the AVD manager (Android simulator):<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/10-avd.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-10\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/10-avd.png\" alt=\"10-avd\" width=\"683\" height=\"623\" \/><\/a><\/li>\n<li>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.<\/li>\n<li>Go to Eclipse. Open <strong>HelloJni.java<\/strong> and set a breakpoint on the line containing the stringFromJNI() call.<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/11-bkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-11\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/11-bkpt.png\" alt=\"11-bkpt\" width=\"678\" height=\"624\" \/><\/a><\/li>\n<li>Start debugging by clicking at the arrow to the right from the bug icon and selecting &#8220;hello-jni&#8221;:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/12-dbg.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-12\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/12-dbg.png\" alt=\"12-dbg\" width=\"678\" height=\"624\" \/><\/a><\/li>\n<li>If everything goes well, the app will be installed to the simulator and your breakpoint will be trigerred.<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/13-jhit.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/13-jhit.png\" alt=\"13-jhit\" width=\"675\" height=\"624\" \/><\/a>If anything goes wrong here, try restarting the debugging, rebuilding the app and restarting Eclipse.<\/li>\n<li>Now it&#8217;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 <strong>Android-&gt;Debug Android App<\/strong>.<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/14-cdbg.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-14\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/14-cdbg.png\" alt=\"14-cdbg\" width=\"682\" height=\"621\" \/><\/a><\/li>\n<li>VisualGDB will notice that the app is already running (as it&#8217;s being debugged by Eclipse) and will show a prompt:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/15-already.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-15\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/15-already.png\" alt=\"15-already\" width=\"350\" height=\"220\" \/><\/a><\/li>\n<li>As we want to co-debug the code with Eclipse, please select &#8220;<strong>Attach to the running instance<\/strong>&#8220;. You can also set the &#8220;Remember my decision&#8221; flag.<\/li>\n<li>If you are using the simulator, it might take some time for C\/C++ debugger to attach. VisualGDB will show a progress window:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/16-start.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-16\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/16-start.png\" alt=\"16-start\" width=\"330\" height=\"200\" \/><\/a><\/li>\n<li>Once the debugger is attached, your Visual Studio window will look similar to this:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/17-cdbgstart.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-17\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/17-cdbgstart.png\" alt=\"17-cdbgstart\" width=\"681\" height=\"621\" \/><\/a><\/li>\n<li>Go back to Eclipse and select &#8220;Step over&#8221; so that the <strong>stringFromJNI()<\/strong> function gets executed.<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/18-stepover.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-18\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/18-stepover.png\" alt=\"18-stepover\" width=\"675\" height=\"624\" \/><\/a><\/li>\n<li>The Visual Studio breakpoint will be triggered:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/19-cbkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-19\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/19-cbkpt.png\" alt=\"19-cbkpt\" width=\"681\" height=\"621\" \/><\/a><\/li>\n<li>You can use the Visual Studio debugger to explore local variable values (e.g. hover the mouse over <strong>thiz<\/strong>). When done, just press F5 to continue.<\/li>\n<li>Eclipse will complete the <strong>Step Over <\/strong> command and stop:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/20-jcont.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-20\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/20-jcont.png\" alt=\"20-jcont\" width=\"675\" height=\"624\" \/><\/a><\/li>\n<li>Press <strong>Resume <\/strong>in Eclipse to continue program execution. You can switch to the simulator window and ensure that the string is displayed correctly:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/21-sim.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-21\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/codebug\/21-sim.png\" alt=\"21-sim\" width=\"700\" height=\"647\" \/><\/a><\/li>\n<li>To stop debugging, disconnect the Eclipse debugging session and then terminate the Visual Studio session by pressing Shift+F5.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to debug the hello-jni project with both Eclipse and Visual Studio: Eclipse will be used to<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[87],"tags":[31,45],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/85"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=85"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":173,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/85\/revisions\/173"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}