Windows下Eclipse 实现JNI C++/C断点调试

Using the NDK plugin

posted Jul 2, 2012, 7:16 PM by Siva Velusamy
ADT 20 includes an NDK plugin that provides support for building and debugging NDK projects in Eclipse. This document describes how to install and use the NDK plugin.

Installation

The NDK plugin currently works with CDT 7.0.2 or CDT 8.0.2.
  1. Download Eclipse for Java.
  2. Install CDT from Eclipse update site http://download.eclipse.org/tools/cdt/releases/indigo.
  3. Install Android SDK + NDK Plugins from Eclipse update site https://dl-ssl.google.com/android/eclipse/
Using the NDK Plugin

1. First set the path to SDK and NDK:

Eclipse -> Window -> Preferences -> Android -> set path to SDK

Eclipse -> Window -> Preferences -> Android -> NDK -> set path to the NDK


2. Right click on an Android project and select Android Tools -> Add native support.
Note that you will not be able to add native support if the project already has C/C++ nature.

At this point, you will be able to build your applications using Project -> Build All.

Debugging native applications

1. Update your build config to include “NDK_DEBUG = 1”.

Right click project -> properties -> C/C++ Build:

2. Set a breakpoint in your C code.
3. Right click on your project, select Debug As -> Android Native Application

Note: There is a delay of a few seconds between when the activity is launched and when native debugging starts. If your code is already executed by that point, then you won’t see the breakpoint being hit. So either put a breakpoint in code that is called repetitively, or make sure that you call JNI code after you see that ndk-gdb has connected.

Known Issues

1. Eclipse does not automatically find the include paths to all the NDK headers on Windows. This issue will be fixed in the next update (20.0.1) when it is released.
2. Eclipse does not automatically find the include paths with CDT 8.1.0 (Juno). This issue is tracked in Bug33788.\


如果没有NDK选项:
1. Help-->Install New Software... --> Work with 输入 https://dl-ssl.google.com/android/eclipse/。
       2.在打开的窗口出现的列表中会出现Developer tools,将其全选。
       3.点击Next。若有提示就点击OK,一路下去。最后提示你重启Eclipse(ADT)。
重启后发现 Window->References->Android 里面有NDK设置选项了。

1.) 在项目根目录下创建jni目录,下面是要放进去的第一个文件Android.mk LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := main_jni #LOCAL_CFLAGS := LOCAL_SRC_FILES := main.cpp #LOCAL_LDLIBS := include $(BUILD_SHARED_LIBRARY) 2.) 在jni目录创建main.cpp #include using namespace std; #ifdef __cplusplus extern "C" { #endif class Test { public: Test(){}; ~Test(){}; int SomeFunc() { return 20140522; } }; jint Java_com_yxiaolv_testjni_MainActivity_SomeFunc(JNIEnv *env, jobject thiz) //红色部分必须和你java类包名一致 { Test *test = new Test(); return test->SomeFunc(); } #ifdef __cplusplus } #endif //将下面第3.)至第5.)步的代码片段放到eclipse创建android项目时自动产生的MainAcitivity.java中就可以了 3.) 在Java Activity代码中添加对这个jni的调用 (本例中是MainActivity.java ): static { System.loadLibrary("main_jni"); } 4.) 在调用者Activity中定义函数 native int SomeFunc(); 5.) 在activity中调用 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = new TextView(this); int i = SomeFunc(); tv.setText( String.valueOf(i) ); setContentView(tv); } //下面的第6.)和第7.)步可以被集成的.cproject 和在.project中添加plugin之后一键编译,不用单独起dos窗口了。由于没有找到生成.cproject的工具,只能手写,所以本文不使用该方法,以后找到了好的方法再补充。委屈 6.) 在dos窗口中,cd到项目路径下,运行ndk-build 命令,该命令会自动搜索该路径子目录下的native项目,进行编译。 (Note: 编译完成后不要忘记了刷新项目)。 7.) 重新编译和部署项目,就可以看到已经包含了.so的apk运行在目标机上了。 摘自 http://stackoverflow.com/questions/13654678/how-compile-c-project-via-android-ndk 1). 如果不知道怎样安装和使用windows版的NDK,可以参考 http://blog.csdn.net/do_script/article/details/26478583
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值