More Related Content Similar to Get Started with Android NDK - Build Native C/C++ Code Similar to Get Started with Android NDK - Build Native C/C++ Code (20) Get Started with Android NDK - Build Native C/C++ Code5. C/C++ support
C standard library
Data types
Character classification
Strings
Mathematics
File input/output
Date/time
Localization
Memory allocation
Process control
Signals
Alternative tokens
Miscellaneous headers
<assert.h>
<errno.h>
<setjmp.h>
<stdarg.h>
C++ Standard Library
Input/output
Strings
Standard Template Library
(array, list, map, etc)
6. C++ support
C++ 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
7. File types
● .o - object
● .a - static library(archive)
● .so - dynamically Linked "Shared Object" Libraries
8. NDK Build
Parameters
● clean
● NDK_DEBUG=1
● NDK_LOG=1
● NDK_HOST_32BIT=1
● NDK_APPLICATION_MK
Execute $GNUMAKE -f $NDK/build/core/build-local.mk [parameters]
9. 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
10. 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
12. Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := NDKBegining
LOCAL_SRC_FILES := ndkBegining.c
include $(BUILD_SHARED_LIBRARY)
13. Android.mk
include $(CLEAR_VARS)
LOCAL_MODULE:= swresample
LOCAL_SRC_FILES:= lib/libswresample-0.so
LOCAL_EXPORT_C_INCLUDES := lib/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := tutorial01
LOCAL_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)
14. Availabe API
● log
● z (zlib)
● dl
● GLESv1_CM
● GLESv2
● GLESv3
● jnigraphics
● EGL
● OpenSLES
● android
● OpenMAXAL
● c(default)
● m (default)
18. Local and Global Reference
Local references
Local references are valid only while the method that you invoke runs. Automatic freeing
of the local references occurs after the native method returns.
Global references
Global references remain valid until you explicitly delete them. You can create global
references from local references by using the JNI service NewGlobalRef.
JNI Services
20. Code Samples
#java code
package 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 code
extern "C" {
JNIEXPORT jdouble JNICALL com_test_sample1_sum
(JNIEnv *, jclass, jdouble, jdouble);
}
21. Code Samples
#java code
class ResultHandler {
public void onReturnedString(String str)
{
/* Do something with the string */
}
}
#c code
void 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);
22. Code Samples
jclass clsIfc = jniEnv->FindClass("example/StuffInter");
jmethodID getFactor = jniEnv->GetStaticMethodID(clsIfc, "getFactor","()I");
// Perform the callback.
int retval = jniEnv->CallStaticIntMethod(clsIfc,getFactor);