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.

Jesse Wilson

December 03, 2015
Tweet

More Decks by Jesse Wilson

Other Decks in Technology

Transcript

  1. JNI
    @jessewilson

    View Slide

  2. Java
    • Productive


    • Lots of great libraries


    • Fast enough

    View Slide

  3. JVM
    • Architecture neutral


    • Garbage collected


    • Fast
    Java Virtual Machine

    View Slide

  4. JVM
    • Architecture neutral


    • Garbage collected


    • Fast
    Java Virtual Machine
    just-in-time


    compiled

    View Slide

  5. JVM
    • Architecture neutral


    • Garbage collected


    • Fast
    Java Virtual Machine
    just-in-time


    compiled
    interpreted

    View Slide

  6. ART
    • Architecture neutral


    • Garbage collected


    • Fast
    Android Runtime

    View Slide

  7. ART
    • Architecture neutral


    • Garbage collected


    • Fast
    Android Runtime
    ahead-of-
    time


    compiled

    View Slide

  8. ART
    • Architecture neutral


    • Garbage collected


    • Fast
    Android Runtime
    just-in-time


    compiled
    ahead-of-
    time


    compiled

    View Slide

  9. ART
    • Architecture neutral


    • Garbage collected


    • Fast
    Android Runtime
    just-in-time


    compiled
    interpreted
    ahead-of-
    time


    compiled

    View Slide

  10. Architecture


    Neutral

    View Slide

  11. Popular


    Architectures
    • Desktops: x86, x64


    • Sma
    rt
    phones: ARM, MIPS

    View Slide

  12. Architecture


    Neutral
    • You ship one app


    • The runtime adapts it to the host
    computer’s OS and architecture

    View Slide

  13. Operating System
    Computer
    Application

    View Slide

  14. Operating System
    Computer
    Application
    architecture


    speci
    fi
    c

    View Slide

  15. Operating System
    Computer
    JVM / ART
    Application
    architecture


    speci
    fi
    c

    View Slide

  16. Operating System
    Computer
    JVM / ART
    Application
    architecture


    speci
    fi
    c
    architecture


    neutral

    View Slide

  17. Operating System
    Computer
    JVM / ART
    Application

    View Slide

  18. Operating System
    Computer
    JVM / ART
    Application

    View Slide

  19. Operating System
    Computer
    JVM / ART
    Application

    View Slide

  20. Operating System
    Computer
    JVM / ART
    Application

    View Slide

  21. Operating System
    Computer
    JVM / ART
    Application

    View Slide

  22. Operating System
    Computer
    JVM / ART
    Application

    View Slide

  23. Operating System
    Computer
    JVM / ART
    Application

    View Slide

  24. Operating System
    Computer
    JVM / ART
    Application

    View Slide

  25. Operating System
    Computer
    JVM / ART
    Application

    View Slide

  26. Operating System
    Computer
    JVM / ART
    Application

    View Slide

  27. Operating System
    Computer
    JVM / ART
    Application

    View Slide

  28. Operating System
    Computer
    JVM / ART
    Application

    View Slide

  29. Operating System
    Computer
    JVM / ART
    Application

    View Slide

  30. Operating System
    Computer
    JVM / ART
    Application

    View Slide

  31. Operating System
    Computer
    JVM / ART
    Application

    View Slide

  32. Operating System
    Computer
    JVM / ART
    App Java

    View Slide

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

    View Slide

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


    neutral

    View Slide

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


    neutral
    architecture


    speci
    fi
    c

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  50. App Java
    ARM C/C++

    View Slide

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

    View Slide

  52. App Java
    ARM C/C++
    foo.apk
    Image
    Image
    Image
    Image
    Image
    Image
    Image
    Image
    Layout
    Layout
    Resource
    Resource
    Translation
    XML
    Translation
    XML

    View Slide

  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

    View Slide

  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!

    View Slide

  55. Garbage


    Collected

    View Slide

  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?

    View Slide

  57. List characters = new ArrayList<>();

    characters.add("grant");

    characters.add("hammond");

    characters.add("malcolm");

    View Slide

  58. String
    String
    String
    ArrayList Object[] char[]
    char[]
    char[]

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  66. Moving


    References
    • You need to be careful about pointers
    into the managed heap


    • If an object is moved, your pointers
    will break!

    View Slide

  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!

    View Slide

  68. Fast


    Enough

    View Slide

  69. A Langauge


    of Abstraction
    • List vs. array


    • Methods are vi
    rt
    ual by default


    • Strict encapsulation

    View Slide

  70. A Dynamic


    Runtime
    • Re
    fl
    ection


    • Runtime code loading


    • Memory managed

    View Slide

  71. Native Code

    View Slide

  72. Typical


    Native


    Libraries
    • Cryptography


    • Databases


    • PDF viewers

    View Slide

  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

    View Slide

  74. Why Not


    Native? • “faster”

    View Slide

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

    View Slide

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

    View Slide

  77. “We rewrote our program in


    and it was 5x faster”
    C

    View Slide

  78. “We rewrote our program in


    and it was 5x faster”
    Java

    View Slide

  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

    View Slide

  80. Lots of fast


    Java programs
    • javac vs. gcj


    • IntelliJ vs. Visual Studio

    View Slide

  81. How to JNI

    View Slide

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

    View Slide

  83. View Slide

  84. View Slide

  85. public final class Hello {

    static {

    System.loadLibrary("jniexample");

    }


    public static native String greet(String name);

    }

    View Slide

  86. View Slide

  87. View Slide

  88. #include

    #include


    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);

    }


    View Slide

  89. http://tinyurl.com/jnidocs

    View Slide

  90. JNI Reference
    • Mapping Java types to C types


    • References


    • Calling into Java


    • Exceptions


    • Synchronization

    View Slide

  91. Tips
    • Android Studio


    • ProGuard


    • CheckJNI

    View Slide

  92. Building JNI
    • Much improved in 2015


    • Gradle!


    • Testing on desktop JVM

    View Slide

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

    View Slide

  94. Questions?

    View Slide