Android ndk

Post on 26-Jan-2015

195 views 3 download

description

 

Transcript of Android ndk

Android NDK

Kuban Dzhakipov@Sibers

2014

Plan

● Introduction● Build tools● Java Native Interface

Introduction

● What is NDK?● Usage● Brief History● Architecture (ARM, x86, MIPS)

NDK ContentsFolders

● build

● docs

● platforms

● prebuilt

● samples

● sources

● tests

● toolchains

Files

● ndk-build

● ndk-stack

● ndk-gdb

etc….

C/C++ supportC standard libraryData typesCharacter classificationStringsMathematicsFile input/outputDate/timeLocalizationMemory allocationProcess controlSignalsAlternative tokens Miscellaneous headers<assert.h><errno.h><setjmp.h><stdarg.h>

C++ Standard LibraryInput/outputStringsStandard Template Library(array, list, map, etc)

C++ supportC++ Exceptions C++ RTTI Standard

Library

system (/system/lib/libstdc++)

no no no

gabi++ (static, shared) yes yes no

stlport (static, shared) yes yes yes

gnustl (static, shared) yes yes yes

libc++ (static, shared) yes yes yes

File types

● .o - object● .a - static library(archive)● .so - dynamically Linked "Shared Object" Libraries

NDK BuildParameters

● clean

● NDK_DEBUG=1

● NDK_LOG=1

● NDK_HOST_32BIT=1

● NDK_APPLICATION_MK

Execute $GNUMAKE -f $NDK/build/core/build-local.mk [parameters]

NDK Stack I/DEBUG ( 31): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

I/DEBUG ( 31): Build fingerprint: 'generic/google_sdk/generic/:2.2/FRF91/43546:eng/test-keys'

I/DEBUG ( 31): pid: 351, tid: 351 %gt;%gt;%gt; /data/local/ndk-tests/crasher <<<

I/DEBUG ( 31): signal 11 (SIGSEGV), fault addr 0d9f00d8

I/DEBUG ( 31): r0 0000af88 r1 0000a008 r2 baadf00d r3 0d9f00d8

I/DEBUG ( 31): r4 00000004 r5 0000a008 r6 0000af88 r7 00013c44

I/DEBUG ( 31): r8 00000000 r9 00000000 10 00000000 fp 00000000

I/DEBUG ( 31): ip 0000959c sp be956cc8 lr 00008403 pc 0000841e cpsr 60000030

I/DEBUG ( 31): #00 pc 0000841e /data/local/ndk-tests/crasher

I/DEBUG ( 31): #01 pc 000083fe /data/local/ndk-tests/crasher

I/DEBUG ( 31): #02 pc 000083f6 /data/local/ndk-tests/crasher

I/DEBUG ( 31): #03 pc 000191ac /system/lib/libc.so

I/DEBUG ( 31): #04 pc 000083ea /data/local/ndk-tests/crasher

I/DEBUG ( 31): #05 pc 00008458 /data/local/ndk-tests/crasher

I/DEBUG ( 31): #06 pc 0000d362 /system/lib/libc.so

NDK Stack********** Crash dump: **********

Build fingerprint: 'generic/google_sdk/generic/:2.2/FRF91/43546:eng/test-keys'

pid: 351, tid: 351 >>> /data/local/ndk-tests/crasher <<<

signal 11 (SIGSEGV), fault addr 0d9f00d8

Stack frame #00 pc 0000841e /data/local/ndk-tests/crasher : Routine zoo in

/tmp/foo/crasher/jni/zoo.c:13

Stack frame #01 pc 000083fe /data/local/ndk-tests/crasher : Routine bar in

/tmp/foo/crasher/jni/bar.c:5

Stack frame #02 pc 000083f6 /data/local/ndk-tests/crasher : Routine my_comparison in

/tmp/foo/crasher/jni/foo.c:9

Stack frame #03 pc 000191ac /system/lib/libc.so

Stack frame #04 pc 000083ea /data/local/ndk-tests/crasher : Routine foo in

/tmp/foo/crasher/jni/foo.c:14

Stack frame #05 pc 00008458 /data/local/ndk-tests/crasher : Routine main in

/tmp/foo/crasher/jni/main.c:19

Stack frame #06 pc 0000d362 /system/lib/libc.so

NDK GDB

Android.mk

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := NDKBeginingLOCAL_SRC_FILES := ndkBegining.cinclude $(BUILD_SHARED_LIBRARY)

Android.mkinclude $(CLEAR_VARS)LOCAL_MODULE:= swresampleLOCAL_SRC_FILES:= lib/libswresample-0.soLOCAL_EXPORT_C_INCLUDES := lib/includeinclude $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := tutorial01LOCAL_SRC_FILES := tutorial01.c ffmpeg/cmdutils.c \ ffmpeg/ffmpeg.c \ ffmpeg/ffmpeg_filter.c \ ffmpeg/ffmpeg_opt.c

LOCAL_C_INCLUDES := ffmpeg

LOCAL_LDLIBS := -llog -ljnigraphics -lz

LOCAL_SHARED_LIBRARIES := libswresample

include $(BUILD_SHARED_LIBRARY)

Availabe API● log● z (zlib) ● dl● GLESv1_CM● GLESv2● GLESv3● jnigraphics● EGL● OpenSLES● android● OpenMAXAL● c(default)● m (default)

Application.mk● APP_OPTIM

● APP_BUILD_SCRIPT

● APP_ABI

● APP_PLATFORM

● APP_STL

● NDK_TOOLCHAIN_VERSION

Java Native Interface

Primitive type (jni.h)

Local and Global ReferenceLocal referencesLocal references are valid only while the method that you invoke runs. Automatic freeing of the local references occurs after the native method returns.

Global referencesGlobal references remain valid until you explicitly delete them. You can create global references from local references by using the JNI service NewGlobalRef.

JNI Services

Threads

Code Samples

#java codepackage com.test.sample1

public class Test {static {

System.loadLibrary(“test”);}public static void main(String[] args){

System.out.print(sum(10, 10));}public static native double sum(double val1, double val2);

}

# c codeextern "C" {JNIEXPORT jdouble JNICALL com_test_sample1_sum (JNIEnv *, jclass, jdouble, jdouble);}

Code Samples#java code

class ResultHandler { public void onReturnedString(String str) { /* Do something with the string */ } }

#c codevoid Java_com_foo_bar_getMessagewithoutReturn(JNIEnv *env, jobject thiz, jobject handler);

jmethodID mid; jclass handlerClass = (*env)->FindClass(env, "com/foo/bar/ResultHandler"); if (handlerClass == NULL) { /* error handling */ } mid = (*env)->GetMethodID(env, handlerClass, "onReturnedString", "(Ljava/lang/String;)V"); if (mid == NULL) { /* error handling */ }(*env)->CallVoidMethod(env, handler, mid, resultString);

Code Samplesjclass clsIfc = jniEnv->FindClass("example/StuffInter");jmethodID getFactor = jniEnv->GetStaticMethodID(clsIfc, "getFactor","()I");

// Perform the callback.int retval = jniEnv->CallStaticIntMethod(clsIfc,getFactor);

Questions?

Thanks for your time!

Sources:d.android.comdocs.oracle.com