您的当前位置:首页正文

Android 使用cmake实现JNI

2024-11-13 来源:个人技术集锦

1、什么是JNI

JNI 全称 Java Native Interface,Java 本地化接口,可以通过 JNI 调用系统提供的 API。

JNI是一套编程接口,用来实现Java代码与本地的C/C++代码进行交互。

NDK是Google开发的一套开发和编译工具集, 主要用于Android的JNI开发。

2、如何实现JNI

个人推荐使用cmake进行JNI的开发。下面讲一讲具体步骤:

         

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.
             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.

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.
                       native-lib

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

native-lib.cpp的代码为:

#include <jni.h>
#include <string>
#include <android/log.h>

#define LOG_TAG "videoPlayer from C"
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

extern "C"
JNIEXPORT jstring JNICALL
Java_com_hy_android_utils_PlayerJNI_stringFromJNI(JNIEnv *env, jobject instance) {

    // TODO
    std::string hello = "Hello from C++";

    return env->NewStringUTF(hello.c_str());
}

extern "C"
JNIEXPORT void JNICALL
Java_com_hy_android_utils_PlayerJNI_funFromJava(JNIEnv *env, jclass type, jobject obj,
                                                jstring id_) {
    const char *id = env->GetStringUTFChars(id_, 0);

    LOGE("---%s",id);
    //打印值 E/videoPlayer from C: ---1000

    jclass cls=env->GetObjectClass(obj);
    jmethodID methodID=env->GetMethodID(cls,"getData","(II)V");

    if(NULL==methodID){
        return;
    }

    env->CallVoidMethod(obj,methodID,111,666);

    env->ReleaseStringUTFChars(id_, id);
}     复制代码

创建一个java类PlayerJNI,添加native方法,代码为: 

public class PlayerJNI {
    private static final String TAG = "PlayerJNI";
    static {
        System.loadLibrary("native-lib");
    }


    public void getData(int a,int b){
        Log.e(TAG, a+"---"+b);
        //打印值为 PlayerJNI: 111---666
    }

    public static native String stringFromJNI();

    public static native void funFromJava(Object obj,String id);
}
复制代码

3、从java层调用c层的代码为:

String str= PlayerJNI.stringFromJNI();
Log.e("----",str);
//打印值为 Hello from C++复制代码

4、从c层调用java类PlayerJNI里面的getData方法:

//将对象传到C层
PlayerJNI player=new PlayerJNI();
PlayerJNI.funFromJava(player,"1000");复制代码
//c层调用getData方法时,又可以获取c层的传参
public void getData(int a,int b){
    Log.e(TAG, a+"---"+b);
    //打印值为 PlayerJNI: 111---666
}
public static native void funFromJava(Object obj,String id);复制代码
//实现类java传参到c层,然后c层调用java方法并传参到java方法类
Java_com_hy_android_utils_PlayerJNI_funFromJava(JNIEnv *env, jclass type, jobject obj,
                                                jstring id_) {
    const char *id = env->GetStringUTFChars(id_, 0);

    LOGE("---%s",id);
    //打印值 E/videoPlayer from C: ---1000

    jclass cls=env->GetObjectClass(obj);
    jmethodID methodID=env->GetMethodID(cls,"getData","(II)V");

    if(NULL==methodID){
        return;
    }

    env->CallVoidMethod(obj,methodID,111,666);

    env->ReleaseStringUTFChars(id_, id);
}     复制代码


转载于:https://juejin.im/post/5ba21da4e51d450e664b39d7

显示全文