下载as –>preference search ‘SDK’–>SDK tools –>make lldb ndk 下载
新建工程 选择c++支持
static {
System.loadLibrary(“native-lib”);
}
public native int com();
alt 回车 自动生成
cmakelists.text中
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# Associated headers in the same location as their source
# file are automatically included.
重点 --- src/main/cpp/native-lib.cpp
重点 --- src/main/cpp/serialport.cpp)
c 和cpp文件都可以
cpp中的方法 要加上 extend “c”
例如
extern “C”
JNIEXPORT jint JNICALL
Java_com_example_sunzl_myapplication_MainActivity_com(JNIEnv *env, jobject instance) {
// TODO
int j = 0;
// TODO
for (int i = 0; i < 10; ++i) {
j+=10;
}
j <<= 2;
return j;
}