Android ndk

24
Android NDK Kuban Dzhakipov @Sibers 2014

description

 

Transcript of Android ndk

Page 1: Android ndk

Android NDK

Kuban Dzhakipov@Sibers

2014

Page 2: Android ndk

Plan

● Introduction● Build tools● Java Native Interface

Page 3: Android ndk

Introduction

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

Page 4: Android ndk

NDK ContentsFolders

● build

● docs

● platforms

● prebuilt

● samples

● sources

● tests

● toolchains

Files

● ndk-build

● ndk-stack

● ndk-gdb

etc….

Page 5: Android ndk

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)

Page 6: Android ndk

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

Page 7: Android ndk

File types

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

Page 8: Android ndk

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]

Page 9: Android ndk

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

Page 10: Android ndk

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

Page 11: Android ndk

NDK GDB

Page 12: Android ndk

Android.mk

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

Page 13: Android ndk

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)

Page 14: Android ndk

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

Page 15: Android ndk

Application.mk● APP_OPTIM

● APP_BUILD_SCRIPT

● APP_ABI

● APP_PLATFORM

● APP_STL

● NDK_TOOLCHAIN_VERSION

Page 16: Android ndk

Java Native Interface

Page 17: Android ndk

Primitive type (jni.h)

Page 18: Android ndk

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

Page 19: Android ndk

Threads

Page 20: Android ndk

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);}

Page 21: Android ndk

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);

Page 22: Android ndk

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

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

Page 23: Android ndk

Questions?

Page 24: Android ndk

Thanks for your time!

Sources:d.android.comdocs.oracle.com