Sunday, August 14, 2011

How to build Android ndk sample using ant

This is a walkthrough of how to build the hello-jni sample from Android ndk after you have installed sdk and ndk. You want to use ant instead of Eclipse for building sample project.

Suppose your install paths of sdk and ndk are
~/android-sdk-mac_x86 and ~/android-ndk-r5c

Setup PATH
export PATH=${PATH}:~/android-sdk-mac_x86/tools:~/android-ndk-r5c

ndk-build
cd ~/android-ndk-r5c/samples/hello-jni
ndk-build


list target (assume sdk installed)
android list target

update project with target 1
android update project -t 1 -p .

list avd (suppose AVD4G created during sdk installation)
android list avd

start emulator
emulator -avd AVD4G -scale 0.5 &

debug build
ant debug

install to emulator
ant install

use ant uninstall to remove

If you want to code-sign the binary, first create the build.properties and self-signing certificate and compile release

create build.properties

# location of the keystore. This is used by ant release
key.store= /my-path-to-mykey/my.keystore
key.alias=mykeystore


create self-signing certificate and compile release
keytool -genkey -v -keystore /my-path-to-mykey/my.keystore -alias mykeystore -keyalg RSA -keysize 2048 -validity 10000
ant release





This is how to compile C++ program with stlport in ndk


cd ~/android-ndk-r5c/samples/test-libstdc++


modify jni/test-libstl.cpp to

#include <iostream>
using namespace std;

int main() {
  cout << "hello, world\n";
  return 0;
}


create jni/Application.mk with

APP_STL := stlport_static


modify jni/Android.mk with

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := test-libstl
LOCAL_SRC_FILES := test-libstl.cpp
LOCAL_LDLIBS := -llog

include $(BUILD_EXECUTABLE)

include $(CLEAR_VARS)
LOCAL_EXECUTABLE := test-libstl

run:
~/android-sdk-mac_x86/platform-tools/adb push libs/armeabi/$(LOCAL_MODULE) /data/local/bin/$(LOCAL_MODULE)
~/android-sdk-mac_x86/platform-tools/adb shell chmod 755 /data/local/bin/$(LOCAL_MODULE)
~/android-sdk-mac_x86/platform-tools/adb shell /data/local/bin/$(LOCAL_MODULE)


run with emulator

ndk-build clean && ndk-build
ndk-build run


For Windows, add this PATH in environment (path should not have spaces)
C:\Android\android-sdk\tools;C:\Android\apache-ant-1.8.2\bin;C:\Android\android-ndk-r6;

JAVA_HOME=C:\Program Files\Java\jdk1.6.0_29

and you need cygwin to start ndk-build
.
.
.

0 comments: