打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android JNI 通过JNI_Onload注册方法的过程

简单的Jni 例子都是映射模式,及对应的Jni 的c/c++ 实现需要,被java的函数命名规则限制死,为了解决这类毛病,引入的JNI_OnLoad这类方法。
jint JNI_OnLoad(JavaVM* vm, void* reserved)
该方法在Jni so 被加载时调用
以下提供一个实例:
C方面
#include <string.h>
#include <stdio.h>
#include "utils/Log.h"
#include <unistd.h>
#include <assert.h>
 
#include "jni.h"
#include "JNIHelp.h"
#ifndef LOG_TAG
#define LOG_TAG "venus"
#endif
 
 
static void setTitle(JNIEnv* env, jobject thiz, jstring str)
{
    char buf[256];
    const char *strUTF = env->GetStringUTFChars(str, 0);
 
    snprintf(buf, sizeof(buf), "venus set Title: %s\n", strUTF);
    env->ReleaseStringUTFChars(str, strUTF);
 
    jstring title = env->NewStringUTF(buf);
    jclass cls = env->GetObjectClass(thiz);
 
    //jmethodID mid;
    //mid = env->GetMethodID(cls, "setTitle", "(Ljava/lang/String;)V");
    //env->CallVoidMethod(thiz, mid, str);
 
    jfieldID fid;
    fid = env->GetFieldID(cls, "mTitle", "Ljava/lang/String;");
    env->SetObjectField(thiz, fid, title);
    env->ReleaseStringUTFChars(title, buf);
 
    LOGD("--------------setTitle------------>>>>%s\n", buf);
}
 
 
 
static jint getSum(JNIEnv* env, jobject thiz, jint num1, jint num2)
{
    int result;
 
    result = num1 + num2;
    LOGD("--------------getSum------------>>>>%d\n", result);
    return result;
}
 
 
 
static const JNINativeMethod gMethods[] = {
    {"setTitle", "(Ljava/lang/String;)V", (void *)setTitle},
    {"getSum", "(II)I", (void *)getSum},
 
};
 
static int registerMethods(JNIEnv* env)
{
    jclass clazz;
    static const char* const kClassName =  "com/android/jni_second/JNI";
 
    /* look up the class */
    clazz = env->FindClass(kClassName);
 
    if (clazz == NULL) {
        LOGE("Can't find class %s\n", kClassName);
        return -1;
    }
 
    /* register all the methods */
    if (env->RegisterNatives(clazz, gMethods, sizeof(gMethods) / sizeof(gMethods[0])) != JNI_OK)
    {
        LOGE("Failed registering methods for %s\n", kClassName);
        return -1;
    }
 
    /* fill out the rest of the ID cache */
    return 0;
}
 
 
/*
 * This is called by the VM when the shared library is first loaded.
 */
jint JNI_OnLoad(JavaVM* vm, void* reserved) {
    JNIEnv* env = NULL;
    jint result = -1;
 LOGI("--------------JNI_OnLoad---------------------");
 
    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
    {
        LOGE("ERROR: GetEnv failed\n");
        goto fail;
    }
 
    assert(env != NULL);
 
    if (registerMethods(env) != 0)
    {
        LOGE("ERROR: PlatformLibrary native registration failed\n");
        goto fail;
    }
 
    /* success -- return valid version number */
    result = JNI_VERSION_1_4;
fail:
    return result;
}
Java方面
package com.android.jni_second;
 
public class JNI {
 
 public native  void setTitle(String str);
 
 public native  int getSum(int num1, int num2);
 
 public String getTitle(){
  return mTitle;
 }
 
 private String mTitle;
}

例子很简单,看后记得吐槽哈~谢谢!
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
JNI技术与Android应用(0)
JNI与Android VM之间的关系---- 本文摘录自 高焕堂老师 的Android课...
认识*.so里的JNI_OnLoad()函数
Android JNI知识简介
Android视频渲染: YUV转RGB
Android的NDK开发(5)————Android JNI层实现文件的read、wri...
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服