System.loadLibrary("Unix, Native Code and why bother")

System.loadLibrary("Unix, Native Code and why bother")

Presented at AppDevCon (Amsterdam) 2018

2bf14ad96e09ea7eda4973795ce70fa6?s=128

Júlio Zynger

March 16, 2018
Tweet

Transcript

  1. System.loadLibrary( “Unix, Native Code and why bother”)

  2. Library static shared static dynamic loading (.o, .a, ...) (.so,

    .dll, .dylib, ...)
  3. Program A Program B static libs (.a) static libs (.a)

    static linking compile-time
  4. Program A Program B Program A Program B static libs

    (.a) static libs (.a) shared libs (.so) static linking dynamic linking compile-time run-time
  5. Library static shared bookstore library Paul Richter

  6. source code compiler

  7. source code compiler objects (.o) .o .o .o .a archive

    .o
  8. source code compiler linker objects (.o) .o .o .o .a

    archive .o executable .so .so .so .so
  9. source code compiler linker loader objects (.o) .o .o .o

    .a archive .o executable in-memory image .so .so .so .so
  10. Building the objects

  11. lib1.c

  12. main.c

  13. Static Libraries

  14. Static Libraries

  15. Shared Libraries: Static Loading

  16. Shared Libraries: Static Loading LD_LIBRARY_PATH

  17. Shared Libraries: Dynamic Loading

  18. main.c

  19. main.c

  20. Android

  21. Kernel Display driver USB driver Camera driver WiFi driver Power

    Mgmt. Binder IPC ... Application Framework Apps Home Contacts Dialer Clock Your app 3rd party app ... Activity Manager Window Manager Package Manager Location Manager View System Content Providers ... Libraries Android Runtime (Dalvik/ART) Core Libraries Dalvik Virtual Machine (DVM) Surface Manager OpenGL Media Framework SQLite WebKit SSL ... Bionic
  22. Kernel Display driver USB driver Camera driver WiFi driver Power

    Mgmt. Binder IPC ...
  23. Kernel Display driver USB driver Camera driver WiFi driver Power

    Mgmt. Binder IPC ... Libraries Surface Manager OpenGL Media Framework SQLite WebKit SSL ... Bionic
  24. Android Bionic

  25. Bionic libc libm libdl Linker libstdc++

  26. Bionic libc libm libdl Linker libstdc++ License Size Speed

  27. Bionic libc libm libdl Linker libstdc++ BSD + Rewritten code

    + Android specific services
  28. Bionic libc libm libdl Linker libstdc++

  29. Bionic libc libm libdl Linker libstdc++ Completely from scratch! dlopen()

    dlsym() dlerror() dlclose() {
  30. Bionic libc libm libdl Linker libstdc++

  31. Kernel Display driver USB driver Camera driver WiFi driver Power

    Mgmt. Binder IPC ... Libraries Android Runtime (Dalvik/ART) Core Libraries Dalvik Virtual Machine (DVM) Surface Manager OpenGL Media Framework SQLite WebKit SSL ... Bionic
  32. Kernel Display driver USB driver Camera driver WiFi driver Power

    Mgmt. Binder IPC ... Application Framework Apps Home Contacts Dialer Clock Your app 3rd party app ... Activity Manager Window Manager Package Manager Location Manager View System Content Providers ... Libraries Android Runtime (Dalvik/ART) Core Libraries Dalvik Virtual Machine (DVM) Surface Manager OpenGL Media Framework SQLite WebKit SSL ... Bionic
  33. Kernel Audio driver Application Framework Apps Your app Media Player

    Libraries Android Runtime (Dalvik/ART) Core Libraries Dalvik Virtual Machine (DVM) Media Framework Media Player Audio Flinger libaudio.so JNI dlopen()
  34. System.loadLibrary

  35. System.loadLibrary Runtime.loadLibrary

  36. Runtime.java

  37. Runtime.java

  38. None
  39. None
  40. Win: main.dll Linux: libmain.so Mac: libmain.dylib

  41. None
  42. LD_LIBRARY_PATH

  43. Runtime.java

  44. //TODO Runtime::doLoad Runtime.java

  45. //TODO Runtime::doLoad

  46. //TODO Runtime::doLoad LD_LIBRARY_PATH

  47. ClassLoader search (for application libraries) VM search (for system libraries)

  48. Runtime.java

  49. Workaround for zygote’s LD_LIBRARY_PATH Runtime.java

  50. Runtime.cpp

  51. Native.cpp

  52. Native.cpp

  53. Native.cpp

  54. Kernel Application Framework Apps Your app Runtime.java Libraries Android Runtime

    Dalvik Virtual Machine (DVM) Native.cpp Runtime.cpp yourlib.so JNI dlopen()
  55. We learned... • Static vs Shared Libraries • Dynamic Loading

    • Library paths (+ difference on android) • Android’s internals ◦ Bionic ◦ Dynamic Loading with Dalvik ◦ System.loadLibrary
  56. Thanks! @juliozynger soundcloud.com/jobs