Introduction to Native Android Development with NDK

Size: px
Start display at page:

Download "Introduction to Native Android Development with NDK"

Transcription

1 Introduction to Native Android Development with NDK

2 Outline Motivation: case study of a real project Android Architecture Simplified Tool chain Diagram Adding 3 rd party modules Adding pdf and encrypted DB Files Layout Code Layout Passing parameters Java - Native Debugging with logs Tools Overview Exercises Q&A

3 Motivation: case study of a real project

4 Motivation: case study of a real project

5 Motivation: case study of a real project

6 Motivation: case study of a real project

7 Motivation: case study of a real project

8 Motivation: case study of a real project How can we encrypt the DB? How can we generate pdf reports?

9 Embedded Android By: Karim Yaghmour Published by O Reilly Media, Inc. Copyright 2013 Karim Yaghmour All rights reserved. Used with permission Android Architecture

10 Android Architecture Forked Linux Distribution Example: Logging No Linux System Logs Android Own Logging driver Maintains own circular buffer No socket access per message, thus no context switch per message All components rely on liblog Embedded Android By: Karim Yaghmour Published by O Reilly Media, Inc. Copyright 2013 Karim Yaghmour All rights reserved. Used with permission

11 Simplified* Tool chain Diagram Native Source files (*.c, *.cpp) Java source files (*.java) Crosscompiler Java Compiler Object files (*.o) Static libraries *.a 3 rd party libraries and.class files Class files (*.class) Linker dex Dynamic Library (*.so) Apk builder.dex files Here we omit aidl, graphics, xml etc Apk file

12 Adding 3 rd party modules Native Source files (*.c, *.cpp) 3 rd Party Native Source files (*.c, *.cpp) Java source files (*.java) Crosscompiler 3 rd Party Static Libraries (*.a) Java Compiler Object files (*.o) Static libraries *.a 3 rd party libraries and.class files Class files (*.class) Linker 3 rd Party Dynamic Library (*.so) dex Dynamic Library (*.so) Apk builder.dex files Apk file

13 Adding pdf and encrypted DB Native Source files (*.c, *.cpp) libharu (*.cpp, *.h) Java source files (*.java) Crosscompiler Java Compiler Object files (*.o) Static libraries *.a 3 rd party libraries and.class files Class files (*.class) Linker libsqlcipher_android*.so dex Dynamic Library (*.so) Apk builder.dex files Apk file

14 Project layout Files Layout Contents of apk Makefile C/C++ source dex file (.class packaged in Android dex format) Dynamic Library Dynamic Library Object File

15 Code Layout LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := HelloJNI LOCAL_SRC_FILES := HelloJNI.cpp include $(BUILD_SHARED_LIBRARY) extern "C" { JNIEXPORT jstring JNICALL Java_com_example_hellojni_MainActivity_strcat(JNIEnv * env, jobject obj, jstring jstr1, jstring jstr2) { char buf[128]; return env->newstringutf(buf); } Interface to the JVM public native String strcat(string str1, String str2); static { System.loadLibrary("HelloJNI"); }

16 Passing parameters Java - Native JNIEXPORT jstring JNICALL Java_com_example_hellojni_MainActivity_strcat(JNIEnv * env, jobject obj, jstring jstr1, jstring jstr2) { char buf[128]; C method name reflects full Java name Interface to the VM const char *str1 = env->getstringutfchars(jstr1, 0); const char *str2 = env->getstringutfchars(jstr2, 0); strcpy(buf, str1); strcat(buf, str2); Parameters from Java are accessed through the env variable } //can use even assembly here env->releasestringutfchars(jstr1, str1); env->releasestringutfchars(jstr2, str2); return env->newstringutf(buf); Data from Java is accessed through env variable No automatic garbage collection, don t forget to deallocate memory

17 Debugging with logs Include the logs header #include <android/log.h> #define APPNAME "HelloJNI" JNIEXPORT jstring JNICALL Java_com_example_hellojni_MainActivity_strcat( JNIEnv * env, jobject obj, jstring jstr1, jstring jstr2) { const char *str = env->getstringutfchars(jstr1, 0); android_log_print(android_log_verbose, APPNAME, "str1=%s", str1); } Set verbosity } Also, add the following line to Android.mk : LOCAL_LDLIBS := -llog

18 Tools Overview NDK ADT CDT Sequoyah DS-5 Native Development Kit Android Development Tools C/C++ Development Tool Eclipse mobile development tool Development suite by arm Cross-compiler for Android Linux Android dev Eclipse plugin C/C++ Eclipse plugin, generic C/C++ compiler, debugger Debugger Debugger

19 Exercises Exercise 1: HelloJNI Objectives: Add missing code Execute the app Inspect logs Download from Exercise 2: Tower of Hanoi Objectives: Add missing code Execute the app See difference in performance between Java and C implementation of Hanoi Tower Algorithm Download from

20 Q&A