Android(安卓)Studio3.4.2配置NDK
16lz
2022-04-14
说多了都是累,配置个环境花了一天 时间。看了五湖四海大佬们写的帖子,怎么感觉就是解决不了我的问题。我的错误是:
07-22 14:53:06.058 5541-5541/com.example.view E/art: dlopen("JNITest", RTLD_LAZY) failed: dlopen failed: library "JNITest" not found07-22 14:53:06.059 5541-5541/com.example.view E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.view, PID: 5541 java.lang.UnsatisfiedLinkError: dlopen failed: library "JNITest" not found at java.lang.Runtime.load(Runtime.java:331) at java.lang.System.load(System.java:981) at com.example.view.MainActivity.(MainActivity.java:66) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.Class.newInstance(Class.java:1606) at android.app.Instrumentation.newActivity(Instrumentation.java:1066) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2228) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) at android.app.ActivityThread.access$800(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5293) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
学习就是不断探索与尝试,然后总结经验。下面详细说明配置过程:
工具准备
点击SDK
安装CMake和NDK
然后进入Project Struct设置NDK路径
点击进入Project Structure
如果没有环境变量,在设置系统环境变量
将上图中的路径添加到下图中
打开终端。输入 ndk-build
如果出现上图结果,说明环境配置成功!
实战 NDK
新建JNITest类
package com.example.view;public class JNITest { public native String stringFromJNI();}
然后在main目录下 新建cpp目录
新建JNITest.cpp文件
#include #include extern "C" JNIEXPORT jstring JNICALLJava_com_example_view_JNITest_stringFromJNI( JNIEnv *env, jobject /* this */) { std::string hello = "Hello from C++"; return env->NewStringUTF(hello.c_str());}
在这个目录下新建CMakeLists.txt
# For more information about using CMake with Android Studio, read the# documentation: https://d.android.com/studio/projects/add-native-code.html# Sets the minimum version of CMake required to build the native library.cmake_minimum_required(VERSION 3.4.1)# Creates and names a library, sets it as either STATIC# or SHARED, and provides the relative paths to its source code.# You can define multiple libraries, and CMake builds them for you.# Gradle automatically packages shared libraries with your APK.add_library( # Sets the name of the library.#库文件名 JNITest # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). #cpp文件 JNITest.cpp)# Searches for a specified prebuilt library and stores the path as a# variable. Because CMake includes system libraries in the search path by# default, you only need to specify the name of the public NDK library# you want to add. CMake verifies that the library exists before# completing its build.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)# Specifies libraries CMake should link to your target library. You# can link multiple libraries, such as libraries you define in this# build script, prebuilt third-party libraries, or system libraries.target_link_libraries( # Specifies the target library.#库文件名 JNITest # Links the target library to the log library # included in the NDK. ${log-lib})
最后目录如下
然后在build.gradle中 添加cmake,如下图
OK配置完成,然后 就是综合编译。
CPP功能验证
引用库
static { System.loadLibrary("JNITest"); }
在这里提醒一下,一定不要写错了
System.load("JNITest");
否则编译不会报错,APP不能运行!!!
最后实例化一下
JNITest jniTest = new JNITest(); Log.i("mouse","JNI : "+ jniTest.stringFromJNI());
结果如下图
结果是与CPP文件里是对应的。
大功告成!!!
更多相关文章
- Android(安卓)对话框【Dialog】去除白色边框代码
- Android(安卓)Build 系统
- 赵雅智_Android的getResources()资源引用
- eclipse上Android(安卓)NDK开发环境搭建
- LBE 安全大师支持android 4.4注入分析
- Android分辨率自适应
- ionic打包apk时报错No resource identifier found for attribute
- android从资源文件中读取文件流显示
- NPM 和webpack 的基础使用