android 4.4系统jni函数查找报错问题解决方案

android 4.4系统jni函数查找报错问题解决方案


本人开发中遇到一个蛋疼的问题,就是在apk在高版本上运行都ok,但是安装到android 4.4版本总是报java.lang.UnsatisfiedLinkError: Native method not found。没法使用jni,头都挠破了,最后问题是这么解决的:
我刚开始生成的so文件是通过NDK
1、javac xxx.java
2、javah -jni com.xx.xx.xx,生成的.h文件放到jni文件夹中,再编写C++代码
(这两步如果不会的话可以百度一下)
3、build.gradle中配置
android 4.4系统jni函数查找报错问题解决方案_第1张图片
4、编译之后build下会生成so文件,然后在代码中调用即可
android 4.4系统jni函数查找报错问题解决方案_第2张图片
这样生成的so文件就是调用不了,不知道为什么,最后我尝试了一下使用CMake编译就可以了,具体步骤如下:

1、javac xxx.java
2、javah -jni com.xx.xx.xx,生成的.h文件放到jni文件夹中,再编写C++代码
(这两步如果不会的话可以百度一下)
3、复制一个CMakeLists.txt到module下面
android 4.4系统jni函数查找报错问题解决方案_第3张图片
具体代码:



cmake_minimum_required(VERSION 3.4.1)


#每次添加一个so库都需要添加一个add_library
add_library( # Sets the name of the library.
             hello #1、这里是将要调用的so文件,比如System.loadLibrary("hello");可自定义

             # Sets the library as a shared library.
             SHARED  #2、这里不用改

             # Provides a relative path to your source file(s).
             src/main/jni/test.cpp )  #3、这里是c++代码路径
add_library( # Sets the name of the library.
             cutils

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/jni/CUtils.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.
                       hello #这里需要写一个上面添加的一个so文件名

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

4、在build.gradle中配置一下
android 4.4系统jni函数查找报错问题解决方案_第4张图片
5、直接代码中System.loadLibrary();加载调用即可

不知道为什么这么写,反正我的问题是解决了,我当时的问题是必现问题,如果跟我一样的现象可以尝试一下
o( ̄︶ ̄)o

你可能感兴趣的:(android开发)