實例詳解Android中JNI的使用方法

前言

做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使用內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: