分享

AndroidJni开发

 QomoIT 2020-06-10

简介:

涉及到一些算法或者底层驱动的时候,往往需要使用jni来开发。现在官方推荐使用CMake工具来开发jni。

使用CMake开发Jni其实挺简单的,要求不高的话只需要简单配置一下就可以使用了。

配置环境

使用CMake进行Jni开发需要使用CMake插件、LLDB插件、NDK插件,这些都可以通过Android Studio很快地安装。
打开SDK Manager,找到Android SDK->SDK Tool选项,安装CMake、LLDB、NDK插件。
在这里插入图片描述

创建支持C++代码的工程

勾选Include C++ support选项,勾选后Android Studio可以更好地支持及帮助我们检查jni代码,之后一路next即可。
在这里插入图片描述
创建完成后会发现Android Studio会自动为我们生成一个Jni调用示例。我们把工各切换至project视图,来看一看目录结构。
在这里插入图片描述
看目录可以知道,相比普通工程,AndroidStudio主动为我们生成了一个cpp的目录,该目录主要存放调用的c++头文件,源代码及jni代码,还有一个CMakeLists.txt的文件,cpp目录下面包含一个native-lib.cpp文件(稍后会讲解),这里需要关注的主要文件有3个,native-llib.cpp、MainActivity和CMakeList.txt。下面重点讲解一下这几个文件的作用。

native-lib.cpp文件
该文件是之前勾选支持c++后Android Studio主动为我们生成的一个示例调用文件,里面包含了jni代码。

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

extern "C" JNIEXPORT jstring

JNICALL
Java_mhwang_com_jnidemo_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

MainActivity.java文件

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}

这里的代码主要注意2点,一是

static {
        System.loadLibrary("native-lib");
    }

这里主要加载native-lib库,这里加载库的名称需要跟CMakeLists.txt里面配置的一样。
二是public native String stringFromJNI(),方法,主要提供java本地jni接口,java层代码就是调用这个接口来通过jni调用C++函数的。

CMakeLists.txt文件
这个文件的作用是通过配置将C++代码编译到共享对象库中,具体功能项直接通过注释来说明吧。

# For more information about using CMake with Android Studio, read the
# documentation: https://d./studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.
# 设置CMake构造本地库所需要的最低版本

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.
# 创建并命名库,可以将库的类型设置为静态
# 或共享,并提供该库源代码的相对路径。
# 你可以定义多个library库,并使用CMake来构建。
# Gradle会自动打包库集成到apk中。

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).
             # 库所在位置的相对路径
             src/main/cpp/native-lib.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.
# NDK提供了一套实用的原生API和库,可以使用find_library搜索NDK中存在的库,只需要加入所需要使用库的名称即可,如下面的日志库log-lib。
# 更多的库可参考https://developer./ndk/guides/stable_apis
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.
# 指定CMake连接到目标库中,可以链接多个库,如上面的native-lib库以及NDK中自带的log-lib库
target_link_libraries( # Specifies the target library.
                       native-lib

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

截至目前为止,我们什么也没有添加,但是这已经是一个完整的jni开发的Android 应用了,我们来运行看一下效果。
在这里插入图片描述
可以看到,app已经能调用jni代码中的Hello from C++了。
虽然看着简单,但是在编译运行时,Android Studio为我们做了大量的工作。
1、首先Gradle调用了工程中的CMakeLists.txt文件
2、CMake按照期里面的命令将C++源文件native-lib.cpp编译到共享对象库中,并命名为libnative-lib.so,之后由Gradle将其打包到APK中。
3、运行时,由于System.loadLibrary()是静态方法,会在应用MainActivity中的onCreate()调用之前加载原生库。实际运行结果也是先执行loadLibrary()

在这里插入图片描述
4、之后再由onCreate()中调用stringFromJNI(),并将返回的“Hello from C++”更新视图。

更多详细的说明可以参考官方文档

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多