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

Co-Debugging JNI with Android Studio and Visual Studio

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:

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

  1. [Visual Studio]透过Visual Studio 2012的选择性贴上将XML与JSON直接转成对应的类别

    原文:[Visual Studio]透过Visual Studio 2012的选择性贴上将XML与JSON直接转成对应的类别 在开发专案时若碰到要串接服务或是他人的API,常常避免不了都要面对XML或 ...

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

    SQL Server 并没有代码格式化的工具,对于处理他人编写的长SQL需要手工的格式化是一件麻烦的事情. 推荐SQL Server Management Studio以及Visual Studio下 ...

  3. visual studio 和visual studio code 的区别是什么?

    区别有三: 区别一:含义不一样. Visual Studio(简称VS)是美国微软公司的开发工具包系列产品,是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具.代码 ...

  4. 2.[WP Developer体验Andriod开发]Andriod Studio结合Visual Studio Emulator for Android调试Android App

    0. 工欲善其事必先利其器 上一篇博客对比了一下Android和WinPhnoe的布局容器,后续篇章重点放在Android的开发上了. 说到开发就绕不开调试程序,调试Android App我们有2种选 ...

  5. 2.[Andriod]Andriod Studio结合Visual Studio Emulator for Android调试Android App

    0. 工欲善其事必先利其器 上一篇博客对比了一下Android和WinPhnoe的布局容器,后续篇章重点放在Android的开发上了. 说到开发就绕不开调试程序,调试Android App我们有2种选 ...

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

    不论你是否使用DirectX或Unity来开发全息应用,你都会使用Visual Studio 2015来进行调试和部署应用.在本部分,你将会学习以下内容: 如何通过Visual Studio将你的应用 ...

  7. [Visual Studio] 开启Visual Studio 2012通过右键菜单创建单元测试(Unit Test)

    Visual Studio 2012可以说是迄今为止微软VS开发工具中用户体验最好的产品,无论是速度还是体验以及功能,都非常出色,但是,使用了一段时间后发现有一个之前版本VS都有的功能却在Visual ...

  8. 【Visual Studio】Visual Studio对CLR异常的特殊支持

    Visual Studio 对异常进行了特殊的支持,它能够在进行了特殊设置后,使代码中的try catch块失效.也就是说,一个异常在正常情况下应该能够被某个特殊的try catch块捕获,但是Vis ...

  9. 【Visual Studio】Visual Studio 2010 "LNK1123: 转换到 COFF 期间失败: 文件无效或损坏" 的解决方法

    1.将 项目|项目属性|配置属性|连接器|清单文件|嵌入清单 “是”改为“否”. 2.找到 C:\Windows\winsxs\x86_netfx-cvtres_for_vc_and_vb_b03f5 ...

随机推荐

  1. LeetCode OJ 34. Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  2. chapter9_1 协同程序

    协同程序与线程差不多,也就是一条执行序列:有自己独立的栈.局部变量.指令指针,以及和其他协同程序共享的全局变量和其他大部分东西. 两者区别在于:一个多线程的程序可以同时运行几个线程,而协同程序却需要彼 ...

  3. jq选择器对象筛选

    1.选择对象 1).基本 ·#id 根据给定的ID匹配一个元素.例如:$("#id")·element 根据给定的元素名匹配所有元素.例如:$("div")·. ...

  4. Git一些其它的功能

    Git 开始之前我们配置过user.name和user.email.其实还有很多其他的配置项 例如:让Git显示颜色,会让命令输出来更醒目: $ git config --global color.u ...

  5. html5游戏开发框架之lufylegend开源库件学习记录

    下载地址http://lufylegend.com/lufylegend 引用 <script type="text/javascript" src="../luf ...

  6. UI----安健2 UIswitch UIslider

    - (void)viewDidLoad { [super viewDidLoad]; [self buttonswitch]; [self buttonslider]; } -(void)button ...

  7. 01背包dp+并查集 Codeforces Round #383 (Div. 2)

    http://codeforces.com/contest/742/problem/D 题目大意:有n个人,每个人有重量wi和魅力值bi.然后又有m对朋友关系,朋友关系是传递的,如果a和b是朋友,b和 ...

  8. angular中重要指令介绍($eval,$parse和$compile)

    在angular的服务中,有一些服务你不得不去了解,因为他可以说是ng的核心,而今天,我要介绍的就是ng的两个核心服务,$parse和$compile.其实这两个服务讲的人已经很多了,但是100个读者 ...

  9. 关于Webdriver自动化测试时,页面数据与数据库id不一致的处理方式,需要使用鼠标事件

    有时候Web页面需要通过onmouseout事件去动态的获取数据库的数据,在使用Webdriver进行自动化测试的时候,对于页面显示的数据,其在数据库可能会存在一个id或者code,但是id或者cod ...

  10. RPC框架基本原理(二):客户端注册

    客户端的注册流程如下 核心功能主要如下: 1.生成调用远程HSF服务的代理 此代理的效果为生成ServiceMetadata中指定的interface的代理,调用时可将代理转型为服务接口,并进行直接的 ...