最新Android-JNI学习(一)-创建.so文件

根据前面学习的c学习的基础知识,在as下,进行手动的ndk开发

这里写图片描述
1.在java类中先写native方法,并且加载将将要生成的c或c++的文件的名称
这里写图片描述

2.在terminal的命令行中输入命令生成头文件
javah -d ../jni com.xxx.xxx 。其中-d是生成文件夹的意思
这里写图片描述
3.在生成的jni,右键创建c或c++代码。
这里写图片描述
4.由于as现在支持cmake的构建ndk的方法,原有的方法比如在工程目录下的gradle.properties中添加
这里写图片描述
都是无效的。
因此应该app的gradle下面做相应的cmake的配置
在android–> defaultConfig下面添加
这里写图片描述

5.在android下面添加,否则会默认只生成一个平台的so文件
这里写图片描述
6.指定cmake文件的路径
这里写图片描述
7.创建CMakeLists.txt文件
这里写图片描述
8.CMakeLists.txt的内容,并根据提示进行响应的修改

cmake_minimum_required(VERSION 3.4.1)

add_library( # Sets the name of the library.
JNIHello
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/jni/JNIHello.c )

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.
JNIHello
# Links the target library to the log library
# included in the NDK.
${log-lib} )

9.最后在下面目录中找到so文件
这里写图片描述

其响应的配置都可以参考as默认创建的jni。在创建工程时,选择c++支持。就可以创建默认应用。

猜你喜欢

转载自blog.csdn.net/yuezheyue123/article/details/81028233
今日推荐