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

JNI Hello World (Android KW)

Jesse Wilson
December 03, 2015

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.

Jesse Wilson

December 03, 2015

Transcript

94 slides
  1. JVM • Architecture neutral • Garbage collected • Fast Java

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

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

    Runtime just-in-time compiled interpreted ahead-of- time compiled
  4. Architecture Neutral • You ship one app • The runtime

    adapts it to the host computer’s OS and architecture
  5. Operating System Computer JVM / ART App Java App C/C++

    architecture neutral architecture speci fi c
  6. App Java ARM C/C++ foo.apk Image Image Image Image Image

    Image Image Image Layout Layout Resource Resource Translation XML Translation XML
  7. 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
  8. 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!
  9. Garbage Collection • You solve application problems • The runtime

    worries about allocating and freeing memory • Why make a clumsy human do a computer’s job?
  10. String String String ArrayList Object[] char[] char[] char[] uint struct

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

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

    void* unsigned int* struct struct uint struct char* void* struct native heap managed heap
  13. 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
  14. Moving References • You need to be careful about pointers

    into the managed heap • If an object is moved, your pointers will break!
  15. 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!
  16. A Langauge of Abstraction • List vs. array • Methods

    are vi rt ual by default • Strict encapsulation
  17. 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
  18. 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
  19. public final class Hello { 
 static { 
 System.loadLibrary("jniexample");

    
 } 
 
 public static native String greet(String name); 
 }
  20. #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); 
 }
  21. JNI Reference • Mapping Java types to C types •

    References • Calling into Java • Exceptions • Synchronization

More Decks by Jesse Wilson

Other Decks in Technology