• 【系统移植】JNI


    jni代码

    java调用jni的接口:


    1, 调用动态库

    static{
     
      System.loadLibrary("led_jni");  // system/lib/libled_jni.so
     }

    2, 调用驱动方法名:
        public native int openDev();
      
    3, 直接去调用,不需要去实现openDev()

    openDev();
     public native int openDev();
     public native int DevOn();
     public native int DevOff();
     public native int closeDev();

    jni的接口代码: jni代码(*.cpp)==>libled_jni.so

    参考模板:Native.cpp developmentsamplessimplejniJni 2599 2011-5-17

    1, JNI_OnLoad()   <== System.loadLibrary("led_jni") 第一个函数
     |
     原型:jint JNI_OnLoad(JavaVM* vm, void* reserved)
      返回值: 正确: JNI_VERSION_1_4
       错误: 小于0;
     a, 获取jni的版本,和dvm的运行环境对象 env (提供操作jni的方法)
      原型:  jint GetEnv(void** env, jint version)
      使用:   
        JNIEnv* env = NULL;
       vm->GetEnv((void **)&env, JNI_VERSION_1_4) 
       返回值:
        正确: JNI_OK == 0
        错误: 小于0
       
     b, 构建映射表, 注册给dvm
      表示映射表:
      typedef struct
          const char* name;   // java方法名
          const char* signature;  // 方法的参数和返回值 描述
          void*       fnPtr; // c/c++的函数名
      } JNINativeMethod;
      const static JNINativeMethod   myMethods[] ={
       {"openDev", "()I", (void *)open_led},
       {"DevOn", "()I",   (void *)led_on},
       {"DevOff", "()I",  (void *)led_off},
       {"closeDev", "()I", (void *)close_led},
      };

      注册:
       env->RegisterNatives(jclass clazz, const JNINativeMethod* methods,jint nMethods)
       参数1: 将native方法所在的包.类转换成jclass, 告诉dvm,本地方法是属于哪个类
       jclass  myclz = env->FindClass("com/hq/ledjnitest/LedActivity");

       env->RegisterNatives(myclz, myMethods,   sizeof(myMethods)/sizeof(myMethods[0]));

    Android.mk

    LOCAL_PATH:= $(call my-dir)
    include $(CLEAR_VARS)
    LOCAL_SRC_FILES:= led_jni.cpp
    LOCAL_MODULE :=libled_jni

    #指定连接动态库< 类似于gcc  -lxx
    LOCAL_SHARED_LIBRARIES :=
            libcutils
    #指定头文件路径, 类似于gcc -Ixxx
    LOCAL_C_INCLUDES +=
            $(JNI_H_INCLUDE)
    #编译成so,需要一个标志
    LOCAL_PRELINK_MODULE := false
    include $(BUILD_SHARED_LIBRARY)

    编译:

    mmm mytest/jni/
    ============================================
    PLATFORM_VERSION_CODENAME=REL
    PLATFORM_VERSION=2.1-update1
    TARGET_PRODUCT=fs100
    TARGET_BUILD_VARIANT=eng
    TARGET_SIMULATOR=false
    TARGET_BUILD_TYPE=release
    TARGET_ARCH=arm
    HOST_ARCH=x86
    HOST_OS=linux
    HOST_BUILD_TYPE=release
    BUILD_ID=ERE27
    ============================================
    make:进入目录'/home/ldswfun/src_s5pc/eclair_2.1_farsight'
    target thumb C++: libled_jni <= mytest/jni/led_jni.cpp
    In file included from mytest/jni/led_jni.cpp:5:
    dalvik/libnativehelper/include/nativehelper/jni.h:489: note: the mangling of 'va_list' has changed in GCC 4.4
    target SharedLib: libled_jni (out/target/product/fs100/obj/SHARED_LIBRARIES/libled_jni_intermediates/LINKED/libled_jni.so)
    target Non-prelinked: libled_jni (out/target/product/fs100/symbols/system/lib/libled_jni.so)
    target Strip: libled_jni (out/target/product/fs100/obj/lib/libled_jni.so)
    Install: out/target/product/fs100/system/lib/libled_jni.so
    make:离开目录“/home/ldswfun/src_s5pc/eclair_2.1_farsight

    运行:

    apk===> /system/app
    so ==> /syste/lib/
    cp -raf out/target/product/fs100/system/lib/libled_jni.so   /opt/myrootfs/system/lib

     @成鹏致远

    (blogs:http://lcw.cnblogs.com

    (emailwwwlllll@126.com)

    (qq552158509





  • 相关阅读:
    我的有道难题算法-双倍超立方数
    终于获取了SharePoint.OpenDocument对象打开的Word对象
    Eclipse下的项目管理插件介绍
    初识 sqlite 与 content provider 学习笔记
    android 官方文档中的一些错误收集
    android TraceView (图形化性能测试工具)使用入门笔记
    Texttospeech 入门与进阶学习笔记(android)
    Intent进阶 和 Intentfilter 学习笔记
    android UI设计属性中英对照表(未修订)
    android学习笔记7天打造一个简易网络Mp3播放器
  • 原文地址:https://www.cnblogs.com/lcw/p/3802654.html
Copyright © 2020-2023  润新知