JNI生成so文件

参考:
https://developer.android.com/ndk/downloads/index.html
https://blog.csdn.net/LJyLy525/article/details/84305328

第1步:新建一个Android Studio 工程 JniHelloWorld。新建一个MyJni.java文件。
在这里插入图片描述

MyJni.java

public class MyJni {

    static {
        System.loadLibrary("MyJni");
    }

    public native static String getString();
}

第2步:然后点击一下 make project 会在app的build目录下面生成.class文件。
在这里插入图片描述

第3步,在app/src/main文件夹下新建一个jni文件夹,然后打开Android Studio的终端,cd到这个目录,然后输入下面的指令

javah -jni -classpath D:\github\JniHelloWorld\app\build\intermediates\classes\debug com.brotherd.jnihelloworld.MyJni

就会在这个jni文件夹下生成一个.h文件,com_brotherd_jnihelloworld_MyJni.h,文件内容如下。

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_brotherd_jnihelloworld_MyJni */

#ifndef _Included_com_brotherd_jnihelloworld_MyJni
#define _Included_com_brotherd_jnihelloworld_MyJni
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_brotherd_jnihelloworld_MyJni
 * Method:    getString
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_brotherd_jnihelloworld_MyJni_getString
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

在jni目录下新建一个c/c++source file ,取名test.c 实现上面.h文件中的方法。

#include "jni.h"
#include "com_brotherd_jnihelloworld_MyJni.h"

JNIEXPORT jstring JNICALL Java_com_brotherd_jnihelloworld_MyJni_getString
  (JNIEnv *env, jclass jz){

  return (*env)->NewStringUTF(env,"this is the first time for me to use jni");

  }

配置:
在这里插入图片描述
编译完文件后,在build–>intermediates–>ndk目录下面找到Android.mk文件,copy到jni目录下;

androidStuido Terminal命令行进入到"main"层级目录下执行ndk-build 会生成你想要的so文件。

1、如果在build 的过程中出现错误,在android.mk 文件中找到

include $(CLEAR_VARS)  这一行,

在下面增加一行:

LOCAL_LDLIBS    := -lm -llog 

之后注释以下配置,将生成os文件的libs文件夹名称改为jniLibs即可。

在这里插入图片描述

删除jni文件,或者将文件名jni改成libs,保留文件方便以后修改。
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值