Building an Android* command-line application using the NDK build tools

Similar documents
How to set up FMOD*, Cocos2D-x*, and OpenAL* Libraries for Android* on Intel Architecture

Intel Unite Plugin Guide for VDO360 Clearwater

Clear CMOS after Hardware Configuration Changes

Intel Software Guard Extensions Platform Software for Windows* OS Release Notes

Intel Software Guard Extensions SDK for Linux* OS. Installation Guide

Intel Unite. Intel Unite Firewall Help Guide

Intel Integrated Native Developer Experience 2015 Build Edition for OS X* Installation Guide and Release Notes

Intel Integrated Native Developer Experience 2015 (OS X* host)

Intel Unite Solution Version 4.0

Intel Unite Solution Intel Unite Plugin for WebEx*

Customizing an Android* OS with Intel Build Tool Suite for Android* v1.1 Process Guide

Intel Stress Bitstreams and Encoder (Intel SBE) 2017 AVS2 Release Notes (Version 2.3)

Intel Parallel Studio XE 2015 Composer Edition for Linux* Installation Guide and Release Notes

Intel Stereo 3D SDK Developer s Guide. Alpha Release

Intel System Debugger 2018 for System Trace Linux* host

Intel Unite Solution. Linux* Release Notes Software version 3.2

How to Create a.cibd File from Mentor Xpedition for HLDRC

Android on Everything! Smooth Development of Cross-platform Native Android Games

LED Manager for Intel NUC

Intel Media Server Studio 2018 R1 Essentials Edition for Linux* Release Notes

Intel Manycore Platform Software Stack (Intel MPSS)

INTEL PERCEPTUAL COMPUTING SDK. How To Use the Privacy Notification Tool

Intel Unite Solution. Plugin Guide for Protected Guest Access

Intel Parallel Studio XE 2016

Intel Parallel Studio XE 2019 Update 1

NVIDIA DEBUG MANAGER FOR ANDROID NDK - VERSION 8.0.1

Installation Guide and Release Notes

Intel Integrated Native Developer Experience 2015 Build Edition for OS X* Installation Guide and Release Notes

Intel Desktop Board DZ68DB

Software Evaluation Guide for WinZip 15.5*

IBM Datacap Mobile SDK Developer s Guide

Analyze and Optimize Windows* Game Applications Using Intel INDE Graphics Performance Analyzers (GPA)

Intel Unite. Enterprise Test Environment Setup Guide

Tutorial: Analyzing MPI Applications. Intel Trace Analyzer and Collector Intel VTune Amplifier XE

NVDIMM DSM Interface Example

Intel Cache Acceleration Software for Windows* Workstation

Intel Parallel Studio XE 2018

Introduction to Intel Boot Loader Development Kit (Intel BLDK) Intel SSG/SSD/UEFI

Software Evaluation Guide for WinZip* esources-performance-documents.html

Android: Call C Functions with the Native Development Kit (NDK)

Intel Manageability Commander User Guide

Intel Unite Solution. Plugin Guide for Protected Guest Access

Intel Parallel Studio XE 2011 SP1 for Linux* Installation Guide and Release Notes

Intel Server Board SE7505VB2 SMBIOS Update Utility User s Manual

Evolving Small Cells. Udayan Mukherjee Senior Principal Engineer and Director (Wireless Infrastructure)

How to Create a.cibd/.cce File from Mentor Xpedition for HLDRC

Movidius Neural Compute Stick

Intel Parallel Studio XE 2011 for Linux* Installation Guide and Release Notes

Sample for OpenCL* and DirectX* Video Acceleration Surface Sharing

Modernizing Meetings: Delivering Intel Unite App Authentication with RFID

Intel Desktop Board D945GCLF2

Android on Intel Course Intel Development Tools

Using the Intel VTune Amplifier 2013 on Embedded Platforms

Reference Boot Loader from Intel

Intel Compute Card Slot Design Overview

Software Evaluation Guide for Photodex* ProShow Gold* 3.2

Optimization of Lustre* performance using a mix of fabric cards

Intel Unite Solution Version 4.0

Intel Galileo Firmware Updater Tool

Intel & Lustre: LUG Micah Bhakti

Intel Xeon W-3175X Processor Thermal Design Power (TDP) and Power Rail DC Specifications

Intel Unite Solution Intel Unite Plugin for Ultrasonic Join

Intel System Event Log (SEL) Viewer Utility. User Guide SELViewer Version 10.0 /11.0 February 2012 Document number: G

Intel Analysis of Speculative Execution Side Channels

Intel Server Board S2600CW2S

Intel Setup and Configuration Service. (Lightweight)

Intel RealSense Depth Module D400 Series Software Calibration Tool

AT&T Entertainment Experience Suite Video Optimizer 1.2

Intel Parallel Studio XE 2011 for Windows* Installation Guide and Release Notes

12th ANNUAL WORKSHOP 2016 NVME OVER FABRICS. Presented by Phil Cayton Intel Corporation. April 6th, 2016

Intel Unite Solution Version 4.0

Intel System Event Log Viewer Utility

Device Firmware Update (DFU) for Windows

NVIDIA CUDA C GETTING STARTED GUIDE FOR MAC OS X

Intel QuickAssist for Windows*

Intel System Information Retrieval Utility

Installation Guide and Release Notes

Intel Atom Processor E3800 Product Family Development Kit Based on Intel Intelligent System Extended (ISX) Form Factor Reference Design

OpenCL* and Microsoft DirectX* Video Acceleration Surface Sharing

Intel 945(GM/GME)/915(GM/GME)/ 855(GM/GME)/852(GM/GME) Chipsets VGA Port Always Enabled Hardware Workaround

Intel Visual Compute Accelerator Product Family

Intel Xeon Phi Coprocessor. Technical Resources. Intel Xeon Phi Coprocessor Workshop Pawsey Centre & CSIRO, Aug Intel Xeon Phi Coprocessor

Intel Software Guard Extensions (SGX) SW Development Guidance for Potential Bounds Check Bypass (CVE ) Side Channel Exploits.

Running Docker* Containers on Intel Xeon Phi Processors

Tegra 250 Development Kit Android Setup Experience

Intel Security Dev API 1.0 Production Release

PetaLinux SDK User Guide. Eclipse Plugin Guide

Getting Started. NVIDIA CUDA Development Tools 2.2 Installation and Verification on Mac OS X. May 2009 DU _v01

HAProxy* with Intel QuickAssist Technology

Zynq-7000 Platform Software Development Using the ARM DS-5 Toolchain Author: Simon George and Prushothaman Palanichamy

Intel 848P Chipset. Specification Update. Intel 82848P Memory Controller Hub (MCH) August 2003

Intel Education Theft Deterrent Release Note WW16'14. August 2014

The following modifications have been made to this version of the DSM specification:

An introduction to today s Modular Operating System

Tutorial: Finding Hotspots on an Android* Platform

Software Evaluation Guide Adobe Premiere Pro CS3 SEG

Intel vpro Technology Virtual Seminar 2010

Intel Desktop Board DH61SA

Intel Desktop Board D946GZAB

NVIDIA CUDA GETTING STARTED GUIDE FOR MAC OS X

Transcription:

Building an Android* command-line application using the NDK build tools Introduction Libraries and test apps are often written in C/C++ for testing hardware and software features on Windows*. When these same features are added to an Android* platform, rewriting these libraries and tests in Java* is a large task. It would be preferable to simply port the existing C/C++ code, but many believe that these libraries and tests need to be accessed as Java-based Android applications. But that s not always the case. If the code to be ported is written in ANSI C/C++ and doesn t have any OS-specific dependencies, it can be rebuilt using the Android NDK build tools and run from the command line in a shell in much the same way you can run command-line apps from the command prompt in Windows. This article shows how to write a simple Hello World application and run it on an Android device using a remote shell. Setting up your development environment Download and install the Android NDK Go to https://developer.android.com/ndk/downloads/index.html and download the appropriate NDK for your OS. Follow the instructions to extract and install the NDK on your computer. Set up your build environment Modify your path environment variable to indicate the location of the NDK directory. This allows you to run the NDK build tools from any other location on your computer without having to specify the tool s entire path. On Linux*, you can modify the variable for your local shell with the following command: export PATH=$PATH:/your/new/path/here If you d like the change to be permanent and present in each shell upon opening, add the following line to your ~/.profile or /etc/profile: PATH=$PATH:/your/new/path/here

Figure 1. Modify the path environment variable. On Windows, you can modify your environment variables by opening the Control Panel > System and Security > System > Advanced system settings > Environment Variables. Find your path variable in the System variables list, and then click Edit. Add a semicolon to the end of the last path, and then add your NDK path to the end. Click OK on each dialog. Figure 2. Modify the environment variables.

Writing the code and build scripts Creating your makefiles To build for Android, you need to have at least two makefiles: Android.mk and Application.mk. Android.mk is similar to the makefiles you might be familiar with for building on Linux from the command line. In this file you can define the source files to build, the header include directories, compiler flag options, libraries and their locations, the module name, and much more. Application.mk is for specifying Android-specific properties, such as the target Android platform, SDK version, and platform architecture. Figure 3. The Android.mk makefile. In Android.mk (Figure 3), you can see that a LOCAL_PATH directory is specified. This is initialized to the current directory so that you can use relative paths to other files and directories in the build environment. The line that includes CLEAR_VARS clears existing local variables that might have been set from previous builds or more complex builds that have multiple makefiles. The LOCAL_MODULE variable specifies the output name of the binary you re creating. The LOCAL_C_INCLUDES variable specifies the directories you want the preprocessor to search for additional include files. The LOCAL_SRC_FILES variable specifies the specific source files you d like to be built for this application/library. Place your.c or.cpp files here.

The final line is the key portion to indicate the building of an executable instead of a library. Most native code is built into libraries for Android applications, but changing the value to $(BUILD_EXECUTABLE) in Android.mk will result in an executable. Figure 4. The Application.mk makefile. In Application.mk (Figure 4), the first line indicates a build for x86 versus ARM. This tells the NDK to use the correct tool-chain for x86 target architecture. The second line specifies the platform to build for. In this case, it is version 21, which is for Android 5.0 also known as Lollipop*. The third line indicates the use of the static version of the standard library runtime. The final line indicates the name of the main makefile for this application. Writing your application A command-line application in Android is written in the same way, regardless of the platform. Figure 5 shows an example of a simple Hello World application. The cout function is used to print to the screen, and myprint() is defined in another file.

Figure 5. An example of a simple Hello World application. Figure 6 shows the layout of the folder structure for the project and source files. Figure 6. The layout of the folder structure for the project and source files.

Building your application To have the ndk-build script build an application, first create a project folder. In the folder, create a folder named jni. In the jni folder, place the Android.mk file, Application.mk file, and source files. Then navigate to the project folder in a terminal and execute ndk-build. ndk-build is a script that resides in the root folder of your NDK installation directory. The ndk-build script will parse the project directory and its subfolders and build the application. Since this example is about building a command-line application, the structure of having it built under a jni folder doesn t make much sense since there is no Java code or any code to interface with Java. However, removing the jni folder results in two additional steps that must be taken in the build process. The first step is to specify the NDK project path. Here, it is set to dot (.) for the current working directory. export NDK_PROJECT_PATH=. Then navigate to the project directory and use the ndk-build script. The second step is to specify where the Application.mk file is. Place it in the project directory, so the build command looks like this: ndk-build NDK_APPLICATION_MK=./Application.mk This prints your compile steps, executable creation, and where it installs your app. In this case, it creates a libs directory under your project directory. In the libs directory, it will create an x86 directory and place your executable in there. Figure 7. The build command. Below is the project tree with the source code and output from the build.

Figure 8. The project tree with the source code and output from the build. Deploying the application Installing your application In order to install your application, you re going to need a host machine and an adb (Android debug bridge) connection to the Android device from it. You ll need to use adb to connect to the device. The adb application comes with the Android SDK and can be downloaded as part of the platform tools bundle. Move your application to your host machine (main.out, in this instance). Using adb in a command prompt, you can push the file to your Android device. adb push /path/to/your/file /data/path/to/desired/folder/ Figure 9. Using adb in a command prompt. Now the main.out executable is on your Android device in the folder you specified.

Running your application To run your application, first you need to open a shell to your device. You do this with the adb shell command. Now you have a Unix*-like shell open. Change to the directory where you stored your sample. You can t yet execute it though. On Unix systems, a file needs to be marked as executable for you to be able to run it. You can do this with the chmod command. Now you re ready to run your command line app. Execute it by typing./<filename> or in this instance:./main.out. Figure 10. Running the application. Congratulations! You can now build and run a command-line application on an Android device. Author bio and photo Gideon Eaton is a member of the Intel Software and Services Group and works with independent software vendors to help them optimize their software for Intel Atom processors. In the past he worked on a team that wrote Linux* graphics drivers for platforms running the Android OS. Notices No license (express or implied, by estoppel or otherwise) to any intellectual property rights is granted by this document.

Intel disclaims all express and implied warranties, including without limitation, the implied warranties of merchantability, fitness for a particular purpose, and non-infringement, as well as any warranty arising from course of performance, course of dealing, or usage in trade. This document contains information on products, services and/or processes in development. All information provided here is subject to change without notice. Contact your Intel representative to obtain the latest forecast, schedule, specifications and roadmaps. The products and services described may contain defects or errors known as errata which may cause deviations from published specifications. Current characterized errata are available on request. Copies of documents which have an order number and are referenced in this document may be obtained by calling 1-800-548-4725 or by visiting www.intel.com/design/literature.htm. Intel, the Intel logo, and Intel Atom are trademarks of Intel Corporation in the U.S. and/or other countries. This sample source code is released under the Intel Sample Source Code License Agreement. *Other names and brands may be claimed as the property of others 2015 Intel Corporation.