1.
Android NDK Intro
Tokyo Android Meetup 21st July 2016
2.
About The Presenter
Giles Payne
Android Developer - TenTen Corp
Developing for Android since Cupcake
BA Honours Mathematics and Computation, Oxford University
Country of Origin: Scotland
3.
What is the Android NDK?
A set of tools for building/debugging C/C++ code to run on Android
Invoked from Java via JNI(Java Native Interface)
Available for download at https://developer.android.com/ndk/index.html
4.
Android NDK - Why?
Performance
Order of magnitude speedup for performance critical code
Code reuse
Existing code C/C++ base can be ported to Android without total rewrite in Java
Large amount of libraries written in C/C++ can be reused (boost/cocos2d-x/OpenCV)
Cross platform development
iOS and other mobile OSs all support C/C++
5.
NDK How To
Declare method as native with no body
public native void myNativeMethod();
Use javah to generate native stub
javah -o "MyClass.h" -classpath "/home/giles/myjavalibs" com.example.MyClass
Add implementation to stub and build C/C++ lib
extern "C" void Java_com_example_MyClass_myNativeMethod(JNIEnv* env, jobject this) {
/* do stuff */
}
6.
What if I need to call back out to Java?
jclass myjclass = (*env)->GetObjectClass(env, myjobj);
jmethodID myMethodId = (*env)->GetMethodID(env, myjclass, "myMethod", "()Ljava/lang/String;");
jstring jstrResult = (jstring)(*env)->CallObjectMethod(env, myjobj, myMethodId);
Reference:
http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html
7.
Android NDK Gotchas
Managing your own reference counting
Check you have correct calls to NewStringUTF/ReleaseStringUTFChars
Create/delete global references using NewGlobalRef/DeleteGlobalRef
Library state on second run
Native libraries will not be unloaded after the calling Java activity exits!
Libraries need to be completely reinitialized when calling activity restarts. Hint:
extern int __bss_start;
extern int _end;
void reStart() {
void* bssBackup = native_backup_bss(); /* back-up somethings that need to be remembered */
memset((void*)__bss_start, 0 , _end-__bss_start); /* clear all memory that was initially zeroed */
9.
NDK Chip Architecture Support
ARM - nearly all smartphones use ARM chips
x86 - recently x86 tablets becoming more common
MIPs - not widely used
10.
Android NDK Debugging: The Old Way
Set up port forwarding for android emulator
adb forward tcp:1234 tcp:1234
Run gdbserver on device/emulator
gdbserver :1234 ondevicebinary args
Run gdb on PC
./arm-linux-androideabi-gdb.exe
Select file
file onPCbinary
Set shared object search path
set solib-search-path .
Run
target remote :1234
It appears that you have an ad-blocker running. By whitelisting SlideShare on your ad-blocker, you are supporting our community of content creators.
Hate ads?
We've updated our privacy policy.
We’ve updated our privacy policy so that we are compliant with changing global privacy regulations and to provide you with insight into the limited ways in which we use your data.
You can read the details below. By accepting, you agree to the updated privacy policy.