Click here to Skip to main content
15,881,424 members
Articles / Mobile Apps / Android
Article

Compiling ZeroMQ library for Android Applications on Intel® x86 Platforms

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
6 Jul 2015CPOL4 min read 10.4K  
Building the x86 version of ZeroMQ library step by step

This article is for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers

Intel® Developer Zone offers tools and how-to information for cross-platform app development, platform and technology information, code samples, and peer expertise to help developers innovate and succeed. Join our communities for Android, Internet of Things, Intel® RealSense™ Technology and Windows to download tools, access dev kits, share ideas with like-minded developers, and participate in hackathon’s, contests, roadshows, and local events.

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 in https://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.
    Java
    public void startZMQserver() throws Exception {
     ZMQ.Context context = ZMQ.context(1);
    
     // Socket to talk to clients
     responder = context.socket(ZMQ.REP);
     responder.bind("tcp://*:" + SERVER_PORT);
    
     printLog("Server", "Started");
     while (!Thread.currentThread().isInterrupted()) {
      // Wait for next request from the client
      byte[] request = responder.recv(0);
      printLog("Server", "Received:" + new String(request));
    
      // Do some 'work'
      Thread.sleep(1000);
    
      // Send reply back to client
      String reply = "World";
      responder.send(reply.getBytes(), 0);
      printLog("Server", "Response send.");
     }
     responder.close();
     context.term();
    }
    
  5. Implement the client side function.
    Java
    public void startZMQclient() throws Exception {
     ZMQ.Context context = ZMQ.context(1);
    
     // Socket to talk to server
     printLog("Client", "Connecting to ZMQ server…");
    
     requester = context.socket(ZMQ.REQ);
     requester.connect("tcp://" + SERVER_IP + ":" + SERVER_PORT);
    
     printLog("Client", "Connected to server.");
     for (int requestNbr = 0; requestNbr != 10; requestNbr++) {
      String request = "Hello";
      printLog("Client", "Sending Hello " + requestNbr);
      requester.send(request.getBytes(), 0);
    
      byte[] reply = requester.recv(0);
      printLog("Client", "Received " + new String(reply) + " "
        + requestNbr);
     }
     requester.close();
     context.term();
    }
    
  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.
    Image 1
    Figure 1. Demo JZMQ start screen with the log file example.

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.

References

  1. ØMQ - http://en.wikipedia.org/wiki/%C3%98MQ
  2. ZeroMQ build for Android - http://zeromq.org/build:android
  3. ØMQ - The Guide - http://zguide.zeromq.org/page:all

About the Author

Bin Zhu is an application engineer in the Intel® Atom™ Processor Mobile Enabling Team, Developer Relations Division, Software and Solutions Group (SSG). He is responsible for Android app enabling on Intel Atom processors and focuses on multimedia technologies for the Android x86 platforms.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Intel is inside more and more Android devices, and we have tools and resources to make your app development faster and easier.


Comments and Discussions

 
-- There are no messages in this forum --