JNI开发步奏

为了学习学习和工作中开发音视频,不得不学习音视频的基本之一 JNI开发  

1. 本次使用的Android gradle 版本是3.1.2

首先创建功能 

将c++支持添加上去 

之后最后进入这个界面

一般都选择默认 除非你可能使用c++ 更高级功能, 下面2个可以选上 也可以不选 下面2个作用是c++或者c语言 预编译时候报错上报处理

一个支持jni的Android工程已经创建 可能等时间长一点

相比之前的工程 明显可以看到项目多一些东西

项目中 多一些东西有什么用 下一篇文章再讲 这篇只是带大家走一遍 jni 调用流程

首先   创建一个

在 这个类里面 实现一个native 方法

public static native int doubleData(int data);

接下来在 Terminal 里面  cd app 回车 cd src 回车 cd main 回车  cd java

执行 javah  com.example.zed009.jnicode6.SimpleJniUtils  (javah 包名+类名)

获取到一个 

头文件

放入cpp 中

在默认 native-lib 全干掉

# include "com_example_zed009_jnicode6_SimpleJniUtils.h"
#include "StreamPublishLib.h"

#include <jni.h>

JNIEXPORT jint JNICALL Java_com_example_zed009_jnicode6_SimpleJniUtils_doubleData
        (JNIEnv *env, jclass j, jint data) {
    streamPublisher * ptr = new streamPublisher();

    return  data*2;
}

实现了 头文件的方法 

然后在 SimpleJniUtils 中添加  

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

你已经可以调用一个完整的jni  

但是 你为什么 load native-lib  而不是 别的  打开cmakelists 文件

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
             )

set the name of library  这个很好理解   下面那个就是你要提供实现这个库c或者c++代码路径

但是你怎么调用别人写的C代码 

首先你要将将 c编译成c库

同样你创建一个支持c++的工程 然后即将native-lib文件干掉  将你调用c代码复制到cpp文件夹下面

这里我把头文件放进去了

可以不需要放

然后更改你的Cmake文件

# 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.
             streampublishlib-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/StreamPublishLib.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.
                       streampublishlib-lib

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

然后编译

在app /build 目录 下 

找到你的。so库  这个名字 系统给生成  你不想要这个名字也可以改配置  自行百度

之后将你的so库 放进你之前的项目里面 jniLIbs下面

更改你cmake 

添加

add_library(libstreampublishlib-lib SHARED IMPORTED)
set_target_properties(libstreampublishlib-lib PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libstreampublishlib-lib.so )

在目标链接里面l 添加你so库编译目标

target_link_libraries( # Specifies the target library.
                       native-lib
                       libstreampublishlib-lib


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

然后在你c代码 实现里面

JNIEXPORT jint JNICALL Java_com_example_zed009_jnicode6_SimpleJniUtils_doubleData
        (JNIEnv *env, jclass j, jint data) {
    streamPublisher * ptr = new streamPublisher();

    return  data*2;
}

去调用so库里面方法 

这样就是一个完整调用jni 方法

本人遇到的坑 有

ndk 工具 版本太老  我的工具升级到3.0以上 ndk还是老的 然后干掉 重新下

      项目开始之前 一定 下载ndk cmake llvm 等依赖 

elispe 使用jvm 编译    Android 靠llvm 编译 具体有什么差别 下面再介绍吧

猜你喜欢

转载自blog.csdn.net/dhl006009/article/details/84313195
今日推荐