Compiling ZeroMQ library for Android Applications on Intel x86 Platforms

Compiling ZeroMQ library for Android Applications on Intel x86 Platforms

Introduction

ZeroMQ is an open source library widely used by programmers around the world. Its Wikipedia definition is as follows: “ZeroMQ (also spelled ØMQ, 0MQ or ZMQ) is a high-performance asynchronous messaging library aimed at use in scalable distributed or concurrent applications. It provides a message queue, but unlike message-oriented middleware, a ZeroMQ system can run without a dedicated message broker. The library is designed to have a familiar socket-style API.”

On ZeroMQ’s website, the community has provided build instructions for the Android platform (http://zeromq.org/build:android). However, those instructions are focused on ARM* architecture, and developers will meet build failure when they try to build the x86 version of ZeroMQ library by following the instruction step by step. That is why this article has been written.

Build Environment Preparation

The first goal in building ZeroMQ is to create a separate standalone tool chain for x86 that relies on Android NDK.
1. Download and install the latest Android NDK from:
http://developer.android.com/tools/sdk/ndk/index.html. In this article we use android-ndk-r10d-linux-x86_64.
2. Fix a known issue in make-standalone-toolchain.sh, by applying the patch mentioned inhttps://code.google.com/p/android/issues/detail?id=74145. Without this patch the build will fail in the next step.
3. Generate a standalone tool chain for the Android x86 platform.
./home/xxx/android-ndk-r10c/build/tools/make-standalone-toolchain.sh –arch=x86 –toolchain=x86-4.9 –install-dir=/home/xxx/android-standalone-toolchain-x86

You should follow the next two suggestions to avoid unexpected build problems:
 
1. Generate a single x86 tool chain in a separate directory. Don’t mix the x86 tool chain and the ARM tool chains in one directory.
2. Install the standalone tool chain in your /home directory, to help avoid root/no-root authority issues.
4. Configure the environment variables.
export PATH=/home/xxx/android-standalone-toolchain-x86/bin:$PATH export OUTPUT_DIR=/home/xxx/tmp/zeromq-android

Build ZeroMQ for Android x86

The following steps will show you how to build ZeroMQ along with Jzmq and its adjoining JAR, to be directly usable in Android and loaded into an APK file.

1. Download the source code and build ZeroMQ 3.x.
mkdir /home/xxx/tmp cd /home/xxx/tmp git clone https://github.com/zeromq/zeromq3-x.git cd zeromq3-x/ ./autogen.sh ./configure –host=i686-linux-android –prefix=$OUTPUT_DIR LDFLAGS="-L$OUTPUT_DIR/lib" CPPFLAGS="-fPIC -I$OUTPUT_DIR/include" –enable-static –disable-shared LIBS="-lgcc" make make install
2. Download the source code and build jzmq.
cd /home/xxx/tmp git clone https://github.com/zeromq/jzmq.git cd jzmq/ ./autogen.sh ./configure –host=i686-linux-android –prefix=$OUTPUT_DIR LDFLAGS="-L$OUTPUT_DIR/lib" CPPFLAGS="-fPIC -I$OUTPUT_DIR/include" –disable-version –with-zeromq=$OUTPUT_DIR LIBS="-lpthread -lrt" make make install 
If you see a failure during the execution of “./configure”, remove LIBS="-lpthread –lrt" and try again.
3. Copy libjzmq.so and zmq.jar files to your desired directory, and now ZeroMQ has been built successfully!
cp /home/xxx/tmp/zeromq-android/lib/libjzmq.so /home/xx/tmp/ cp /home/xxx/tmp/zeromq-android/share/java/zmq.jar /home/xx/tmp/

An example using ZeroMQ in Android x86

You can now start using ZeroMQ in your Android applications. Here is a simple step-by-step example that describes how to use ZeroMQ in implementing message interaction between the ZeroMQ client and the ZeroMQ server. Some of this sample code is from the ZeroMQ official guide and wrapped into the Android project.

1. Copy libjzmq.so into the directory “DemoJZMQ/jni/”, and copy zmq.jar into the directory “DemoJZMQ/libs/”.
2. Add the script below to Android.mk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := libjzmq LOCAL_SRC_FILES := libjzmq.so include $(PREBUILT_SHARED_LIBRARY)
3. Add the script below to Application.mk.
APP_ABI := x86
4. Implement the server side function.

01  public void startZMQserver() throws Exception {

02 ZMQ.Context context = ZMQ.context(1);

03
 

04 // Socket to talk to clients

05 responder = context.socket(ZMQ.REP);

06 responder.bind("tcp://*:" + SERVER_PORT);

07
 

08 printLog("Server", "Started");

09 while (!Thread.currentThread().isInterrupted()) {

10  // Wait for next request from the client

11 byte[] request = responder.recv(0);

12 printLog("Server", "Received:" + new String(request));

13
 

14 // Do some 'work'

15 Thread.sleep(1000);

16
 

17 // Send reply back to client

18 String reply = "World";

19 responder.send(reply.getBytes(), 0);

20 printLog("Server", "Response send.");

21  }

22 responder.close();

23 context.term();

24
 }

5. Implement the client side function.

01  public void startZMQclient() throws Exception {

02 ZMQ.Context context = ZMQ.context(1);

03
 

04 // Socket to talk to server

05 printLog("Client", "Connecting to ZMQ server…");

06
 

07  requester = context.socket(ZMQ.REQ);

08 requester.connect("tcp://" + SERVER_IP + ":" + SERVER_PORT);

09
 

10 printLog("Client", "Connected to server.");

11 for (int requestNbr = 0; requestNbr != 10; requestNbr++) {

12 String request = "Hello";

13 printLog("Client", "Sending Hello " + requestNbr);

14 requester.send(request.getBytes(), 0);

15
 

16 byte[] reply = requester.recv(0);

17 printLog("Client", "Received " + new String(reply) + " "

18 + requestNbr);

19  }

20
  requester.close();

21  context.term();

22  }

6. Run the example app on a device, click the “start server” button first, and then click “start client”. From “adb logcat” you can see the execution results (Figure 1) that show when the client sends a “Hello” to the server, the server responds “World” back to the client 10 times.

Summary

This article describes how to compile the ZeroMQ library for the Android x86 platform step by step. Also provided is a simple example using message transfer to show how to use ZeroMQ in Android applications. As mentioned above, the key point in compiling ZeroMQ for x86 successfully is to generate correct standalone tool chains for x86 based on the Android NDK. This method will be useful for not only ZeroMQ, but also for other 3rd party library builds.

For more such Android resources and tools from Intel, please visit the Intel® Developer Zone

Source: https://software.intel.com/en-us/articles/compiling-zeromq-library-for-android-applications-on-intel-x86-platforms

Digit.in
Logo
Digit.in
Logo