NDK 之动态注册

在这里记录了一下 JNI 的动态注册,研究了好长时间,总算是写出来一份可以提供给 java 层调用的jni方法了

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

#define TAG "tian.shm"

#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG,__VA_ARGS__);
jint native_tsm_player_prepare(JNIEnv * env,jobject obj,jstring source) {
  LOGI("-------native_tsm_player_prepare---------source:%s",env->GetStringUTFChars(source,NULL))
  return 0;
}
void native_tsm_player_start(JNIEnv * env,jobject obj) {
  LOGI("-------native_tsm_player_start---------")
}
void native_tsm_player_stop(JNIEnv * env,jobject obj) {
  LOGI("-------native_tsm_player_stop---------")
}
void native_tsm_player_release(JNIEnv * env,jobject obj) {
  LOGI("-------native_tsm_player_release---------")
}
JNINativeMethod method []= {
      {
              "nativePrepare",
              "(Ljava/lang/String;)I",
              (jint *)native_tsm_player_prepare
      },
      {
              "nativeRelease",
              "()V",
              (void *)native_tsm_player_release
      },
      {
              "nativeStop",
              "()V",
              (void *)native_tsm_player_stop
      },
      {
              "nativeStart",
              "()V",
              (void *)native_tsm_player_start
      }
};


int JNI_OnLoad(JavaVM
             *vm,
             void *unused
) {
  JNIEnv *env;
  int env_result = vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6);
  if (env_result != JNI_OK) {
      return JNI_FALSE;
  }
  jclass cls = env->FindClass("com/example/tsmplayer/TsmPlayer");

  env->RegisterNatives(cls, method, sizeof(method) / sizeof(JNINativeMethod));


  return JNI_VERSION_1_6;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • NDK中动态注册JNI方法。 本文链接 Java中定义了native方法后,在C/C++中使用JNI_OnLoad...
    AnRFDev阅读 2,112评论 0 1
  • 动态注册 在静态注册中,每次添加新函数后,要重新生成头文件,而且函数名较长,操作起来非常麻烦,那使用动态注册可以避...
    aafa41d78d15阅读 745评论 0 0
  • 概要 作者上一文通过Cmake的编译方式对Android开发NDK调用三方so库进行了演示,这里主要讲一下在NDK...
    D区小陈阅读 737评论 0 2
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 13,108评论 2 59
  • 本系列文章如下: Android JNI(一)——NDK与JNI基础Android JNI学习(二)——实战JNI...
    隔壁老李头阅读 205,909评论 25 536