Android Studio怎么用JNI编写出Hello World

搭建环境

搭建好android studio的ndk环境其实就是一行代码的问题:在你的local.properties中加入这一行代码就好前提是你已经下载好了ndk的包

ndk.dir=/Users/NingSo/android-ndk-r10e 

JNI 开发流程主要分为以下 6 步:

  • 编写声明了 native 方法的 Java 类
  • 将 Java 源代码编译成 class 字节码文件
  • 将 Java 源代码编译成 class 字节码文件
  • 用 javah -jni 命令生成.h头文件javah 是 jdk 自带的一个命令,-jni 参数表示将 class 中用native 声明的函数生成 JNI 规则的函数
  • 用本地代码实现.h头文件中的函数注意实现函数时参数要自己定义声明
  • 执行ndk-build 将本地写好的C代码编译成动态库
  • 拷贝动态库至 java.library.path 本地库搜索目录下,并运行 Java 程序
    《Android Studio怎么用JNI编写出Hello World》 Paste_Image.png
    通过上面的介绍,相信大家对 JNI 及开发流程有了一个整体的认识,下面通过一个 HelloWorld 的示例,再深入了解 JNI 开发的各个环节及注意事项.

代码演示

第一步:

在自己项目中创建一个包含native的方法类HelloWorld.java –>包名com.ningso.ningsodemo

public class HelloWorld {    
    public native String sayHello(String name); // 1.声明这是一个native函数,由本地代码实现    
    static {
        System.loadLibrary("hello");   // 2.加载实现了native函数的动态库,只需要写动态库的名字    
  }
}
第二步:

在终端执行javac命令将.java源文件编译成.class字节码文件

《Android Studio怎么用JNI编写出Hello World》 执行javac命令

-d 表示将编译后的class文件放到指定的文件夹下面

结果图:

《Android Studio怎么用JNI编写出Hello World》 编译后的字节码文件

以上也可以直接执行./gradlew build 不过文件就要自己去找了,该class在你的
/app/build/intermediates/classes/debug文件夹对应的包名下

第三步:

继续在终端执行javah -jni命令 根据class文件生产.h头文件

《Android Studio怎么用JNI编写出Hello World》 Paste_Image.png

注意:-d和-o只能使用其中一个参数。
参数说明:classpath:类搜索路径,这里表示从当前的 bin 目录下查找
-d:将生成的头文件放到当前的 jni 目录下
-o: 指定生成的头文件名称,默认以类全路径名生成(包名+类名.h)

结果图:

《Android Studio怎么用JNI编写出Hello World》 对应的两条命令生产的.h文件,位置不同,名称不同而已。但是内容一样

Hello.h内容:

/* DO NOT EDIT THIS FILE - it is machine generated *
/#include <jni.h>
/* Header for class com_ningso_ningsodemo_HelloWorld */
#ifndef _Included_com_ningso_ningsodemo_HelloWorld
#define _Included_com_ningso_ningsodemo_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/* * Class:     com_ningso_ningsodemo_HelloWorld 
* Method:    sayHello 
* Signature: (Ljava/lang/String;)Ljava/lang/String; 
*/
JNIEXPORT jstring JNICALL Java_com_ningso_ningsodemo_HelloWorld_sayHello 
(JNIEnv *, jobject, jstring);

#ifdef __cplusplus}
#endif
#endif
第四步:

在main目录下新建一个jin文件夹将.h文件拖至此目录下并在这做如下操作:

  • 实现.h中的函数
#include "hello.h"
#include <stdio.h>
#ifdef __cplusplh
extern "C" {
#endif
/* * Class:     com_ningso_ningsodemo_HelloWorld * Method:    sayHello * Signature: (Ljava/lang/String;)Ljava/lang/String; 
*/
JNIEXPORT jstring JNICALL Java_com_ningso_ningsodemo_HelloWorld_sayHello
(JNIEnv *env, jclass cls, jstring j_str) 
 {
 const char *c_str = NULL;     
 char buff[128] = { 0 };      
c_str = (*env)->GetStringUTFChars(env, j_str, NULL);      
if (c_str == NULL) {          
printf("out of memory.\n");          
return NULL;      
}      
printf("Java Str:%s\n", c_str);
sprintf(buff, "hello %s", c_str);
(*env)->ReleaseStringUTFChars(env, j_str, c_str);  
return (*env)->NewStringUTF(env, buff);  
}
#ifdef __cplusplus}
#endif
  • 编写Android.mk文件 内容如下:
LOCAL_PATH      := $(call my-dir)   ## 定义 LOCAL_PATH 环境变量为本文件的目录,mydir 表示当前目录。
include $(CLEAR_VARS)               ## 清除除了 LOCAL_PATH 以外其他的 LOCAL_ 环境变量
LOCAL_MODULE    := hello            ## 动态库名字为hello
LOCAL_SRC_FILES := hello.c          ## 源文件名字
include $(BUILD_SHARED_LIBRARY)     ## 编译生成共享动态库
  • 编写Application.mk文件内容如下:
APP_ABI := all   ## 表示生成所有平台的动态库。

结果图:

《Android Studio怎么用JNI编写出Hello World》 编译成功的结果图

第五步:

在终端cd进入到jni目录下,执行ndk-build命令:

《Android Studio怎么用JNI编写出Hello World》 编译成功的结果显示图

然后回到项目main目录下会发现已经生产好的so文件躺在那等你调用了。

《Android Studio怎么用JNI编写出Hello World》 看!!!结果!!

最后运行apk了

在你的代码里面执行如下代码:

HelloWorld helloWorld = new HelloWorld();
textView.setText(helloWorld.sayHello("终于运行起来了。"));

《Android Studio怎么用JNI编写出Hello World》 完结

参考

    原文作者:ningso
    原文地址: https://www.jianshu.com/p/15c27cc09272
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞