Continue my previous avahi article, here, i would port the same example code to Android platform.
Download and unravel Android NDK
for me, the untarred NDK root locates in ~/Android/AndroidNDK/android-ndk-r10e
And install the android tools in the linux system. (yum for fedora, apt-get for ubunu/debian)
I strongly suggest you DO NOT install the others android-tool, headers and libraries via apt-get/yum.
If you need those, just download from Android website and decompress them.
Download and untar the avahi libraries, in here I use version 0.6.31.
Download 2 patches and place them in the untarred avahi folder:
apply the patches.
You would encounter a warning here, just ignore it. it is for there is no .git folder inside the avahi folder.
For the old configuration tools does not support androideabi, you need to upgrade the config.guess and config.sub from here. it is, to overwrite these 2 files inside the avahi root folder.
The struct in6_pktinfo has been added in Android 4.0(2011/10). When the avahi 0.6.31 was released (2012/2), the BioniC (the position of glibC for Android) libraries on most populace's Android phone was not implemented the whole IVP6. Therefore, the structure ha been declare inside avahi. In present(2015/12), I thought there is no one set build-target version before 4.0. It is requisite to modify code to remove the obsolete lines in avahi-core/socket.c , about line 67:
The in6_pktinfo has been declared in NDKfolder/platforms/android-version/arch/usr/include/linux/ipv6.h, to comment out it to avoid compilation error.
if your application would be integrated web-browser and avahi disovering functions, you should modify the code avahi-core/netlink.c, about line 160:
It is, the add.nl_pid value would not be conflicted with browser's. More detail of this change is in here.
The Avahi configure arguments for me be:browser
I disable most functions of Avahi. Because those are useless for the goal of discovering mDNS service and dependent on dbus, gtk, qt and so forth libraries. To building those depended libraries are difficult for Android.
The path, /home/gaiger/Android/AndroidNDK/android-ndk-r10e/platforms/android-14/arch-arm/usr, is where the Android system libraries locates, those are being part of Android NDK.
/home/gaiger/Android/AndroidNDK/android-ndk-r10e : where AndroidNDK root locates.
platforms/android-14/arch-arm : I chose to android 4.0 with arm-architecture as my target system.
it is very natural to add include path (-Iusr_path/include) and linking path(-Iusr_path/lib) as arguments in cross-compilation.
-mcpu=arm926ej-s -mfloat-abi=softfp : To avoid crash in some (mostly those are not existed ) cheap Android device CPU without hardware floating computing. Besides, Avahi do not involve in heavy floating point computing, So adapt pure soft-float computing would not lead mDNS discovering sluggish.
arm926ej-s means, assuming the CPU does not support hardware floating but supports Jazelle ( for java VM).
More detail of CPU architecture, you could refer to this article.
--with-distro=debian : for me , I use ubuntu. If you use fedora/CentOS, you use set it as fedora.
--disable-stack-protector : there is no libssp (stack smash protector) in current BioniC defaultly. To advert configuring error, one should disable this function. The detail about libssp is in here.
After configuring done, it is very instinctive to press make and make install.
To this step, the avahi libraries for Android has been built, in the avahi_root/built
Create a folder and a sub-folder in avahi root: sandyboxcode/jni
Copy the code sandbox-discover-standalone.c of my last article in folder sandyboxcode/jni.
And Create two files, Application.mk and Android.mk in the same folder ( sandyboxcode/jni).
The content of file Application.mk be:
The Android.mk file be:
Then to the sandbox folder, type :
Now you have gotten binary of sandbox-discover-standalone for Android, under the folder libs/armeabi .
Push the built binary to a android device, which might need to be rooted to execute a binary in shell.
For me, I use the development board tiny210 on Android 4.0, which was rooted natively.
check if the device connected to working computer indeed:
push the binary to the android :
execute it in the shell:
It is successful totally.
Known insufficiency :
if you encounter Segmentation fault when you execute the binary in Android,
you should correct the code in sandbox-discover-standalone.c, of function ServiceResolverCallback:
It could avoid the crashing:
It is passible to write a stand Makefile for Android ndk build :
i assure the Makefile be in sandyboxcode/Makefile (NOT inside the jni folder), for the sandbox-discover-sandalone.c be :
Note : the shared libraries liblog.so be linking directly!.