eclipse中创建NDK工程
本文记录如何创建NDK工程
- eclipse中创建NDK工程
创建普通的工程。
工程上右键选择Android tools ->Add Native Support,此时会出项JNI目录,
编写jni目录下面的.cpp文件
#include <jni.h>
#include <string.h>
extern "C" {
JNIEXPORT jstring JNICALL
Java_com_example_dreamhouse_MainActivity_getAddress(JNIEnv* env,jclass thiz)
{
return env -> NewStringUTF("903");
}
}
若为.c文件,则代码为:
#include <jni.h>
#include <string.h>
jstring
Java_com_example_dreamhouse_MainActivity_getAddress(JNIEnv* env,jobject thiz)
{
return (*env) -> NewStringUTF(env,"903");
}
注:编辑的时候,可以按ALT+/进行快速补齐代码。
在MainActivity.java文件中导入库
static {
System.loadLibrary(Dreamhouse);
}
public native String getAddress();
此时可以在CPP文件中可能会提示找不到头文件,设置项目属性,Properties->C/C++ General->Paths and Symbols,设置头文件的位置。
然后编译运行即可。
还有一种方法相当于Add Native Support,就是添加一个Builder