前言
做Android开发的程序员应该都知道,Android的开发语言我们都是在使用JAVA(Kotlin和Flutter我们暂时不考虑)。但是,有时候我们也需要使用到C语言进行一些功能的开发。这个时候我们就需要用到JNI了。
1.导入C语言的类
首先我们需要把C语言写的功能类放入我们的项目中。这里我直接从资料中找了一个,毕竟我不会写。路径在src/main/jni中
find_name.cpp
#include <jni.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <netdb.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/select.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define send_MAXSIZE 50 #define recv_MAXSIZE 1024 struct NETBIOSNS { unsigned short int tid; //unsigned short int 占2字节 unsigned short int flags; unsigned short int questions; unsigned short int answerRRS; unsigned short int authorityRRS; unsigned short int additionalRRS; unsigned char name[34]; unsigned short int type; unsigned short int classe; }; char *getNameFromIp(const char *ip); extern "C" jstring Java_com_hao_cmake_MainActivity_cpuFromJNI(JNIEnv* env, jobject thiz, jstring ip) { const char* str_ip; str_ip = env->GetStringUTFChars(ip, 0); return env->NewStringUTF(getNameFromIp(str_ip)); } char *getNameFromIp(const char *ip) { char str_info[1024] = { 0 }; struct sockaddr_in toAddr; //sendto中使用的对方地址 struct sockaddr_in fromAddr; //在recvfrom中使用的对方主机地址 char send_buff[send_MAXSIZE]; char recv_buff[recv_MAXSIZE]; memset(send_buff, 0, sizeof(send_buff)); memset(recv_buff, 0, sizeof(recv_buff)); int sockfd; //socket unsigned int udp_port = 137; int inetat; if ((inetat = inet_aton(ip, &toAddr.sin_addr)) == 0) { sprintf(str_info, "[%s] is not a valid IP address\n", ip); return str_info; } if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { sprintf(str_info, "%s socket error sockfd=%d, inetat=%d\n", ip, sockfd, inetat); return str_info; } bzero((char*) &toAddr, sizeof(toAddr)); toAddr.sin_family = AF_INET; toAddr.sin_addr.s_addr = inet_addr(ip); toAddr.sin_port = htons(udp_port); //构造netbios结构包 struct NETBIOSNS nbns; nbns.tid = 0x0000; nbns.flags = 0x0000; nbns.questions = 0x0100; nbns.answerRRS = 0x0000; nbns.authorityRRS = 0x0000; nbns.additionalRRS = 0x0000; nbns.name[0] = 0x20; nbns.name[1] = 0x43; nbns.name[2] = 0x4b; int j = 0; for (j = 3; j < 34; j++) { nbns.name[j] = 0x41; } nbns.name[33] = 0x00; nbns.type = 0x2100; nbns.classe = 0x0100; memcpy(send_buff, &nbns, sizeof(nbns)); int send_num = 0; send_num = sendto(sockfd, send_buff, sizeof(send_buff), 0, (struct sockaddr *) &toAddr, sizeof(toAddr)); if (send_num != sizeof(send_buff)) { sprintf(str_info, "%s sendto() error sockfd=%d, send_num=%d, sizeof(send_buff)=%d\n", ip, sockfd, send_num, sizeof(send_buff)); shutdown(sockfd, 2); return str_info; } int recv_num = recvfrom(sockfd, recv_buff, sizeof(recv_buff), 0, (struct sockaddr *) NULL, (socklen_t*) NULL); if (recv_num < 56) { sprintf(str_info, "%s recvfrom() error sockfd=%d, recv_num=%d\n", ip, sockfd, recv_num); shutdown(sockfd, 2); return str_info; } //这里要初始化。因为发现linux和模拟器都没问题,真机上该变量若不初始化,其值就不可预知 unsigned short int NumberOfNames = 0; memcpy(&NumberOfNames, recv_buff + 56, 1); char str_name[1024] = { 0 }; unsigned short int mac[6] = { 0 }; int i = 0; for (i = 0; i < NumberOfNames; i++) { char NetbiosName[16]; memcpy(NetbiosName, recv_buff + 57 + i * 18, 16); //依次读取netbios name if (i == 0) { sprintf(str_name, "%s", NetbiosName); } } sprintf(str_info, "%s|%s|", ip, str_name); for (i = 0; i < 6; i++) { memcpy(&mac[i], recv_buff + 57 + NumberOfNames * 18 + i, 1); sprintf(str_info, "%s%02X", str_info, mac[i]); if (i != 5) { sprintf(str_info, "%s-", str_info); } } return str_info; }
这里要注意一点,jstring Java_com_hao_cmake_MainActivity_cpuFromJNI方法中,com_hao_cmake是我们的包名,MainActivity是调用JNI的Activity名称,cpuFromJNI是对应方法的名字。
2.接着导入Android.mk文件
这个文件也是放在jni文件夹中
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # 指定so库文件的名称 LOCAL_MODULE := jni_mix # 指定需要编译的源文件列表 LOCAL_SRC_FILES := find_name.cpp # 指定C++的编译标志 LOCAL_CPPFLAGS += -fexceptions # 指定要加载的静态库 #LOCAL_WHOLE_STATIC_LIBRARIES += android_support # 指定需要链接的库 LOCAL_LDLIBS := -llog include $(BUILD_SHARED_LIBRARY) $(call import-module, android/support)
3.我们配置一下build.gradle文件
android -> defaultConfig 下添加
externalNativeBuild{ ndkBuild{ abiFilters "arm64-v8a","armeabi-v7a" } }
android 下添加
externalNativeBuild { ndkBuild { path file('src/main/jni/Android.mk') } } packagingOptions{ pickFirst 'lib/arm64-v8a/libjni_mix.so' pickFirst 'lib/armeabi-v7a/libjni_mix.so' }
4.好了,此时可以编译一下项目了
5.此时我们可以找一下我们生成的so包了
在build → intermediates → ndkBuild → debug → obj → local下,我们可以找到我们生成的相关配置平台的so文件
6.将生成的so文件拷入src/main/jniLibs中
这个样子的
7.调用C语言方法的Activity如下
public class MainActivity extends AppCompatActivity { public native String cpuFromJNI(String ip); static { System.loadLibrary("jni_mix"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String str = cpuFromJNI("192.168.0.163"); Toast.makeText(this,str,Toast.LENGTH_SHORT).show(); } }
这样我们就完成了用C语言类生成so包,并使用JNI进行调用的全流程。
注意:在使用JNI进行调用的时候,我们的环境一定要有NDK,这个我这里就不说了,大家如果没有搭建需要上网找找搭建一下。
总结
到此这篇关于Android中JNI使用的文章就介绍到这了,更多相关Android中JNI使用内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!
- 本文固定链接: https://zxbcw.cn/post/218946/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)