完成上述步骤就已经配置好了NDK,我们来看看比一般的项目多了哪些东西.
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ELqGtcOA-1572240983910)(http://olg7c0d2n.bkt.clouddn.com/18-9-20/55579079.jpg)]
1. cpp文件夹
很明显,这里面是存放cpp源文件的.
#include <jni.h>
#include
//这里的jstring表示的是返回值,对应于Java中的String
extern “C” JNIEXPORT jstring
JNICALL
Java_com_xfhy_ndkdemo_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = “Hello from C++”;
return env->NewStringUTF(hello.c_str());
}
-
必须引入jni.h头文件,因为下面的JNIEnv,jstring和jobject都是里面的.(jni.h文件定义了JNI(Java Native Interface)所支持的类型与接口。通过预编译命令可以支持C和C++。jni.h文件还依赖jni_md.h文件,jni_md.h文件定义了机器相关的jbyte, jint和jlong对应的本地类型。)
-
JNIEXPORT和JNICALL这两个宏(被定义在jni.h)确保这个函数在本地库外可见,并且C编译器会进行正确的调用转换.
-
extern "C"
的主要作用是为了能够正确实现C++代码调用其他C语言代码. -
这里的jstring表示的是返回值,对应于Java中的String
-
注意
Java_com_xfhy_ndkdemo_MainActivity_stringFromJNI
,这里表示的是Java+类的全名+方法名,必须按照这个格式声明方法.(这个方法对应的java代码如下,平时我们写了一个native方法,可以直接按alt+enter生成cpp这边的方法定义,真是太方便了,哈哈)
public native String stringFromC();
Java和Jni的类型对照表
引用类型对照表
2. CMakeLists.txt
多了个txt文件,看看里面都有啥东西
cmake版本
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library. lib的名称
native-lib
Sets the library as a shared library.
SHARED
Provides a relative path to your source file(s). 源文件
native-lib.cpp )
find_library( # Sets the name of the path variable.
log-lib
Specifies the name of the NDK library that
you want CMake to locate.
log )
target_link_libraries( # Specifies the target library.
native-lib
Links the target library to the log library
included in the NDK.
${log-lib} )
-
cmake_minimum_required
cmake版本 -
add_library
指定要编译的库,并将所有的 .c 或 .cpp 文件包含指定。 -
find_library
查找库所在目录 -
target_link_libraries
将库与其他库相关联
add_library添加多个源文件时.这样使用:
file(GLOB native_srcs “*.cpp”)
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).
${native_srcs})
3. build.gradle
android {
defaultConfig {
externalNativeBuild {
cmake {
cppFlags “”
}
}
}
externalNativeBuild {
cmake {
path “CMakeLists.txt”
}
}
}
使用cmake配置gradle关联.