Android Studio3.4.2配置NDK

说多了都是累,配置个环境花了一天 时间。看了五湖四海大佬们写的帖子,怎么感觉就是解决不了我的问题。我的错误是:

07-22 14:53:06.058 5541-5541/com.example.view E/art: dlopen("JNITest", RTLD_LAZY) failed: dlopen failed: library "JNITest" not found
07-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.<clinit>(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)

学习就是不断探索与尝试,然后总结经验。下面详细说明配置过程:

工具准备

1
点击SDK
在这里插入图片描述
安装CMake和NDK
ANZHUANG
然后进入Project Struct设置NDK路径
1
点击进入Project Structure


如果没有环境变量,在设置系统环境变量
在这里插入图片描述
在这里插入图片描述
将上图中的路径添加到下图中
在这里插入图片描述
打开终端。输入 ndk-build
在这里插入图片描述

如果出现上图结果,说明环境配置成功!

实战 NDK

新建JNITest类

package com.example.view;

public class JNITest {
    public native String stringFromJNI();
}

然后在main目录下 新建cpp目录
新建JNITest.cpp文件

#include <jni.h>
#include <string>

extern "C" JNIEXPORT jstring JNICALL
Java_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文件里是对应的。

大功告成!!!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值