http://software.intel.com/en-us/articles/using-the-android-x86-ndk-with-eclipse-and-porting-an-ndk-sample-app



Using the Android* x86 NDK with Eclipse* and Porting an NDK Sample App

Categories: 
Tags: 

Objective

The Android plugin for Eclipse (ADT) now supports NDK-based app development. It can auto generate project and build files, and code stubs, and integrate into the full Android app development (building native libs, copying the libs into appropriate JNI folder inside the project, packaging the app, and generating final APK with NDK code included). In this article we will discuss how to configure Eclipse to take advantage of this feature, and walk through porting a sample NDK app.

Configuring Eclipse ADT Plugin for Use with the NDK

Before we can use this new feature, we need to configure Eclipse ADT plugin to point to the location of the NDK installation path. First, make sure to update the ADT plugin in Eclipse to get the latest changes, and also download the latest NDK from the site below:

http://developer.android.com/tools/sdk/ndk/index.html

In Eclipse, open Window->Preferences->Android->NDK. Enter the NDK installation path (here we assume you downloaded the NDK to c:\sw\android-ndk on Windows*) as shown below and click Apply, then OK.


Figure 1: NDK configuration in Eclipse Android ADT plugin

We should now have the NDK-related features enabled and active in Eclipse ADT. Let’s open one of the NDK JNI samples and build it.

Open hello-jni Sample NDK App in Eclipse and Add NDK Build Support to the Project

To build the NDK JNI native sample in Eclipse, we need to let Eclipse ADT know that this sample uses native/NDK code.

Let’s first import the HelloJni sample into eclipse. In Eclipse, open File -> New -> Project -> Android -> Android Project from Existing Code. In the next screen, enter the path for hello-jni sample project (available in NDK installation folder), and choose copy to workspace checkbox, click Finish. Replace the root directory path with HelloJni path from your NDK installation folder.


Figure 2: Import the sample HelloJni Project into Eclipse

Since the x86 Android NDK supports Gingerbread* and higher Android platforms, let’s make sure we update the Min and Target SDK configuration in the app manifest. In Eclipse, Open AndroidManifest.xml under HelloJni Sample; click “Uses SDK” in the AndroidManifest editor and Verify the Min SDK is set to 10 (API 10).


Figure 3: Manifest file editor in Eclipse

We are now ready to add the NDK build support to this project in Eclipse. In Eclipse, right click hello-jni project (com.example.hellojni.HelloJni) -> Android Tools -> Add Native Support -> Click Finish.


Figure 4: Add NDK support to project in Eclipse

This updates the project configuration with NDK build-related settings. When we rebuild the project, it will first compile and build the NDK code, copy the built libs to appropriate project folders, and then continue with the regular APK build process.

Build and Run the App in Emulator

Let’s go ahead and build the app and run it in the Android Emulator. We assume you already have Gingerbread x86 AVD running on HAXM-based Android Emulator.

In Eclipse, right click hello-jni project (com.example.hellojni.HelloJni) -> Run As -> Android Application. Eclipse should automatically rebuild the project and try to deploy/run the APK on the emulator.

But when the emulator runs, we see the error message below because the default NDK project settings do not support x86.


Figure 5: Running the sample app in Emulator

In Eclipse, you can see the logcat showing the error “couldn’t load hello-jni”


Figure 6: Logcat shows failure to load the native lib

By default, the NDK build doesn’t automatically build for x86 ABI. We will need to either create a build file “Application.mk” to explicitly specify our build targets or pass in command line arguments to the NDK build command.

Port the App to Use x86 ABI and NDK Build Files

Let’s modify the project to support x86 ABI. In Eclipse, right click the “jni” folder in hello-jni project, then choose New -> File -> Enter “Application.mk” as the filename, then click Finish.


Figure 7: Adding new NDK build file “Application.mk” to sample project

Edit the file “Application.mk”, add the line “APP_ABI := all” as shown below, and save the file.


Figure 8: Edit the Application.mk file to target “all” architectures

Build the App, Verify it Builds x86 lib, and Test App in x86 Emulator

When we rebuild the app now, it will build for all the supported ABIs in NDK. Right click “hello-jni” project in Eclipse -> Run As -> Android Application. Eclipse will automatically invoke the NDK build and generate all native libs. You can verify it under the libs folder as shown below.


Figure 9: Eclipse project explorer showing the generated native libs

Eclipse will deploy the app in the emulator. This time we should see the app successfully running, as shown in this screenshot:


Figure 10: Sample app successfully running inside x86 Android Emulator

We hope this post helped you learn how to configure Eclipse ADT for use in NDK code development and how to use the NDK build file “Application.mk” to build for the required target ABIs.

Notices

INFORMATION IN THIS DOCUMENT IS PROVIDED IN CONNECTION WITH INTEL PRODUCTS. NO LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, TO ANY INTELLECTUAL PROPERTY RIGHTS IS GRANTED BY THIS DOCUMENT. EXCEPT AS PROVIDED IN INTEL'S TERMS AND CONDITIONS OF SALE FOR SUCH PRODUCTS, INTEL ASSUMES NO LIABILITY WHATSOEVER AND INTEL DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY, RELATING TO SALE AND/OR USE OF INTEL PRODUCTS INCLUDING LIABILITY OR WARRANTIES RELATING TO FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.

INFORMATION IN THIS DOCUMENT IS PROVIDED IN CONNECTION WITH INTEL PRODUCTS. NO LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, TO ANY INTELLECTUAL PROPERTY RIGHTS IS GRANTED BY THIS DOCUMENT. EXCEPT AS PROVIDED IN INTEL'S TERMS AND CONDITIONS OF SALE FOR SUCH PRODUCTS, INTEL ASSUMES NO LIABILITY WHATSOEVER AND INTEL DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY, RELATING TO SALE AND/OR USE OF INTEL PRODUCTS INCLUDING LIABILITY OR WARRANTIES RELATING TO FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.

Intel may make changes to specifications and product descriptions at any time, without notice. Designers must not rely on the absence or characteristics of any features or instructions marked "reserved" or "undefined." Intel reserves these for future definition and shall have no responsibility whatsoever for conflicts or incompatibilities arising from future changes to them. The information here is subject to change without notice. Do not finalize a design with this information.

The products described in this document may contain design defects or errors known as errata which may cause the product to deviate from published specifications. Current characterized errata are available on request.

Contact your local Intel sales office or your distributor to obtain the latest specifications and before placing your product order.

Copies of documents which have an order number and are referenced in this document, or other Intel literature, may be obtained by calling 1-800-548-4725, or go to: http://www.intel.com/design/literature.htm

Software and workloads used in performance tests may have been optimized for performance only on Intel microprocessors. Performance tests, such as SYSmark and MobileMark, are measured using specific computer systems, components, software, operations, and functions. Any change to any of those factors may cause the results to vary. You should consult other information and performance tests to assist you in fully evaluating your contemplated purchases, including the performance of that product when combined with other products.

Any software source code reprinted in this document is furnished under a software license and may only be used or copied in accordance with the terms of that license.

Intel and the Intel logo are trademarks of Intel Corporation in the US and/or other countries.

Copyright © 2012 Intel Corporation. All rights reserved.

*Other names and brands may be claimed as the property of others.

Optimization Notice