ndk integration (jni) - pub.rondk integration (jni) lecture 6 android native development kit 1 april...

41
NDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Upload: others

Post on 19-Jun-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

NDK Integration (JNI)Lecture 6

Android Native Development Kit

1 April 2014

NDK NDK Integration (JNI), Lecture 6 1/41

Page 2: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Array Operations

NIO Operations

Accessing Fields

Calling Methods

Handling Exceptions

Local & Global References

Threads

Standard JNI vs. Android JNI

Keywords

NDK NDK Integration (JNI), Lecture 6 2/41

Page 3: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Outline

Array Operations

NIO Operations

Accessing Fields

Calling Methods

Handling Exceptions

Local & Global References

Threads

Standard JNI vs. Android JNI

Keywords

NDK NDK Integration (JNI), Lecture 6 3/41

Page 4: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Arrays

I Java arrays - reference type in JNI

I JNI treats primitive arrays and object arrays differently

I Primitive arrays contain primitivesI Object arrays contain class instances or other arrays

I Object[] and int[][] are object arrays

I jarray and subtypes (jintArray, jobjectArray)

NDK NDK Integration (JNI), Lecture 6 4/41

Page 5: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

New Array

I New<Type>Array where Type is Int, Char, Boolean, etc.jintArray javaArray = env->NewIntArray(10);

I In case of memory overflowI Function returns NULLI Exception is thrown in the VMI Native code should be stopped

NDK NDK Integration (JNI), Lecture 6 5/41

Page 6: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Accessing Array Elements using a Copy

I Copy Java array into C array or obtain a direct pointer to thearray elements

I Copy Java array into C arrayI Get<Type>ArrayRegion

jint nativeArray[10];env->GetIntArrayRegion(javaArray, 0, 10, nativeArray);

I Make changes on the array elementsI Copy C array back into Java array

I Set<Type>ArrayRegion

env->SetIntArrayRegion(javaArray, 0, 10, nativeArray);

I Performance problem for big array size

NDK NDK Integration (JNI), Lecture 6 6/41

Page 7: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Operating on Direct Pointer

I Obtain a direct pointer to the array elements when possible

I Get<Type>ArrayElementsjint* nativeDirectArray;jboolean isCopy;nativeDirectArray = env->GetIntArrayElements(javaArray, &isCopy);

I isCopy - the C array points to a copy or a pinned array inheap

I Returns NULL if operation fails

NDK NDK Integration (JNI), Lecture 6 7/41

Page 8: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Releasing a Direct Pointer

I Release array returned by Get<Type>ArrayElements

I Release<Type>ArrayElements

env->ReleaseIntArrayElements(javaArray,nativeDirectArray, 0);

I Last parameter - release modeI 0 - copy back content, free native arrayI JNI COMMIT - copy back content, do not free native array

(update Java array)I JNI ABORT - do not copy back content, free native array

I GetArrayLength

I Get/ReleasePrimitiveArrayCritical

NDK NDK Integration (JNI), Lecture 6 8/41

Page 9: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Object Array

I Create new object arrayI NewObjectArray

jobjectArray arr = env->NewObjectArray(size,javaClass, NULL);

I Params: length, class and initialization value

I Obtain an element from an object arrayI GetObjectArrayElementI Cannot obtain all object elements

jstring js = (jstring)env->GetObjectArrayElement(arr, i);

I Update an element in an object arrayI SetObjectArrayElement

env->SetObjectArrayElement(arr, i, js);

NDK NDK Integration (JNI), Lecture 6 9/41

Page 10: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Outline

Array Operations

NIO Operations

Accessing Fields

Calling Methods

Handling Exceptions

Local & Global References

Threads

Standard JNI vs. Android JNI

Keywords

NDK NDK Integration (JNI), Lecture 6 10/41

Page 11: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

NIO Operations

I Native I/O - buffer management, scalable network and file I/O

I Better performance - deliver data between native and Java appI Create a direct byte buffer to be used in the Java app

I NewDirectByteBuffer

unsigned char* buffer = (unsigned char*) malloc(1024);jobject directBuffer;directBuffer = env->NewDirectByteBuffer(buffer, 1024);

I Based on a native byte array

I Obtain native byte array from Java byte bufferI GetDirectBufferAddress

unsigned char* buffer;buffer = (unsigned char*) env->GetDirectBufferAddress(directBuffer);

I The direct byte buffer can also be created in the Java app

NDK NDK Integration (JNI), Lecture 6 11/41

Page 12: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Outline

Array Operations

NIO Operations

Accessing Fields

Calling Methods

Handling Exceptions

Local & Global References

Threads

Standard JNI vs. Android JNI

Keywords

NDK NDK Integration (JNI), Lecture 6 12/41

Page 13: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Field Types

I In Java: static fields and instance fields

I Each instance has its own copy of the instance fieldsprivate String instanceField = "Instance Field";

I All instances share the same static fieldsprivate static String staticField = "Static Field";

I JNI functions for both types of fields

NDK NDK Integration (JNI), Lecture 6 13/41

Page 14: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Get Field ID

I Obtain class object from instanceI GetObjectClass

jclass cl = env->GetObjectClass(instance);

I Obtain field ID of an instance fieldI GetFieldID

jfieldID instanceFieldId;instanceFieldId = env->GetFieldID(cl,"instanceField", "Ljava/lang/String;");

I Last parameter - field descriptor

I Obtain field ID of static fieldI GetStaticFieldID

jfieldID staticFieldId;staticFieldId = env->GetStaticFieldID(cl,"staticField", "Ljava/lang/String;");

NDK NDK Integration (JNI), Lecture 6 14/41

Page 15: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Get Fields

I Obtain an instance fieldI Get<Type>Field

jstring instanceField;instanceField = env->GetObjectField(instance,instanceFieldId);

I Obtain a static fieldI GetStatic<Type>Field

jstring staticField;staticField = env->GetStaticObjectField(cl,staticFieldId);

I Type = Object, Primitive type

I Return NULL in case of memory overflowI Performance overhead

I Recommended to pass parameters to native methods

NDK NDK Integration (JNI), Lecture 6 15/41

Page 16: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Outline

Array Operations

NIO Operations

Accessing Fields

Calling Methods

Handling Exceptions

Local & Global References

Threads

Standard JNI vs. Android JNI

Keywords

NDK NDK Integration (JNI), Lecture 6 16/41

Page 17: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Types of Methods

I In Java: instance and static methods

I Instance methodprivate String instanceMethod() {

return "Instance Method";}

I Static methodprivate static String staticMethod() {

return "Static Method";}

I JNI functions to access both types

NDK NDK Integration (JNI), Lecture 6 17/41

Page 18: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Get Method ID

I Obtain method ID of an instance methodI GetMethodID

jmethodID instanceMethodId;instanceMethodId = env->GetMethodID(cl,"instanceMethod", "()Ljava/lang/String;");

I Last parameter - method descriptor (signature)

I Obtain method ID of a static methodI GetStaticMethodID

jmethodID staticMethodId;staticMethodId = env->GetStaticMethodID(cl,"staticMethod", "()Ljava/lang/String;");

NDK NDK Integration (JNI), Lecture 6 18/41

Page 19: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Get Methods

I Call instance methodI Call<Type>Method

jstring instanceMethodResult;instanceMethodResult = env->CallObjectMethod(instance, instanceMethodId);

I Call static methodI CallStatic<Type>Field

jstring staticMethodResult;staticMethodResult = env->CallStaticObjectMethod(cl, staticMethodId);

I Type = Void, Object, Primitive type

I Specify method arguments after method ID

I Return NULL in case of memory overflowI Performance overhead

I Minimize transitions between Java and native code

NDK NDK Integration (JNI), Lecture 6 19/41

Page 20: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Field & Method Descriptors

Java Type SignatureBoolean ZByte BChar CShort SInt ILong JFloat FDouble Dfully-qualified-class Lfully-qualified-classtype[] [typemethod type (arg-type)ret-type

I Use javap to obtain descriptors associated to a Java class

NDK NDK Integration (JNI), Lecture 6 20/41

Page 21: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Outline

Array Operations

NIO Operations

Accessing Fields

Calling Methods

Handling Exceptions

Local & Global References

Threads

Standard JNI vs. Android JNI

Keywords

NDK NDK Integration (JNI), Lecture 6 21/41

Page 22: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Catch Exceptions

I Handling exceptions is important in Java

I VM catches exception, clears exception and executes handlingblock

I In native code developers must implement exception handlingflow

I Catch an exception generated while calling a Java methodI ExceptionOccured

env->CallVoidMethod(instance, methodID);jthrowable ex = env->ExceptionOccurred();if (ex != NULL) {

env->ExceptionClear();/* Handle exception here */

}

NDK NDK Integration (JNI), Lecture 6 22/41

Page 23: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Throw Exceptions

I Native code can throw Java exceptions

I First obtain exception classI Throw exception

I ThrowNewjclass cl = env->FindClass("java/lang/NullPointerException");if (cl != NULL) {

env->ThrowNew(cl, "Message");}

I Does not automatically stop native method and transfercontrol to exception handler

I Should free resources and return

NDK NDK Integration (JNI), Lecture 6 23/41

Page 24: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Outline

Array Operations

NIO Operations

Accessing Fields

Calling Methods

Handling Exceptions

Local & Global References

Threads

Standard JNI vs. Android JNI

Keywords

NDK NDK Integration (JNI), Lecture 6 24/41

Page 25: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

References

I The VM tracks object references and garbage collects theones that are not referenced

I JNI allows native code to manage object references andlifetimes

I 3 types of references: local, global and weak global

NDK NDK Integration (JNI), Lecture 6 25/41

Page 26: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Local References

I Most JNI functions return local references

I Cannot be cached and reused in subsequent invocations

I Lifetime limited to the native method - freed when methodreturns

I Minimum 16 local references for the native code in the VM

I Free local references while making memory-intensiveoperations

I Manually free local referenceI DeleteLocalRef

jclass cl = env->FindClass("java/lang/String");env->DeleteLocalRef(cl);

NDK NDK Integration (JNI), Lecture 6 26/41

Page 27: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Global References

I Valid during subsequent invocations of the native method

I Until explicitly freedI Create new global reference

I NewGlobalRefjclass localCl = env->FindClass("java/lang/String");jclass globalCl = env->NewGlobalRef(localCl);env->DeleteLocalRef(localCl);

I Delete global reference when no longer usedI DeleteGlobalRef

env->DeleteGlobalRef(globalCl);

I Can be used by other native methods or native threads

NDK NDK Integration (JNI), Lecture 6 27/41

Page 28: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Weak Global References

I Valid during subsequent invocations of the native method

I The object can be garbage collectedI Create new weak global reference

I NewWeakGlobalRefjclass weakGlobalCl;weakGlobalCl = env->NewWeakGlobalRef(localCl);

NDK NDK Integration (JNI), Lecture 6 28/41

Page 29: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Weak Global References

I Verify if the reference is still pointing to an instanceI IsSameObject

if (env->IsSameObject(weakGlobalCl, NULL) == JNI_-FALSE) {

/* Object is still live */} else {

/* Object is garbage collected */}

I Delete weak global referenceI DeleteWeakGlobalRef

env->DeleteWeakGlobalRef(weakGlobalCl);

NDK NDK Integration (JNI), Lecture 6 29/41

Page 30: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Outline

Array Operations

NIO Operations

Accessing Fields

Calling Methods

Handling Exceptions

Local & Global References

Threads

Standard JNI vs. Android JNI

Keywords

NDK NDK Integration (JNI), Lecture 6 30/41

Page 31: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Threads

I Multithreaded environmentI Threads vs. references

I Local references valid only in the thread context executing thenative method

I Local references cannot be shared between multiple threadsI Global references can be shared between multiple threads

I Threads vs. JNIEnvI Interface pointer valid only in the thread executing the native

methodI Cannot be cached and used by other threads

NDK NDK Integration (JNI), Lecture 6 31/41

Page 32: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Native Threads

I Use threads for running tasks in parallel

I Linux threads, scheduled by the kernel

I Started from managed code with Thread.start

I Can also be started with pthread createI Native threads not known by the VM until they are attached

I No JNIEnvI Cannot make JNI calls

NDK NDK Integration (JNI), Lecture 6 32/41

Page 33: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Native Threads

I First attach native thread to the VMI AttachCurrentThread

JavaVM* cachedJvm;JNIEnv* env;cachedJvm->AttachCurrentThread(&env, NULL);

I Obtain a JNIEnv interface pointer for the current threadI java.lang.Thread object added to main ThreadGroupI Last argument: JavaVMAttachArgs structure - can specify

other thread group

I AttachCurrentThreadAsDaemonI After communication with the Java app, detach from VM

I DetachCurrentThreadcachedJvm->DetachCurrentThread();

NDK NDK Integration (JNI), Lecture 6 33/41

Page 34: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Synchronization

I Synchronization using monitors based on Java objects

I Only one thread can hold a monitor at a timeI Acquire a monitor

I MonitorEnterenv->MonitorEnter(obj);

I obj is a Java objectI If another thread owns the monitor, waits until it’s releasedI If no other thread owns the monitor, becomes owner, entry

counter = 1I If the current thread owns the monitor, increments the entry

counter

NDK NDK Integration (JNI), Lecture 6 34/41

Page 35: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Synchronization

I Release monitorI MonitorExit

env->MonitorExit(obj);I The current thread must be the owner of the monitorI Entry counter is decrementedI When counter = 0, the current thread releases the monitor

NDK NDK Integration (JNI), Lecture 6 35/41

Page 36: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Outline

Array Operations

NIO Operations

Accessing Fields

Calling Methods

Handling Exceptions

Local & Global References

Threads

Standard JNI vs. Android JNI

Keywords

NDK NDK Integration (JNI), Lecture 6 36/41

Page 37: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Standard JNI vs. Android JNI

I All JNI 1.6 features are supported by Android JNII Exception: DefineClass not implemented

I No Java bytecodes or class files in AndroidI Not useful

I JNI does not include proper error checksI Android includes CheckJNI mode

I Performs series of checks before the actual JNI function iscalled

I Enable CheckJNI from adb shellI Enabled by default on emulator

NDK NDK Integration (JNI), Lecture 6 37/41

Page 38: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

CheckJNI

I Attempt to allocate negative-sized arrays

I Passing a bad pointer(jobject, jclass, jarray, jstring) to a JNIcall

I Passing a NULL pointer to a JNI call when argument shouldnot be NULL

I Passing a class name not correctly specified to a JNI call

I Making a JNI call in a critical region

I Passing invalid arguments to NewDirectByteBuffer

I Making a JNI call while an exception is pending

NDK NDK Integration (JNI), Lecture 6 38/41

Page 39: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

CheckJNI

I Using JNIEnv* in the wrong thread

I Using NULL, wrong type fieldID, static/instance mismatch

I Using invalid jmethodID, incorrect return type, static/instancemismatch, invalid instance/class

I Using DeleteGlobal/LocalRef on the wrong reference

I Passing a bad release mode, other than 0, JNI COMMIT,JNI ABORT

I Returning incompatible type from native method

I Passing invalid UTF-8 sequence to a JNI call

NDK NDK Integration (JNI), Lecture 6 39/41

Page 40: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

Outline

Array Operations

NIO Operations

Accessing Fields

Calling Methods

Handling Exceptions

Local & Global References

Threads

Standard JNI vs. Android JNI

Keywords

NDK NDK Integration (JNI), Lecture 6 40/41

Page 41: NDK Integration (JNI) - pub.roNDK Integration (JNI) Lecture 6 Android Native Development Kit 1 April 2014 NDK NDK Integration (JNI), Lecture 6 1/41

I Primitive array

I Object array

I Direct pointer

I Native I/O

I Static/instance fields

I Static/instance methods

I Field/method ID

I Field/method descriptor

I Catch/throw exception

I Local/global/weak globalreferences

I Native threads

I Monitor

I CheckJNI

NDK NDK Integration (JNI), Lecture 6 41/41