Question

I've been doing a lot of attempts to get libpcap compiled for Android, and I don't see any pattern or any progress worth writing down.

I have a very simple sniffer (that works fine in a MIPS linux) that uses libpcap, so I thought to myself oh, ok... no biggie... I'll just compile libpcap for Android (in the end, Android is just a Linux)... and here's where the problems started. I have no idea on how to compile libpcap using ndk-build and the Android.mk and all that infrastructure.

I have the Android NDK in a directory. That NDK has the toolchains built (I have a lot of directories under ~/Documents/Projects/Android_NDK/toolchains/ ) but none of the toolchains has libpcap available.

I've tried with two different libpcap version or... branches:

The Android one, which is the one I'd like to use, https://android.googlesource.com/platform/external/libpcap/

and the regular one: http://www.tcpdump.org/release/libpcap-1.5.3.tar.gz

All tries I've done have been very unsuccessful. I've seen the question Android NDK: Link using a pre-compiled static library which is similar, but I'm still getting various errors.

I have downloaded those pcap libraries to their own directories. Maybe is that the problem? Do I need to put the Android libpcap in some directory within the NDK root directory and re-create the toolchains?

I'm using NDK-r9 on a MacOSX 10.9.2 64bit.

Was it helpful?

Solution

Finally!!

After getting annoyed by the non existing headers in and stuff like that, I found this question, that pointed to a SVN repo (http://sourceforge.net/p/prueba-android/code/HEAD/tree/trunk/jni/) with a libpcap that compiled!

If someone else wants additional details on how my Android.mk and directory structure looks like, please add a comment and I'll extend this answer.

OTHER TIPS

Please read an excellent article at http://blog.umitproject.org/2011/05/libpcap-for-android.html. There are instructions that will help you link to libpcap, but the most important takeaway is that you cannot use libpcap on non-rooted Android. So maybe it's not worth your effort.

On a rooted device, you can simply install a free sniffer like Shark for Root.

If anyone else is having problems compiling libpcap for Android using the NDK, there is version 1.5.2 here with a built Android.mk file in it: https://android.googlesource.com/platform/external/libpcap.git and instructions for compiling this using the NDK are here: http://ducbh.blogspot.co.uk/2013/12/cross-compile-libpcap-for-android.html . I can confirm this works using the current NDK (r10b)...although you may have to add AndroidManifest.xml (blank) and and Application.mk that points to your Android.mk file.

I don't think it would be that difficult to modify the .mk file for the current libpcap version (1.6.2)

In case anyone ends up here in 2022+. You can now cross-compile for Android from the official source. Steps:

First, setup the NDK: Download Android NDK (I used r21e) and extract to a directory of your choosing:

$ cd ~
$ mkdir Android
$ unzip android-ndk-r21e-linux-x86_64.zip

Prepare the environment variables for cross-compilation by placing the commands below (replace <YOUR_USER>) into a file named setup_env.sh (can be saved anywhere):

export NDK=/home/<YOUR_USER>/Android/android-ndk-r21e
export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
export TARGET=aarch64-linux-android
export API=21
export AR=$TOOLCHAIN/bin/$TARGET-ar
export AS=$TOOLCHAIN/bin/$TARGET-as
export CC=$TOOLCHAIN/bin/$TARGET$API-clang
export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
export LD=$TOOLCHAIN/bin/$TARGET-ld
export RANLIB=$TOOLCHAIN/bin/$TARGET-ranlib
export STRIP=$TOOLCHAIN/bin/$TARGET-strip

Now, build libpcap:

  1. Download libpcap tar ball (e.g. https://www.tcpdump.org/release/libpcap-1.10.1.tar.gz)
  2. Extract (in a directory of your choosing): tar xf libpcap-1.10.1.tar.gz
  3. Prepare your env: source <path_to>/setup_env.sh
  4. Change into the extract libpcap directory and configure: ./configure --host=aarch64-linux-android
  5. I found the Makefile generated had the wrong linker set (line 48), so I had to change it to: LD = /home/<YOUR_USER>/Android/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/x86_64-linux-android/bin/ld
  6. Finally, build: make

This build worked for me with running tcpreplay on a rooted Android, hopefully it works for other purposes as well!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top