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. static {
  3. System.loadLibrary("com_nedu_jni_helloword_printf-jni");
  4. }
  5. public native void printHello();
  6. }

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. #ifndef _Included_com_nedu_jni_helloword_Printf_Jni
  5. #define _Included_com_nedu_jni_helloword_Printf_Jni
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. /*
  10. * Class:     com_nedu_jni_helloword_Printf_Jni
  11. * Method:    printHello
  12. * Signature: ()V
  13. */
  14. JNIEXPORT void JNICALL Java_com_nedu_jni_helloword_Printf_1Jni_printHello
  15. (JNIEnv *, jobject);
  16. #ifdef __cplusplus
  17. }
  18. #endif
  19. #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. include $(CLEAR_VARS)
  3. LOCAL_MODULE    := com_nedu_jni_helloword_printf-jni
  4. LOCAL_SRC_FILES :=Printf_Jni.c
  5. 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
*
*/

最新文章

  1. jquery简介和实例
  2. socket.io 中文手册 中文文档
  3. noip模拟赛(一)宠物之战
  4. 两台SQL Server数据同步解决方案
  5. 深入浅出MySQL双向复制技术
  6. oracle修改列的类型
  7. 个人分享:平时开发中感觉几款不错 IDE 、插件、工具
  8. Serv-U软件在64位操作系统下使用不了odbc解决方法
  9. Golang下通过syscall调用win32的dll(calling Windows DLLs from Go )
  10. 兔子--Spring基金会
  11. Rosenbrock function
  12. [译][待续]Chap1.Using neural nets to recognize handwritten digits
  13. 3、Web应用程序中的安全向量 -- cookie盗窃
  14. Delete from join 用法
  15. Centos解除端口占用
  16. eclipse导入Tomcat8源码
  17. eclipse取消自动输入提示
  18. Android动画总结
  19. 基于 CentOS Mysql 安装与主从同步配置详解
  20. Dedecms5.7搜索结果页空白无内容的解决方法

热门文章

  1. leetcode Longest Substring Without Repeating Characters python
  2. 新浪sae 项目之 git 配置
  3. for循环产生的Cortex-M3汇编代码的一个奇怪现象
  4. android-数据持久化
  5. asp.net core + angular2 的环境配置
  6. Effective C++学习笔记——构造/析构/拷贝运算
  7. Google市场推广统计
  8. CSS3学习----选择器、字体
  9. Primefaces 中e.offset(...)问题的处理
  10. 23种设计模式的C++实现