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.
JNI@jessewilson
View Slide
Java• Productive• Lots of great libraries• Fast enough
JVM• Architecture neutral• Garbage collected• FastJava Virtual Machine
JVM• Architecture neutral• Garbage collected• FastJava Virtual Machinejust-in-timecompiled
JVM• Architecture neutral• Garbage collected• FastJava Virtual Machinejust-in-timecompiledinterpreted
ART• Architecture neutral• Garbage collected• FastAndroid Runtime
ART• Architecture neutral• Garbage collected• FastAndroid Runtimeahead-of-timecompiled
ART• Architecture neutral• Garbage collected• FastAndroid Runtimejust-in-timecompiledahead-of-timecompiled
ART• Architecture neutral• Garbage collected• FastAndroid Runtimejust-in-timecompiledinterpretedahead-of-timecompiled
ArchitectureNeutral
PopularArchitectures• Desktops: x86, x64• Smartphones: ARM, MIPS
ArchitectureNeutral• You ship one app• The runtime adapts it to the hostcomputer’s OS and architecture
Operating SystemComputerApplication
Operating SystemComputerApplicationarchitecturespecific
Operating SystemComputerJVM / ARTApplicationarchitecturespecific
Operating SystemComputerJVM / ARTApplicationarchitecturespecificarchitectureneutral
Operating SystemComputerJVM / ARTApplication
Operating SystemComputerJVM / ARTApp Java
Operating SystemComputerJVM / ARTApp JavaApp C/C++
Operating SystemComputerJVM / ARTApp JavaApp C/C++architectureneutral
Operating SystemComputerJVM / ARTApp JavaApp C/C++architectureneutralarchitecturespecific
App JavaARM C/C++
App JavaARM C/C++foo.apk
App JavaARM C/C++foo.apkImageImageImageImageImageImageImageImageLayoutLayoutResourceResourceTranslationXMLTranslationXML
App JavaARM C/C++x64 C/C++x86 C/C++MIPS C/C++foo.apkImageImageImageImageImageImageImageImageLayoutLayoutResourceResourceTranslationXMLTranslationXML
JNI &Architecture• Build & ship a JNI library for everyCPU+OS pair you support• That means you need to decide whichCPU+OS pairs to support!• Fortunately, Android is a single OS,but there are still several CPUs!
GarbageCollected
GarbageCollection• You solve application problems• The runtime worries about allocatingand freeing memory• Why make a clumsy human do acomputer’s job?
List characters = new ArrayList<>(); characters.add("grant"); characters.add("hammond"); characters.add("malcolm");
StringStringStringArrayList Object[] char[]char[]char[]
StringStringStringArrayListObject[]char[]char[]char[]0x000x100x200x300x400x50
StringString StringArrayListObject[]char[]char[]char[]0x000x100x200x300x400x50
StringString StringArrayListObject[]char[]char[]char[]uintstructvoid*unsigned int*structstructuintstruct char*void* struct
StringString StringArrayListObject[]char[]char[]char[]uintstructvoid*unsigned int*structstructuintstruct char*void* structnative heap
StringString StringArrayListObject[]char[]char[]char[]uintstructvoid*unsigned int*structstructuintstruct char*void* structnative heapmanaged heap
GarbageCollector• Within a managed heap, an object’sidentity and location in memory aredecoupled• A compacting collector defragmentsthe heap by moving objects around
MovingReferences• You need to be careful about pointersinto the managed heap• If an object is moved, your pointerswill break!
TrackingReferences• You need to keep the garbagecollector informed about what objectsyou’re using• Otherwise they could be collected outfrom under you!
FastEnough
A Langaugeof Abstraction• List vs. array• Methods are virtual by default• Strict encapsulation
A DynamicRuntime• Reflection• Runtime code loading• Memory managed
Native Code
TypicalNativeLibraries• Cryptography• Databases• PDF viewers
Why Native?• Cross-platform• Historical accident• Inefficient on Java’s performancemodel• Benefits from access to fancyoperating system features
Why NotNative? • “faster”
“We rewrote our program inand it was 5x faster”C
“We rewrote our program inand it was 5x faster”Java
Java,Fast & Slow• Most programs are slow because theyuse inappropriate datastructures,algorithms, and abstractions• Because Java has many rich APIs,Java programs are often faster
Lots of fastJava programs• javac vs. gcj• IntelliJ vs. Visual Studio
How to JNI
http://tinyurl.com/jni-example
public final class Hello { static { System.loadLibrary("jniexample"); } public static native String greet(String name); }
#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); }
http://tinyurl.com/jnidocs
JNI Reference• Mapping Java types to C types• References• Calling into Java• Exceptions• Synchronization
Tips• Android Studio• ProGuard• CheckJNI
Building JNI• Much improved in 2015• Gradle!• Testing on desktop JVM
Testing JNI • You’ll want a separate Gradle module
Questions?