我是NDK的新手。我正在尝试创建一个可以捕获数据包的应用程序。我已经从https://github.com/the-tcpdump-group/libpcap
编译了libpcap现在,当我尝试在Android平板电脑上运行该应用程序时,它会出现以下错误
07-24 02:29:50.627: E/AndroidRuntime(2014): FATAL EXCEPTION: main
07-24 02:29:50.627: E/AndroidRuntime(2014): Process: com.example.lpcap, PID: 2014
07-24 02:29:50.627: E/AndroidRuntime(2014): java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "cmsg_nxthdr" referenced by "libpcap.so"...
07-24 02:29:50.627: E/AndroidRuntime(2014): at java.lang.Runtime.loadLibrary(Runtime.java:364)
07-24 02:29:50.627: E/AndroidRuntime(2014): at java.lang.System.loadLibrary(System.java:526)
07-24 02:29:50.627: E/AndroidRuntime(2014): at com.example.lpcap.MainActivity.<clinit>(MainActivity.java:37)
07-24 02:29:50.627: E/AndroidRuntime(2014): at java.lang.Class.newInstanceImpl(Native Method)
07-24 02:29:50.627: E/AndroidRuntime(2014): at java.lang.Class.newInstance(Class.java:1208)
07-24 02:29:50.627: E/AndroidRuntime(2014): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-24 02:29:50.627: E/AndroidRuntime(2014): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
07-24 02:29:50.627: E/AndroidRuntime(2014): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-24 02:29:50.627: E/AndroidRuntime(2014): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-24 02:29:50.627: E/AndroidRuntime(2014): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-24 02:29:50.627: E/AndroidRuntime(2014): at android.os.Handler.dispatchMessage(Handler.java:102)
07-24 02:29:50.627: E/AndroidRuntime(2014): at android.os.Looper.loop(Looper.java:136)
07-24 02:29:50.627: E/AndroidRuntime(2014): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-24 02:29:50.627: E/AndroidRuntime(2014): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 02:29:50.627: E/AndroidRuntime(2014): at java.lang.reflect.Method.invoke(Method.java:515)
07-24 02:29:50.627: E/AndroidRuntime(2014): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-24 02:29:50.627: E/AndroidRuntime(2014): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-24 02:29:50.627: E/AndroidRuntime(2014): at dalvik.system.NativeStart.main(Native Method)
ndk中的socket.h有这个符号。我不确定它是否正在编译。我必须手动添加它,我该怎么办呢?以下是Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
libpcap_PSRC = pcap-linux.c pcap-usb-linux.c pcap-can-linux.c pcap-netfilter-linux.c
libpcap_FSRC = fad-gifc.c
libpcap_CSRC = pcap.c inet.c gencode.c optimize.c nametoaddr.c etherent.c \
savefile.c sf-pcap.c sf-pcap-ng.c pcap-common.c \
bpf_image.c bpf_dump.c
libpcap_GENSRC = scanner.c grammar.c bpf/net/bpf_filter.c version.c lpcap.c
libpcap_SRC = $(libpcap_PSRC) $(libpcap_FSRC) $(libpcap_CSRC) $(libpcap_GENSRC)
LOCAL_SRC_FILES:=\
$(libpcap_SRC)
LOCAL_SHARED_LIBRARIES := libc cutils
LOCAL_CFLAGS:=-O2 -g
LOCAL_CFLAGS+= -DSYS_ANDROID=1 -Dyylval=pcap_lval -DHAVE_CONFIG_H -D_U_="__attribute__((unused))" -Dlinux -D__GLIBC__
#-D_GNU_SOURCE
LOCAL_LDLIBS := -ldl -lc
LOCAL_MODULE:= libpcap
include $(BUILD_SHARED_LIBRARY)
我使用
在java文件中加载它static{
System.loadLibrary("pcap");
}
我还没有打电话给任何方法。我只是希望它能够毫无问题地加载库。请帮我解决这个问题。
答案 0 :(得分:0)
将预编译的libpcap移动到JNI文件夹。如果要定位多个体系结构,则需要在JNI中创建与每个体系结构(armeabi,x86等)对应的多个文件夹,并相应地放置.so文件。修改您的清单文件,如下所示,以包含您的预建共享库
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libpcap
LOCAL_SRC_FILES := libpcap.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := projectname
LOCAL_SRC_FILES := projectname.cpp
include $(BUILD_SHARED_LIBRARY)
projectname对应libpcap的模块,projectname.cpp对应于实际的源文件