zoukankan      html  css  js  c++  java
  • Android JNI入门第一篇——HelloJni

    android支持使用NDK开发C程序,关于配置NDK环境问题应该不用再赘述了,这个网上有很多,这里通过一篇实例来讲述简单的JNI开发,大家可以参考这篇文章(Get Your Eclipse-Integrated NDK On!)搭建Eclipse编译C语言为so文件的开发环境。

            native方法实现步骤如下:

            1、在Java中声明native()方法,然后编译(javac); 

          2、用javah产生一个.h文件; 

          3、编写包含.h文件的c文件

          4、编译c文件

          5、使用编译成功的so文件。

           第一步:

                  1、声明native方法

                     

    1. public class Printf_Jni {  
    2.   
    3.      static {  
    4.             System.loadLibrary("com_nedu_jni_helloword_printf-jni");  
    5.         }  
    6.     public native void printHello();  
    7. }  

           2、javac编译

                进入java文件所在路径,调用javac命令,如图:

                 

    第二步:使用javah命令生成.h头文件,如图:

               

         这个要回到src目录下,不知道什么原因,如果在上面的javac路径下会报错,如图:

           

          使用javah命令生成的头文件如下:

    1. /* DO NOT EDIT THIS FILE - it is machine generated */  
    2. #include <jni.h>  
    3. /* Header for class com_nedu_jni_helloword_Printf_Jni */  
    4.   
    5. #ifndef _Included_com_nedu_jni_helloword_Printf_Jni  
    6. #define _Included_com_nedu_jni_helloword_Printf_Jni  
    7. #ifdef __cplusplus  
    8. extern "C" {  
    9. #endif  
    10. /* 
    11.  * Class:     com_nedu_jni_helloword_Printf_Jni 
    12.  * Method:    printHello 
    13.  * Signature: ()V 
    14.  */  
    15. JNIEXPORT void JNICALL Java_com_nedu_jni_helloword_Printf_1Jni_printHello  
    16.   (JNIEnv *, jobject);  
    17.   
    18. #ifdef __cplusplus  
    19. }  
    20. #endif  
    21. #endif  

    第三步:编写c文件,代码如下:

    1. #include<stdio.h>    
    2. #include <stdlib.h>    
    3. #include "com_nedu_jni_helloword_Printf_Jni.h"    
    4. JNIEXPORT void JNICALL Java_com_nedu_jni_helloword_Printf_1Jni_printHello  
    5.   (JNIEnv *e, jobject j)    
    6. {    
    7.     printf("Hello world!");   
    8. }    

    第四步,书写Android.mk文件,编译c文件
            Android.mk文件如下:

         

    1. LOCAL_PATH := $(call my-dir)  
    2.   
    3. include $(CLEAR_VARS)  
    4.   
    5. LOCAL_MODULE    := com_nedu_jni_helloword_printf-jni  
    6. LOCAL_SRC_FILES :=Printf_Jni.c  
    7.   
    8. include $(BUILD_SHARED_LIBRARY)  


    LOCAL_MODULE    := com_nedu_jni_helloword_printf-jniLOCAL_MODULE    := com_nedu_jni_helloword_printf-jniLOCAL_MODULE  表示so文件名

    LOCAL_SRC_FILES 需要编译的文件

    按照这篇文章(Get Your Eclipse-Integrated NDK On!)的介绍就可以在Eclipse编译了。

    第五步:使用so文件:

        通过下面的代码加载so文件

    1. System.loadLibrary("com_nedu_jni_helloword_printf-jni");  


    通过下面的代码加载so文件通过下面的代码加载so文件

    调用如下:

    1. Printf_Jni print=new Printf_Jni();   
    1. print.printHello();  

     

     

     

    /**
    * @author 张兴业
    * 邮箱:xy-zhang#163.com
    * android开发进阶群:278401545
    *
    */

  • 相关阅读:
    卷积操作特征图输出大小
    python函数式编程
    python函数
    列表和元组
    Crontab 执行时没有环境变量!
    github 解决 Agent admitted failure to sign using the key
    Lightgbm 随笔
    btrfs-snapper 实现Linux 文件系统快照回滚
    Ansible playbook API 开发 调用测试
    Linux终端下安装jdk
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4500224.html
Copyright © 2011-2022 走看看