Android JNI has many requirements for using native objects. Its implementation is very simple. The following uses paint as an example to record it.
Paint. Java (frameworks \ base \ graphics \ Java \ Android \ graphics)
public class Paint { public int mNativePaint; public Paint(int flags) { mNativePaint = native_init(); ... ... } private static native int native_init(); ... ...};
Paint. cpp (frameworks \ base \ core \ JNI \ Android \ graphics)
class SkPaintGlue { static SkPaint* init(JNIEnv* env, jobject clazz) { SkPaint* obj = new SkPaint(); defaultSettingsForAndroid(obj); return obj; } ... ...};static JNINativeMethod methods[] = { {"native_init","()I", (void*) SkPaintGlue::init}, ... ...};
Mnativepaint in the Java layer is int, corresponding to skpaint in the native Layer *.
In this way, when you need to operate native layer objects on the Java layer, as long as the mnativepaint int is passed, native will know that it is the skpaint pointer.
It is very simple, but I think it is very powerful and clever.
Think about how to represent the C ++ object in the native layer on the Java layer, and think about pointers and integers.