码迷,mamicode.com
首页 > 移动开发 > 详细

Co-Debugging JNI with Android Studio and Visual Studio

时间:2015-11-17 00:07:34      阅读:376      评论:0      收藏:0      [点我收藏+]

标签:

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:

        Note that the name of the function should match the name of your package and activity!
      • Android.mk:

      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:

      and the following inside the onCreate() method:

      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

标签:

原文地址:http://www.cnblogs.com/qingchen1984/p/4970426.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!