笔记:
window-preferences-android-ndk 路径设置好D:\android-ndk-r10e
实际操作:
在addd里面创建一个安卓项目
在mainactivity.java
oncreat中:toast.maketext(this,Getstring(), toast.LENGTH_SHORT).show();
public native CharSequence Getstring();
右键src属性:-》properties获取路径
然后cmd到获取路径
javah
javah -jni (被native修饰方法所在类的具体路径) 【然后回车】
然后右键项目刷新(refresh);#出现com_example_文件
创建文件,new-folder-jni文件
生成.so文件使用include $(BUILE_SHARED_LIBRIRY)#编译类型
.so生成后“
static{
System.loadLibrary(“JNI_study”);
}
实际操作:
操作前准备:查看是否配置ndk‘’
![ndk](https://www.ctvol.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
创建一个安卓项目:
![ndk](https://www.ctvol.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
###编写代码:
![ndk代码](https://www.ctvol.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
然后右键ndkfour项目名–>点击属性–>Location-》找到路径。
打开cmd:
先进入相应的目录比如:E:
然后路径:cd E:\workspaceHeiMa\term38\day5\ndkfour\src
找到函数对应类路径:com.example.ndkfour.MainActivity(包名 +.MainActivity)
然后在cmd中键入:javah -jni com.example.ndkfour.MainActivity
然后到编辑器中:右键项目包或者F5;
即可生成:
![jni](https://www.ctvol.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
然后右键项目包创建folder–>jni->生成的文件扔入其中
![计算机技术网(www.ctvol.com)!!](https://www.ctvol.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
android.mk代码有变动:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := houzi #模块名称
LOCAL_SRC_FILES := houzi.c #源文件 .c或者.cpp
LOCAL_ARM_MODE := arm #编译后的指令集 ARM指令
LOCAL_LDLIBS += -llog #依赖库
include $(BUILD_SHARED_LIBRARY) #指定编译文件的类型
houzi.c文件代码:
#include "houzi.h"
JNIEXPORT jobject JNICALL Java_com_example_ndkfour_MainActivity_Getstring
(JNIEnv *env, jobject object){
jstring ss = (*env)->envNewStringUTF(env , "houzifeiqilai");
return ss;
}
![QQ图片20200416004103.png](https://www.ctvol.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
找到jni路径,到cmd当中cd到目录。
ndk-build 即可生成.so文件
![houzis文件](https://www.ctvol.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
导入.so文件;然后即可运行;
![ndkso文件导入](https://www.ctvol.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
运行成功:
![最后效果](https://www.ctvol.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
该文章属于:计算机技术网原创
转载请注明标题:jni接口如何获取c层定义字符并弹窗显示-计算机技术网
转载请注明出处:https://www.ctvol.com/asreverse/22715.html