Co-debugging JNI with Android studio and Visual Studio

Source: Internet
Author: User
Tags sprintf

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.

    1. Launch Android Studio. Begin Creating a new project:
    2. Specify application name and domain:
    3. On the next wizard page specify the platform:
    4. On the Activity Selection page select "Fullscreen activity":
    5. Proceed with the default activity name:
    6. Once you press "Finish", Android Studio would create your project:
    7. 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.

    8. 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.

    9. Start Visual Studio and create a visualgdb Android project:
    10. Select "Import Existing Project":
    11. Point the VISUALGDB Wizard to the location of your Android Studio project:
    12. Select the targeted platform:
    13. Press "Finish". VISUALGDB would import your project into Visual Studio. Build it by pressing Ctrl-shift-b:
    14. 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:
    15. Click the center of the screen so, the "dummy button" appears. Click the button. Your breakpoint'll be triggered:
    16. 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:
    17. 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:
    18. Go to Android Studio and select ' Step over ' the VISUALGDB breakpoint would trigger. Modify the value of the counter to 99:
    19. Press F5 to continue debugging. Android Studio would step out of the C function showing the value we entered in Visual Studio:
    20. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.