yangtutu_jni.cpp
#include <jni.h>
#include <stdio.h>
#include <sys/time.h>
#include <android/log.h>
static long getCurrentTimeMs(void)
{
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
jboolean yangtutu_bool_int(JNIEnv *env, jobject obj, jint a)
{
int b = a;
__android_log_print(ANDROID_LOG_WARN, "yangtutu_jni", "b=%d", b);
return 1;
}
jint yangtutu_int_byteArray(JNIEnv *env, jobject obj, jbyteArray buffer)
{
int count;
unsigned char *buffer_ptr;
buffer_ptr = (unsigned char *)(env->GetByteArrayElements(buffer,0));
count = (int)(env->GetArrayLength(buffer));
__android_log_print(ANDROID_LOG_ERROR, "yangtutu_jni", "time_start=%ld", getCurrentTimeMs());
for(count -= 1; count >= 0; count--)
buffer_ptr[count] = 0;
__android_log_print(ANDROID_LOG_ERROR, "yangtutu_jni", "time_end=%ld", getCurrentTimeMs());
env->ReleaseByteArrayElements(buffer,(jbyte *)buffer_ptr,0);
return count;
}
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env = NULL;
__android_log_print(ANDROID_LOG_WARN, "yangtutu_jni", "JNI_OnLoad!!");
if(vm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
{
__android_log_print(ANDROID_LOG_ERROR, "yangtutu_jni", "JNI_EVERSION!!");
return JNI_EVERSION;
}
jclass myclass = env->FindClass("com/android/YangTuTuJni");
if(myclass == NULL)
{
__android_log_print(ANDROID_LOG_ERROR, "yangtutu_jni", "FindClass Error!!");
return JNI_ERR;
}
const JNINativeMethod method[] = {
{"native_bool_int","(I)Z",(void*)yangtutu_bool_int},
{"native_int_byteArray","([B)I",(void*)yangtutu_int_byteArray}
};
if(env->RegisterNatives(myclass, method, 2) != JNI_OK)
{
__android_log_print(ANDROID_LOG_ERROR, "yangtutu_jni", "RegisterNatives Error!");
return JNI_ERR;
}
return JNI_VERSION_1_6;
}
Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
yangtutu_jni.cpp
LOCAL_C_INCLUDES += \
$(JNI_H_INCLUDE) \
$(call include-path-for, bluedroid) \
$(call include-path-for, libhardware)/hardware \
$(call include-path-for, libhardware_legacy)/hardware_legacy
LOCAL_SHARED_LIBRARIES := \
libnativehelper \
liblog \
libcutils \
libutils \
libhardware \
libhardware_legacy
LOCAL_MODULE:= libyangtutu_jni
include $(BUILD_SHARED_LIBRARY)
YangTuTuJni.java
package com.android;
import java.io.FileDescriptor;
import java.io.IOException;
import android.util.Log;
public final class YangTuTuJni{
private static final String TAG = "YangTuTuJni";
static {
try {
System.loadLibrary("yangtutu_jni");
Log.w(TAG, "loadLibrary ok !! ");
} catch (UnsatisfiedLinkError e) {
Log.e(TAG, "loadLibrary UnsatisfiedLinkError !! ");
e.printStackTrace();
} catch (Exception e) {
Log.e(TAG, "loadLibrary Exception !! ");
e.printStackTrace();
}
}
public static native boolean native_bool_int(int a);
public static native int native_int_byteArray(byte[] buffer);
}