{"id":81,"date":"2014-12-20T11:16:29","date_gmt":"2014-12-20T11:16:29","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=81"},"modified":"2015-08-05T12:36:45","modified_gmt":"2015-08-05T19:36:45","slug":"android-astudio","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/android\/astudio\/","title":{"rendered":"Co-Debugging JNI with Android Studio and Visual Studio"},"content":{"rendered":"<p><strong>Warning! This tutorial uses outdated versions of VisualGDB and Android Studio. Please follow the new <a href=\"http:\/\/visualgdb.com\/tutorials\/android\/astudio\/flavors\">Gradle Flavors tutorial<\/a> to learn how to use VisualGDB 5.0 with Android Studio 1.3.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>This tutorial shows how to debug a sample Android app with native code with both Android Studio and Visual Studio:<\/p>\n<ul>\n<li><strong>Android Studio<\/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 will be attached to the application at the same time without interfering with one another.<\/p>\n<p>Before you begin, please install <a href=\"\/download\"> VisualGDB 4.<span class=\"auto-style1\">3<\/span><\/a> or later and Android Studio.<\/p>\n<ol>\n<li>Launch Android Studio. Begin creating a new project:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/01-studioprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/01-studioprj.png\" alt=\"01-studioprj\" width=\"679\" height=\"502\" \/><\/a><\/li>\n<li>Specify application name and domain:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/02-appname.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/02-appname.png\" alt=\"02-appname\" width=\"468\" height=\"527\" \/><\/a><\/li>\n<li>On the next wizard page specify the platform:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/03-platform.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/03-platform.png\" alt=\"03-platform\" width=\"468\" height=\"527\" \/><\/a><\/li>\n<li>On the activity selection page select &#8220;Fullscreen activity&#8221;:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/04-activity.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/04-activity.png\" alt=\"04-activity\" width=\"468\" height=\"527\" \/><\/a><\/li>\n<li>Proceed with the default activity name:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/05-actname.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/05-actname.png\" alt=\"05-actname\" width=\"558\" height=\"518\" \/><\/a><\/li>\n<li>Once you press &#8220;Finish&#8221;, Android Studio will create your project:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/06-project.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/06-project.png\" alt=\"06-project\" width=\"700\" height=\"500\" \/><\/a><\/li>\n<li>Now it&#8217;s time to add some native code. Create a <strong> jni<\/strong> folder under the <strong>app<\/strong> folder (switch to the Project view from the Android view on the Project pane) and add 2 files with the following content:\n<ul>\n<li>hello.c:\n<pre class=\"\">#include&lt;string.h&gt;\r\n#include &lt;jni.h&gt;\r\n#include&lt;stdio.h&gt;\r\n\r\nint s_ButtonPressCounter = 0;\r\n\r\njstring\r\nJava_com_example_virtual_myapplication_FullscreenActivity_stringFromJNI(JNIEnv* env, jobject thiz)\r\n{\r\n    char szBuf[512];\r\n    sprintf(szBuf, \"%d\", s_ButtonPressCounter++);\r\n\r\n    jstring str = (*env)-&gt;NewStringUTF(env, szBuf);\r\n    return str;\r\n}<\/pre>\n<p><span class=\"warning\">Note that the name of the function should match the name of your package and activity!<\/span><\/li>\n<li>Android.mk:\n<pre>LOCAL_PATH := $(call my-dir)\r\n\r\ninclude $(CLEAR_VARS)\r\nLOCAL_MODULE := HelloLibrary\r\n#VisualGDBAndroid: AutoUpdateSourcesInNextLine\r\nLOCAL_SRC_FILES := hello.c\r\ninclude $(BUILD_SHARED_LIBRARY)<\/pre>\n<\/li>\n<\/ul>\n<p>Ensure that the jni folder is on the same level as the src folder:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/07-jni.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/07-jni.png\" alt=\"07-jni\" width=\"700\" height=\"565\" \/><\/a><span class=\"warning\">Do not use the default Android Studio&#8217;s JNI folder! As of version 1.0.1 the normal JNI integration is broken and will result in various build and debug problems. If you put your JNI folder on the same level as the src folder, VisualGDB will handle the JNI build and resolve all problems automatically.<\/span><\/li>\n<li>Add the following code to FullscreenActivity.java:\n<pre>public native String stringFromJNI();\r\n\r\nstatic {\r\n    System.loadLibrary(\"HelloLibrary\");\r\n}<\/pre>\n<p>and the following inside the onCreate() method:<\/p>\n<pre class=\"\">final Button button = (Button)findViewById(R.id.dummy_button);\r\nbutton.setOnClickListener(new View.OnClickListener() {\r\n   public void onClick(View v) {\r\n       String str = stringFromJNI();\r\n       button.setText( str);\r\n   }\r\n});<\/pre>\n<p>Then build your app and try debugging it:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/08-nolib.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/08-nolib.png\" alt=\"08-nolib\" width=\"700\" height=\"565\" \/><\/a>The loader will report a missing library. This happens because Android Studio (as of October 2014) does not build native libraries automatically. We will fix this in the next step.<\/li>\n<li>Start Visual Studio and create a VisualGDB Android project:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/09-vsprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/09-vsprj.png\" alt=\"09-vsprj\" width=\"700\" height=\"394\" \/><\/a><\/li>\n<li>Select &#8220;Import existing project&#8221;:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/10-import.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-10\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/10-import.png\" alt=\"10-import\" width=\"649\" height=\"616\" \/><\/a><\/li>\n<li>Point the VisualGDB wizard to the location of your Android Studio project:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/11-location.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-11\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/11-location.png\" alt=\"11-location\" width=\"649\" height=\"616\" \/><\/a><\/li>\n<li>Select the targeted platform:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/12-platform.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-12\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/12-platform.png\" alt=\"12-platform\" width=\"649\" height=\"616\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221;. VisualGDB will import your project into Visual Studio. Build it by pressing Ctrl-Shift-B:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/13-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/13-build.png\" alt=\"13-build\" width=\"700\" height=\"716\" \/><\/a><\/li>\n<li>Start an Android emulator or connect a physical device. Put a breakpoint inside the function in the .c file and press F5 to start debugging. Ensure that the &#8220;Debug app startup&#8221; feature is disabled while Android Studio is running in the background:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/14-launch.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-14\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/14-launch.png\" alt=\"14-launch\" width=\"702\" height=\"718\" \/><\/a><\/li>\n<li>Click the center of the screen so that the &#8220;dummy button&#8221; appears. Click the button. Your breakpoint will be triggered:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/15-breakpoint.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-15\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/15-breakpoint.png\" alt=\"15-breakpoint\" width=\"704\" height=\"718\" \/><\/a><\/li>\n<li>With Visual Studio you can debug the C\/C++ part of your app, but not the java part. We will now use Android Studio to debug the Java part simultaneously with the C\/C++ debugging. Stop debugging by pressing Shift-F5. Go to Android studio, put a breakpoint on a call to stringFromJNI() and begin debugging:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/16-astudiobp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-16\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/16-astudiobp.png\" alt=\"16-astudiobp\" width=\"700\" height=\"565\" \/><\/a><\/li>\n<li>\u00a0Once the breakpoint triggers, go back to Visual<br \/>\nStudio and start debugging. VisualGDB will ask if you want<br \/>\nto attach to an existing instance. Select &#8220;attach&#8221; and set a<br \/>\nbreakpoint on the sprintf() line:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/17-attach.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-17\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/17-attach.png\" alt=\"17-attach\" width=\"350\" height=\"242\" \/><\/a><\/li>\n<li>Go to Android Studio and select &#8216;step over&#8217; The VisualGDB breakpoint will trigger. Modify the value of the counter to 99:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/18-counter.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-18\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/18-counter.png\" alt=\"18-counter\" width=\"702\" height=\"718\" \/><\/a><\/li>\n<li>Press F5 to continue debugging. Android Studio will step out of the C function showing the value we entered in Visual Studio:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/19-stepdone.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-19\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/19-stepdone.png\" alt=\"19-stepdone\" width=\"700\" height=\"565\" \/><\/a><\/li>\n<li>Resume the execution of your app. See how the button text is updated with the value we set:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/20-newtext.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-20\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/tutorials\/android\/astudio\/20-newtext.png\" alt=\"20-newtext\" width=\"478\" height=\"348\" \/><\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Warning! This tutorial uses outdated versions of VisualGDB and Android Studio. Please follow the new Gradle Flavors tutorial to learn<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[31,44,32],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/81"}],"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=81"}],"version-history":[{"count":3,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":486,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/81\/revisions\/486"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}