Monday, May 21, 2012

Java-ZeroMQ for Android

Those last weeks at work, I have been busy on a project that had several subtopics.

Among them, was to use ZeroMQ sockets with an Android phone.

That should have been simple, since ZeroMQ has published a page that explains how to realize this.

Unfortunately, those instructions are not as effective as they promise. And I am not the onliest to have met issues.

I'm not sure if there is a chance that the libraries can actually cross-compile properly with the method exposed on the official Wiki, but it looked unlikely to.

Furthermore, the page only explains how to generate the single libzmq.so and not its JNI port (and high-level improvements) : jzmq.

The goal then was to generate a self-contained binary that embedded the JNI port functions, the ZeroMQ library itself and its libuuid dependency.

And finally, since Android APKs only accept native libraries that end up with .so (no version numbers) it was also mandatory to get the Makefiles to generate a version-free binary (avoid soname definition)

After a few hunches (well, in fact, a lot) I came up with an almost-proper solution : a collection of scripts to ./configure the three interdependent packages  (e2fsprogs, zeromq and jzmq) without modification and have them together generate the native library and the associated jar easily, with the help of a top-level Makefile.

It also downloads the stable packages from their www location.

I said "almost" proper, because the real clean solution would be to intervene to modify the Wiki, propose patches, etc... But you know, it came up like this, and maybe that will be integrated to ZeroMQ in a nicer way in the future if that's the way to go.

Issues mostly came from Jzmq though, since it has not been very actively maintained since almost a year now, and has a few incompatibilities with the new autotools suite.

The final solution is usable as a one-liner : export [...] && make.

It generates a zmq.jar file that is directly usable in your Android app, which automatically loads the libjzmq.so that you have also generated and wisely copied in the libs/armeabi folder of the same application.

See the code on github, and enjoy !

Please report issues and further improvements if you care.

References

Thanks to Mikael Le Goff for the first tests and support with this.
Thanks to Dave for his post that helped me fix a few last issues a lot quicker !

1 comment:

  1. http://www.zeromq.org/build:android has been updated June 6th. I started from your instruction before than, and I am trying to run python "import zmq". I got stuck with not being able to load uuid (that could haev to be checked out into e2fsprogs-1.43.1, which I did not do). I am starting over again from the new instructions on http://www.zeromq.org/build:android and now trying to figure out what to do with zmq.jar so that my "import zmq" stops failling when looking for libzmq.so.3. I know you talk about that above, but I'll give it a cahnce further with the instructs on http://www.zeromq.org/build:android.

    ReplyDelete