31

I'm trying to port Jnetpcap to Android in order to use it for parsing .pcap files. Jnetpcap is a java wrapper for libpcap which uses JNI. I have compiled libpcap as a static library using the android's source code tree.

When compiling Jnetpcap as a shared library I'm getting errors because I have to link with libpcap.a but I don't know how could I tell Android.mk that he must link with the libpcap.a file that I have.

Using "LOCAL_STATIC_LIBRARIES:= libpcap" won't work because libpcap doesn't come by default in the android NDK.

If any of you guys could help me I would be very thankful.

Here is my current Android.mk file:

include /home/sergio/workspace/MyApp/jni/libpcap/Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := jnetpcap

LOCAL_SRC_FILES :=\
    jnetpcap.cpp\
    packet_flow.cpp\
    packet_jheader.cpp\
    jnetpcap_pcap_header.cpp\
    nio_jbuffer.cpp\
    winpcap_stat_ex.cpp\
    winpcap_send_queue.cpp\
    winpcap_ext.cpp\
    jnetpcap_ids.cpp\
    jnetpcap_dumper.cpp\
    jnetpcap_utils.cpp\
    util_in_cksum.cpp\
    jnetpcap_beta.cpp\
    nio_jmemory.cpp\
    packet_jsmall_scanner.cpp\
    packet_protocol.cpp\
    nio_jnumber.cpp\
    packet_jheader_scanner.cpp\
    library.cpp\
    packet_jscan.cpp\
    jnetpcap_pcap100.cpp\
    util_checksum.cpp\
    packet_jpacket.cpp\
    winpcap_ids.cpp\
    jnetpcap_bpf.cpp

LOCAL_C_INCLUDES := /home/sergio/android-ndk-r5b/platforms/android-8/arch-arm/usr/include /home/sergio/workspace/Shark/jni/libpcap

LOCAL_STATIC_LIBRARIES := libpcap

include $(BUILD_SHARED_LIBRARY)
Jimix
  • 1,539
  • 3
  • 15
  • 20

2 Answers2

44

Finally solved it. My Android.mk code:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE    := libpcap 
LOCAL_SRC_FILES := libpcap.a

include $(PREBUILT_STATIC_LIBRARY)


include $(CLEAR_VARS)

LOCAL_MODULE := jnetpcap

LOCAL_SRC_FILES :=\
    jnetpcap.cpp\
    packet_flow.cpp\
    packet_jheader.cpp\
    jnetpcap_pcap_header.cpp\
    nio_jbuffer.cpp\
    winpcap_stat_ex.cpp\
    winpcap_send_queue.cpp\
    winpcap_ext.cpp\
    jnetpcap_ids.cpp\
    jnetpcap_dumper.cpp\
    jnetpcap_utils.cpp\
    util_in_cksum.cpp\
    jnetpcap_beta.cpp\
    nio_jmemory.cpp\
    packet_jsmall_scanner.cpp\
    packet_protocol.cpp\
    nio_jnumber.cpp\
    packet_jheader_scanner.cpp\
    library.cpp\
    packet_jscan.cpp\
    jnetpcap_pcap100.cpp\
    util_checksum.cpp\
    packet_jpacket.cpp\
    winpcap_ids.cpp\
    jnetpcap_bpf.cpp

LOCAL_C_INCLUDES :=\
    /home/sergio/android-ndk-r5b/platforms/android-8/arch-arm/usr/include\
    /home/sergio/workspace/MyApp/jni/libpcap

LOCAL_STATIC_LIBRARIES := libpcap

include $(BUILD_SHARED_LIBRARY)
Jimix
  • 1,539
  • 3
  • 15
  • 20
13

You have to build pcap as static module. Something like this

include $(CLEAR_VARS)
LOCAL_MODULE := pcap
LOCAL_CFLAGS := declare_flags
LOCAL_C_INCLUDES := declare_include
LOCAL_SRC_FILES := src_files
include $(BUILD_STATIC_LIBRARY)

This build pcap as local module which you could link with LOCAL_STATIC_LIBRARIES

just add

LOCAL_STATIC_LIBRARIES := pcap

before calling

include $(BUILD_SHARED_LIBRARY)
Mojo Risin
  • 8,136
  • 5
  • 45
  • 58
  • Thanks for your help. I tried to include the Android.mk file of libpcap in my Android.mk so I can use libpcap. Now I'm getting errors because the compiler is trying to find my source files in the /build/core/ directory. I have updated the Android.mk file which I had posted here. – Jimix Mar 29 '11 at 15:41
  • You don't have to move your Android.mk file just fix the paths to point to the source and header files of pcap library. – Mojo Risin Mar 29 '11 at 16:38
  • Thank you for your help. I have fixed it with include $(PREBUILT_STATIC_LIBRARY). Seems that the errors I was getting weren't caused for not linking with libpcap. The compiler says some functions weren't declared in this scope: pcap_create, pcap_activate, pcap_can_set_rfmon, pcap_set_buffer_size, pcap_set_promisc, pcap_set_snaplen, pcap_set_timeout. I think that maybe the libpcap version for android doesn't have them. – Jimix Mar 29 '11 at 17:15