android JNI测试记录

JNI分静态注册和动态注册,这里测试动态注册

库文件的生成

Android.mk文件

include $(BUILD_EXECUTABLE)

include $(CLEAR_VARS)
LOCAL_SRC_FILES:= jni_lib.cpp
LOCAL_C_INCLUDES := $(JNI_H_INCLUDE)
LOCAL_SHARED_LIBRARIES := libutils libandroid_runtime libnativehelper

LOCAL_MODULE := libtestjni
include $(BUILD_SHARED_LIBRARY)

 

jni_lib.cpp

#include <jni.h>
#include "JNIHelp.h"
#define LOG_TAG "TestLib"

#include <utils/Log.h>
#include "android_runtime/AndroidRuntime.h"

#define INTERNALAPI_PKG_NAME                "fgt/org/SudokuActivity"

using namespace android;

 

JNIEXPORT void JNICALL printHello(JNIEnv * env, jobject jobj) ;
static JNINativeMethod gMethods[] = {
{"myprint",
"()V",
(void*)printHello
}
};
jint JNI_OnLoad(JavaVM*vm,void*reserved)
{
    JNIEnv* env = NULL;
    jint result = -1;

    LOGW("TestInteralApi JNI loaded");

    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
        LOGW("GetEnv failed");
        goto bail;
    }
    assert(env != NULL);
   
// AndroidRuntime::registerNativeMethods(env, INTERNALAPI_PKG_NAME, gMethods, NELEM(gMethods));

    if (AndroidRuntime::registerNativeMethods(env, INTERNALAPI_PKG_NAME, gMethods, NELEM(gMethods))< 0) {
        LOGW("TestInternalApi native registration failed");
        goto bail;
    }

    result = JNI_VERSION_1_4;

bail:
    return result;

}
JNIEXPORT void JNICALL printHello(JNIEnv * env, jobject jobj)
{
 LOGW("Hello LIB!/n");
}

 

package fgt.org;

 

public class SudokuActivity extends Activity implements OnClickListener{
  
 
 
   static {
         try {
             System.loadLibrary("testjni");
         } catch (UnsatisfiedLinkError e) {
             Log.d("itest_jni", "itest jni library not found!");
         }
     }
   public native static void myprint();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值