Running native code on Android #OSDCfr 2012

699 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
699
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Running native code on Android #OSDCfr 2012

  1. 1. Running native code on Android C NDK ARMCédric @deltheil OSDC.fr 2012-10-13
  2. 2. - Why using native code? -
  3. 3. 1. Reuse some C code
  4. 4. 2. Use 3rd party libraries
  5. 5. 3. Create portable components Android iOS ...
  6. 6. 4. Use hardware acceleration SIMD CPU algorithm
  7. 7. 5. ... because it’s fun :)
  8. 8. ...but How?Native Development Kit
  9. 9. - Android NDK overview -
  10. 10. Downloadshttp://developer.android.com/tools/sdk/ndk/index.html cross-platform
  11. 11. Contents1. Tools for Linux,OS X,Win to cross-compilenative ARM,x86,MIPS binaries.
  12. 12. Contents2. System files and headers for Android nativeAPIs. w/ Java Native Interface (a.k.a JNI) <jni.h>
  13. 13. Contents3. Easy-to-use build system: Android.mk &ndk-build (make wrapper).
  14. 14. Contents4. Documentation andcode samples.
  15. 15. - Steps to embed C code -
  16. 16. 1/3 Wrap C Java libmisc.soJava JNI C C code
  17. 17. 1/3 Wrap C/* 1. Wrap your C code with the JNI */#include <jni.h>jstringJava_com_example_Foo_bar(JNIEnv* env, jobject thiz) { char buffer[512]; /* ... */ return (*env)->NewStringUTF(env, buffer);}namespace class method native interface instance
  18. 18. 2/3 Build native lib Java libmisc.soJava JNI C C code
  19. 19. 2/3 Build native lib# 2. Generate a lib with ndk-build$ ndk-buildCompile thumb : misc <= misc.cStaticLibrary : libmisc.aSharedLibrary : libmisc.soInstall : libmisc.so => libs/armeabi/libmisc.so...
  20. 20. 3/3 Java ext Java libmisc.soJava JNI C C code
  21. 21. 3/3 Java ext/* 3. Expose the logic via a native extension */package com.example;public class Foo extends /* ... */ { static { System.loadLibrary("misc"); } public native String bar();}
  22. 22. Basic Java libmisc.soJava JNI C C code
  23. 23. Advanced Java libmisc.so Java JNI C3rd party libs C codeA sources libA.a libB.a B sources
  24. 24. - Building 3rd party libs -
  25. 25. Pro-tipUse the Standalone Toolchain.
  26. 26. What?A customized install for a given platform,arch. handy
  27. 27. Why?No need to write specific Android.mkmakefiles: reuse existing build systems. 0 LOC Makefile autotools
  28. 28. How?
  29. 29. [Ex. 1] jsmnMakefile C JSON parser
  30. 30. [Ex. 1] jsmn 1/3override Makefile variables
  31. 31. [Ex. 1] jsmn 2/3
  32. 32. [Ex. 1] jsmn 3/3choose the proper arch... and that’s it! Full gist @ http://git.io/ndk-jsmn
  33. 33. [Ex. 2] msgpackautotools C/C+ binary serialization
  34. 34. [Ex. 2] msgpack 1/3
  35. 35. [Ex. 2] msgpack 2/3
  36. 36. [Ex. 2] msgpack 3/3use the cross-toolchains... and that’s it! Full gist @ http://git.io/ndk-msgpack
  37. 37. - Using a prebuilt library -
  38. 38. OverviewAndroid.mk libs/ ndk-build armeabi/ libfoo.soApplication.mk single arch
  39. 39. OverviewAndroid.mk libs/ ndk-build armeabi/ libfoo.so armeabi-v7a/ libfoo.soApplication.mk fat binary
  40. 40. - Loading a native library -
  41. 41. BasicPick the right arch vs.target device
  42. 42. Medium
  43. 43. Custom frontal library native method (frontal lib.) Pick the right arch yourself!
  44. 44. Custom: why?[1] Properly target ARMv7 without NEON CPUswhen getCpuFeatures() can’t be used atruntime (e.g. 3rd party libs)[2] Bypass a loadLibrary bug on ICS![1] e.g. «the NVidia Tegra 2 generation SoC has a dual-core ARM Cortex-A9 CPU (lacking ARMsadvanced SIMD extension—NEON)» - see http://en.wikipedia.org/wiki/Tegra[2] see http://www.moodstocks.com/2012/03/20/ice-cream-sandwich-why-native-code-support-sucks
  45. 45. - Quick JNI hints -
  46. 46. C pointer = int field e.g. persist a DB handle and use it throughout the JNI extensionsDon’t forget to destruct it (explicitlyor at finalize() time)
  47. 47. C error codes C error code & string
  48. 48. Resources#AltDevBlogADay NDK Part 1 & Part 2 Linux Mag., 07/11 Tokyo Cabinet Java Pkg VLC for Android
  49. 49. Thanks!Questions? Comments? cedric@moodstocks.com | @deltheil

×