Co-Debugging JNI with Android Studio and Visual Studio

Tutorials > Android > Integration with other tools > Co-Debugging JNI with Android Studio and Visual Studio

Co-Debugging JNI with Android Studio and Visual Studio

December 20, 2014 android, android studio, ndk

Warning! This tutorial uses outdated versions of VisualGDB and Android Studio. Please follow the new Gradle Flavors tutorial to 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 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 will be 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 will create your project:
    7. Now it’s time to add some native code. Create a jni folder under the app folder (switch to the Project view from the Android view on the Project pane) and add 2 files with the following content:
      • hello.c:

        #include<string.h>
        #include <jni.h>
        #include<stdio.h>

        int s_ButtonPressCounter = 0;

        jstring
        Java_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;
        }

        1

        2

        3

        4

        5

        6

        7

        8

        9

        10

        11

        12

        13

        14

        15

        #include<string.h>

        #include <jni.h>

        #include<stdio.h>

        int s_ButtonPressCounter = 0;

        jstring

        Java_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;

        }

        Note that the name of the function should match the name of your package and activity!

      • Android.mk:

        LOCAL_PATH := $(call my-dir)

        include $(CLEAR_VARS)
        LOCAL_MODULE := HelloLibrary
        #VisualGDBAndroid: AutoUpdateSourcesInNextLine
        LOCAL_SRC_FILES := hello.c
        include $(BUILD_SHARED_LIBRARY)

        1

        2

        3

        4

        5

        6

        7

        LOCAL_PATH := $(call my-dir)

        include $(CLEAR_VARS)

        LOCAL_MODULE := HelloLibrary

        #VisualGDBAndroid: AutoUpdateSourcesInNextLine

        LOCAL_SRC_FILES := hello.c

        include $(BUILD_SHARED_LIBRARY)

      Ensure that the jni folder is on the same level as the src folder:Do not use the default Android Studio’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.

    8. Add the following code to FullscreenActivity.java:

      public native String stringFromJNI();

      static {
      System.loadLibrary("HelloLibrary");
      }

      1

      2

      3

      4

      5

      public native String stringFromJNI();

      static {

      System.loadLibrary("HelloLibrary");

      }

      and the following inside the onCreate() method:

      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);
      }
      });

      1

      2

      3

      4

      5

      6

      7

      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);

      }

      });

      Then build your app and try debugging it: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.

    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 will 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 is disabled while Android Studio is running in the background:
    15. Click the center of the screen so that the “dummy button” appears. Click the button. Your breakpoint will be triggered:
    16. 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:
    17. Once the breakpoint triggers, go back to Visual
      Studio and start debugging. VisualGDB will 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 will trigger. Modify the value of the counter to 99:
    19. Press F5 to continue debugging. Android Studio will 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 is updated with the value we set:
时间: 2024-10-10 01:09:02

Co-Debugging JNI with Android Studio and Visual Studio的相关文章

推荐SQL Server Management Studio以及Visual Studio下的免费的插件 ApexSQL Complete

SQL Server 并没有代码格式化的工具,对于处理他人编写的长SQL需要手工的格式化是一件麻烦的事情. 推荐SQL Server Management Studio以及Visual Studio下的插件 ApexSQL Complete,ApexSQLRefactor,ApexSQLSearch ApexSQL Complete 跟踪的已经关闭大概的标签(需要在设置中启用才可生效的,会消耗一定的硬盘空间,推荐给开发狂人用,其实我举得还是标签少点比较好) 多个剪贴板 根据代码结构索引(可以少用

IDE0006 Error running Xamarin Android project in Visual Studio

这个报错一般发生在刚创建了一个cross-platform时候发生: 解决方法: 在解决方案上右击--管理解决方案的nuget程序包: 选择更新标签--勾选xamarin.form--然后点击更新按钮(其他的不要勾选): 上一步需要些时间,耐心等待更新完成,之后关闭visual studio,重启visualstudio: 接着在解决方案上右击--清理解决方案和重新生成解决方案: 在Android那个项目下找到引用点击,找到System.ObjectModel这个然后右击移除 完成:

HoloLens开发手记 - 使用Visual Studio Using Visual Studio

不论你是否使用DirectX或Unity来开发全息应用,你都会使用Visual Studio 2015来进行调试和部署应用.在本部分,你将会学习以下内容: 如何通过Visual Studio将你的应用部署到HoloLens上 Visual Studio 2015如何使用内置的HoloLens模拟器 如何去调试全息应用 先决条件 Prerequisites 安装好开发工具 对于2D应用,在Visual Studio 2015 Update 1及更高版本中创建一个UWP项目.或者开始使用Unity来

【Visual Studio】Visual Studio对异常的支持

Visual Studio 对异常进行了特殊的支持.在当前已打开一个解决方法的前提下,从“调试”菜单选择“异常”: 上面的对话框显示了Visual Studio能识别的不同类型的异常,上面包含是异常已有的异常类型,如果定义了自己的异常,那么可以点击“点击”按钮,添加自己的异常. 每一种异常后面都有两个复选框,分别是“引发”.“用户未处理”.如果勾选了“引发”对话框,那么调试器就会在抛出该异常时中断,注意,在中断时调试器还没有去查找任何匹配的catch块.如果异常类型的对话框没有勾选,那么只有在该

News: Visual Studio Code support debugging Linux Apps

http://arstechnica.com/information-technology/2015/11/visual-studio-now-supports-debugging-linux-apps-code-editor-now-open-source/ ------------------------ Developers can now debug apps running on Linux servers or IoT devices from the comfort of Visu

Introducing Visual Studio&rsquo;s Emulator for Android

原文地址 Microsoft released Visual Studio 2015 Preview this week and with it you now have options for Android development: C++, Cordova, and C# with Xamarin. When choosing one of those Android development options, Visual Studio will also install the bran

Introducing Visual Studio’s Emulator for Android

visual studio 2015支持Android开发了. Microsoft released Visual Studio 2015 Preview this week and with it you now have options for Android development. When choosing one of those Android development options, Visual Studio will also install the brand new Vi

Xamarin C# Android for Visual Studio 平台安装笔记参考

Xamarin是Mono创始人Miguel de Icaza创建的公司,旨在让开发者可以用C#编写iOS, Android, Mac应用程序,也就是跨平台移动开发. 简介 Xamarin是基于Mono的平台,目前主要有以下产品(更具体请见:http://xamarin.com/products): Xamarin Studio:IDE,是从原来的MonoDevelop改名而来.现在从MonoDevelop官方网站下载的其实也是Xamarin Studio:http://monodevelop.c

使用Visual Studio进行 Android开发的十大理由

[原文发表地址]Top 10 reasons to use Visual Studio for C++ Android Development! Visual Studio: C++跨平台的移动解决方案 Visual Studio (下载地址) 正在迅速成为一个跨平台的C++IDE.我们的目标是让Visual Studio成为您研发C++跨平台代码的选择,无论您的目标是Windows (UWP), Android, iOS, Linux, Xbox, PlayStation, Marmalade