Tutorials > Android > Integration with other Tools > Co-debugging JNI with Android Studio and Visual studioco-deb Ugging JNI with Android studio and Visual StudioDecember, Android , Android Studio, NDK
warning! This tutorial uses outdated versions of VISUALGDB and Android Studio. Please follow the new Gradle flavors Tutorial-Learn how to use VISUALGDB 5.0 with Android Studio 1.3.
This tutorial shows how to debug a sample Android app with native code with both Android studio and Visual Studio:
- Android Studio would be used to debug the Java part of the project
- Visual Studio would be used to debug the C + + part of the project
Both debuggers is attached to the application at the same time without interfering with one another.
Before you begin, please install VISUALGDB 4. 3 or later and Android Studio.
- Launch Android Studio. Begin Creating a new project:
- Specify application name and domain:
- On the next wizard page specify the platform:
- On the Activity Selection page select "Fullscreen activity":
- Proceed with the default activity name:
- Once you press "Finish", Android Studio would create your project:
- Now it's time to add some native code. Create AJNIfolder under theappfolder (switch to the project view from the Android view in the Project pane) and add 2 files with the following content:
- HELLO.C:
<textarea class="crayon-plain print-no" style="-moz-tab-size: 4; font-size: 12px ! important; line-height: 15px ! important; z-index: 0; opacity: 0; overflow: hidden;" readonly="" data-settings="dblclick">#include <string.h> #include <jni.h> #include <stdio.h>int s_buttonpresscounter = 0;jstringjava_ Com_example_virtual_myapplication_fullscreenactivity_stringfromjni (jnienv* env, Jobject thiz) {char szBuf[512]; sprintf (Szbuf, "%d", s_buttonpresscounter++); jstring str = (*env)->newstringutf (env, SZBUF); return str;}</textarea>
123456789101112131415 |
#include <string.h>#include <jni.h>#include <stdio.h> int s_buttonpresscounter = 0; jstringjava_com_example_virtual_myapplication_fullscreenactivity_stringfromjni(jnienv* env , jobject thiz) { Char szbuf[+]; sprintf(szbuf, "%d", s_buttonpresscounter+ +); jstring str = (*env)-Newstringutf(env, C16>szbuf); return str; } |
Note that the name of the function should match the name of the Your package and activity!
- ANDROID.MK:
<textarea class="crayon-plain print-no" style="-moz-tab-size: 4; font-size: 12px ! important; line-height: 15px ! important; z-index: 0; opacity: 0; overflow: hidden;" readonly="" data-settings="dblclick">Local_path: = $ (call My-dir) include $ (clear_vars) Local_module: = hellolibrary#visualgdbandroid: Autoupdatesourcesinnextlinelocal_src_files: = Hello.cinclude $ (build_shared_library)</textarea>
1234567 |
local_path := $(call my-dir) include $(clear_vars) local_module := hellolibrary #VisualGDBAndroid: Autoupdatesourcesinnextlinelocal_src_files := Hello. C include $(build_shared_library) |
Ensure that the Jni folder was on the same level as the SRC folder: does not use thedefault Android Studio ' s JNI folder! As of version 1.0.1 the normal JNI integration is broken and would result in various build and debug problems. If you put your Jni folder on the same level as the SRC folder, visualgdb'll handle the JNI build and resolve all proble Ms automatically.
- ADD the following code to Fullscreenactivity.java:<textarea class= "Crayon-plain print-no" style= "-moz-tab-size:4"; Font-size:12px! Important line-height:15px! Important z-index:0; opacity:0; Overflow:hidden; "ReadOnly data-settings=" DblClick ">public native String stringfromjni (); static {System.loadlibrary ( "Hellolibrary");} </textarea>
12345 |
public native< Span class= "crayon-h" > string stringFromJNI< Span class= "Crayon-sy" > ( static { systemloadlibrary ( "Hellolibrary" |
And the following inside the OnCreate () method:
<textarea class="crayon-plain print-no" style="-moz-tab-size: 4; font-size: 12px ! important; line-height: 15px ! important; z-index: 0; opacity: 0; overflow: hidden;" readonly="" data-settings="dblclick">Final Button button = (button) Findviewbyid (R.id.dummy_button); Button.setonclicklistener (new View.onclicklistener () {public void OnClick (View v) {String str = STRINGFROMJNI (); Button.settext (str); }});</textarea>
1234567 |
final button button< Span class= "crayon-h" > = ( button) findviewbyid (r. Id. Dummy_button button. Setonclicklistener(new View). Onclicklistener() { Public void onClick(View v) { String str = stringfromjni(); button. SetText( str); }}); |
Then build your app and try debugging It:the loader would report a missing library. This happens because Android Studio (as of October) does not build native libraries automatically. We'll fix this in the next step.
- Start Visual Studio and create a visualgdb Android project:
- Select "Import Existing Project":
- Point the VISUALGDB Wizard to the location of your Android Studio project:
- Select the targeted platform:
- Press "Finish". VISUALGDB would import your project into Visual Studio. Build it by pressing Ctrl-shift-b:
- 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 "Debug app startup" feature are disabled while Android Studio was running in the background:
- Click the center of the screen so, the "dummy button" appears. Click the button. Your breakpoint'll be triggered:
- With Visual Studio You can debug the C + + part of the Your app, and not the Java part. We'll now use the Android Studio to debug the Java part simultaneously with the C + + debugging. Stop debugging by pressing SHIFT-F5. Go to Android Studio, put a breakpoint in a call to Stringfromjni () and begin debugging:
- Once The breakpoint triggers, go back to Visual
Studio and start Debugging. Visualgdb would ask if you want
To attach to an existing instance. Select "Attach" and set a
Breakpoint on the sprintf () line:
- Go to Android Studio and select ' Step over ' the VISUALGDB breakpoint would trigger. Modify the value of the counter to 99:
- Press F5 to continue debugging. Android Studio would step out of the C function showing the value we entered in Visual Studio:
- Resume the execution of your app. See how the button text was updated with the value we set:
Co-debugging JNI with Android studio and Visual Studio