Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JNI Hello World (Android KW)

JNI Hello World (Android KW)

Video: https://www.youtube.com/watch?v=uglvahwOvIM

A quick look at what JNI is, how it works, and how you can use it to be awesome.

92c80ad799d0788de9ec7a4e3a26f98d?s=128

Jesse Wilson

December 03, 2015
Tweet

More Decks by Jesse Wilson

Other Decks in Technology

Transcript

  1. JNI @jessewilson

  2. Java • Productive • Lots of great libraries • Fast

    enough
  3. JVM • Architecture neutral • Garbage collected • Fast Java

    Virtual Machine
  4. JVM • Architecture neutral • Garbage collected • Fast Java

    Virtual Machine just-in-time compiled
  5. JVM • Architecture neutral • Garbage collected • Fast Java

    Virtual Machine just-in-time compiled interpreted
  6. ART • Architecture neutral • Garbage collected • Fast Android

    Runtime
  7. ART • Architecture neutral • Garbage collected • Fast Android

    Runtime ahead-of- time compiled
  8. ART • Architecture neutral • Garbage collected • Fast Android

    Runtime just-in-time compiled ahead-of- time compiled
  9. ART • Architecture neutral • Garbage collected • Fast Android

    Runtime just-in-time compiled interpreted ahead-of- time compiled
  10. Architecture Neutral

  11. Popular Architectures • Desktops: x86, x64 • Sma rt phones:

    ARM, MIPS
  12. Architecture Neutral • You ship one app • The runtime

    adapts it to the host computer’s OS and architecture
  13. Operating System Computer Application

  14. Operating System Computer Application architecture speci fi c

  15. Operating System Computer JVM / ART Application architecture speci fi

    c
  16. Operating System Computer JVM / ART Application architecture speci fi

    c architecture neutral
  17. Operating System Computer JVM / ART Application

  18. Operating System Computer JVM / ART Application

  19. Operating System Computer JVM / ART Application

  20. Operating System Computer JVM / ART Application

  21. Operating System Computer JVM / ART Application

  22. Operating System Computer JVM / ART Application

  23. Operating System Computer JVM / ART Application

  24. Operating System Computer JVM / ART Application

  25. Operating System Computer JVM / ART Application

  26. Operating System Computer JVM / ART Application

  27. Operating System Computer JVM / ART Application

  28. Operating System Computer JVM / ART Application

  29. Operating System Computer JVM / ART Application

  30. Operating System Computer JVM / ART Application

  31. Operating System Computer JVM / ART Application

  32. Operating System Computer JVM / ART App Java

  33. Operating System Computer JVM / ART App Java App C/C++

  34. Operating System Computer JVM / ART App Java App C/C++

    architecture neutral
  35. Operating System Computer JVM / ART App Java App C/C++

    architecture neutral architecture speci fi c
  36. Operating System Computer JVM / ART App Java App C/C++

  37. Operating System Computer JVM / ART App Java App C/C++

  38. Operating System Computer JVM / ART App Java App C/C++

  39. Operating System Computer JVM / ART App Java App C/C++

  40. Operating System Computer JVM / ART App Java App C/C++

  41. Operating System Computer JVM / ART App Java App C/C++

  42. Operating System Computer JVM / ART App Java App C/C++

  43. Operating System Computer JVM / ART App Java App C/C++

  44. Operating System Computer JVM / ART App Java App C/C++

  45. Operating System Computer JVM / ART App Java App C/C++

  46. Operating System Computer JVM / ART App Java App C/C++

  47. Operating System Computer JVM / ART App Java App C/C++

  48. Operating System Computer JVM / ART App Java App C/C++

  49. Operating System Computer JVM / ART App Java App C/C++

  50. App Java ARM C/C++

  51. App Java ARM C/C++ foo.apk

  52. App Java ARM C/C++ foo.apk Image Image Image Image Image

    Image Image Image Layout Layout Resource Resource Translation XML Translation XML
  53. App Java ARM C/C++ x64 C/C++ x86 C/C++ MIPS C/C++

    foo.apk Image Image Image Image Image Image Image Image Layout Layout Resource Resource Translation XML Translation XML
  54. JNI & Architecture • Build & ship a JNI library

    for every CPU+OS pair you suppo rt • That means you need to decide which CPU+OS pairs to suppo rt ! • Fo rt unately, Android is a single OS, but there are still several CPUs!
  55. Garbage Collected

  56. Garbage Collection • You solve application problems • The runtime

    worries about allocating and freeing memory • Why make a clumsy human do a computer’s job?
  57. List<String> characters = new ArrayList<>(); 
 characters.add("grant"); 
 characters.add("hammond"); 


    characters.add("malcolm");
  58. String String String ArrayList Object[] char[] char[] char[]

  59. String String String ArrayList Object[] char[] char[] char[] 0x00 0x10

    0x20 0x30 0x40 0x50
  60. String String String ArrayList Object[] char[] char[] char[] 0x00 0x10

    0x20 0x30 0x40 0x50
  61. String String String ArrayList Object[] char[] char[] char[] 0x00 0x10

    0x20 0x30 0x40 0x50
  62. String String String ArrayList Object[] char[] char[] char[] uint struct

    void* unsigned int* struct struct uint struct char* void* struct
  63. String String String ArrayList Object[] char[] char[] char[] uint struct

    void* unsigned int* struct struct uint struct char* void* struct native heap
  64. String String String ArrayList Object[] char[] char[] char[] uint struct

    void* unsigned int* struct struct uint struct char* void* struct native heap managed heap
  65. Garbage Collector • Within a managed heap, an object’s identity

    and location in memory are decoupled • A compacting collector defragments the heap by moving objects around
  66. Moving References • You need to be careful about pointers

    into the managed heap • If an object is moved, your pointers will break!
  67. Tracking References • You need to keep the garbage collector

    informed about what objects you’re using • Otherwise they could be collected out from under you!
  68. Fast Enough

  69. A Langauge of Abstraction • List vs. array • Methods

    are vi rt ual by default • Strict encapsulation
  70. A Dynamic Runtime • Re fl ection • Runtime code

    loading • Memory managed
  71. Native Code

  72. Typical Native Libraries • Cryptography • Databases • PDF viewers

  73. Why Native? • Cross-platform • Historical accident • Inef fi

    cient on Java’s pe rf ormance model • Bene fi ts from access to fancy operating system features
  74. Why Not Native? • “faster”

  75. Operating System Computer JVM / ART App Java App C/C++

  76. Operating System Computer JVM / ART App Java App C/C++

  77. “We rewrote our program in and it was 5x faster”

    C
  78. “We rewrote our program in and it was 5x faster”

    Java
  79. Java, Fast & Slow • Most programs are slow because

    they use inappropriate datastructures, algorithms, and abstractions • Because Java has many rich APIs, Java programs are often faster
  80. Lots of fast Java programs • javac vs. gcj •

    IntelliJ vs. Visual Studio
  81. How to JNI

  82. http://tinyurl.com/jni-example

  83. None
  84. None
  85. public final class Hello { 
 static { 
 System.loadLibrary("jniexample");

    
 } 
 
 public static native String greet(String name); 
 }
  86. None
  87. None
  88. #include <string.h> 
 #include <jni.h> 
 
 JNIEXPORT jstring JNICALL

    
 Java_com_squareup_jniexample_Hello_greet(JNIEnv *env, jclass type, jstring name_) { 
 const char *name = (*env)->GetStringUTFChars(env, name_, 0); 
 
 char *greeting; 
 if (strcmp(name, "Jesse") == 0) { 
 greeting = "Yo dog"; 
 } else { 
 greeting = "Hello"; 
 } 
 
 (*env)->ReleaseStringUTFChars(env, name_, name); 
 
 return (*env)->NewStringUTF(env, greeting); 
 }
  89. http://tinyurl.com/jnidocs

  90. JNI Reference • Mapping Java types to C types •

    References • Calling into Java • Exceptions • Synchronization
  91. Tips • Android Studio • ProGuard • CheckJNI

  92. Building JNI • Much improved in 2015 • Gradle! •

    Testing on desktop JVM
  93. Testing JNI • You’ll want a separate Gradle module

  94. Questions?