Chinaunix首页 | 论坛 | 博客
  • 博客访问: 228675
  • 博文数量: 29
  • 博客积分: 1477
  • 博客等级: 上尉
  • 技术积分: 451
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-06 14:48
文章分类
文章存档

2012年(1)

2011年(14)

2010年(14)

我的朋友

分类: 嵌入式

2010-12-21 16:21:53

自定义Android核心服务实践

Daniel Wood 20101203

转载时请注明出处和作者

文章出处:http://danielwood.cublog.cn

作者:Daniel Wood

------------------------------------------------------------------------

4.    JNI层实现

com_android_service_cal.cpp文件

#define LOG_TAG "libJniCal native.cpp"
#include <utils/Log.h>
#include <stdio.h>
#include "jni.h"
#include "../libcal/Cal.h"
static jint
cal_service_add(JNIEnv *env, jobject thiz, jint a, jint b)
{
    android::Cal cal;
    int result = cal.Add(a, b);
    LOGI("%d + %d = %d", a, b, result);
    return result;
}
static jint
cal_service_sub(JNIEnv *env, jobject thiz, jint a, jint b)
{
    android::Cal cal;
    int result = cal.Sub(a, b);
    LOGI("%d - %d = %d", a, b, result);
    return result;
}
static jint
cal_service_mul(JNIEnv *env, jobject thiz, jint a, jint b)
{
    android::Cal cal;
    int result = cal.Mul(a, b);
    LOGI("%d * %d = %d", a, b, result);
    return result;
}
static jfloat
cal_service_div(JNIEnv *env, jobject thiz, jint a, jint b)
{
    android::Cal cal;
    float result = cal.Div(a, b);
    LOGI("%d / %d = %f", a, b, result);
    return result;
}
static const char *classPathName = "com/example/android/simpleservice/Native";/*应用程序中所对应类的地址,这里错了很多次*/

static JNINativeMethod methods[] = {
  {"add", "(II)I", (void*)cal_service_add },
  {"sub", "(II)I", (void*)cal_service_sub },
  {"mul", "(II)I", (void*)cal_service_mul },
  {"div", "(II)F", (void*)cal_service_div },
};

/*
 * Register several native methods for one class.
 */

static int registerNativeMethods(JNIEnv* env, const char* className,
    JNINativeMethod* gMethods, int numMethods)
{
    jclass clazz;
    clazz = env->FindClass(className);
    if (clazz == NULL) {
        LOGE("Native registration unable to find class '%s'", className);
        return JNI_FALSE;
    }
    if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
        LOGE("RegisterNatives failed for '%s'", className);
        return JNI_FALSE;
    }
    return JNI_TRUE;
}
/*
 * Register native methods for all classes we know about.
 *
 * returns JNI_TRUE on success.
 */

static int registerNatives(JNIEnv* env)
{
  if (!registerNativeMethods(env, classPathName,
                 methods, sizeof(methods) / sizeof(methods[0]))) {
    return JNI_FALSE;
  }
  return JNI_TRUE;
}
----------------------------------------------------------------------------
/*
 * This is called by the VM when the shared library is first loaded.
 */


typedef union {
    JNIEnv* env;
    void* venv;
} UnionJNIEnvToVoid;

jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
    UnionJNIEnvToVoid uenv;
    uenv.venv = NULL;
    jint result = -1;
    JNIEnv* env = NULL;
    
    LOGI("JNI_OnLoad");

    if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_4) != JNI_OK) {
        LOGE("ERROR: GetEnv failed");
        goto fail;
    }
    env = uenv.env;

    if (registerNatives(env) != JNI_TRUE) {
        LOGE("ERROR: registerNatives failed");
        goto fail;
    }
    LOGI("JNI_OnLoad Done");
    result = JNI_VERSION_1_4;
    
fail:
    return result;
}

Android.mk文件

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:=com_android_service_cal.cpp
LOCAL_C_INCLUDES:= $(JNI_H_INCLUDE)
LOCAL_SHARED_LIBRARIES := libutils \
libCalService \
libCal
LOCAL_MODULE := libJniCal    
LOCAL_PRELINK_MODULE:= false
include $(BUILD_SHARED_LIBRARY)

我们将JNI层的实现编译成一个自定义的共享库libJniCal,也可以将其实现写到Androidlibandroid_runtime.so中。

通过make libJniCal得到自定义共享库:

target SharedLib: libJniCal (out/target/product/generic/obj/SHARED_LIBRARIES/libJniCal_intermediates/LINKED/libJniCal.so)
target Non-prelinked: libJniCal (out/target/product/generic/symbols/system/lib/libJniCal.so)
target Strip: libJniCal (out/target/product/generic/obj/lib/libJniCal.so)
Install: out/target/product/generic/system/lib/libJniCal.so


阅读(1922) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~