对于Android应用开发来说,最好能手绘下面的系统架构图:
https://raw.githubusercontent.com/BeesAndroid/BeesAndroid/master/art/android_system_structure.png
1.1.1. JNI
frameworks/base/core/jni 代码编译成libandroid_runtime.so
frameworks/base/media/jni 编译成 libmedia_jni.so
均被放置到system/lib目录
JNI正向调用
struct fields_t {
jfieldID context;
jmethodID post_event;
};
static fields_t fields;
/* 获取和调用 */
{
fields.context = env->GetFieldID(clazz, "mNativeContext", "I");
env->SetIntField(thiz, fields.context, (int)context);
fields.post_event = env->GetStaticMethodID(clazz,
"postEventFromNative",
"(Ljava/lang/Object;IIILjava/lang/Object;)V");
}
public class TestClass {
@SuppressWarnings("unused")
private int mNativeContext;
private EventHandler mEventHandler;
TestClass() {
/* ...... */
test(new WeakReference<TestClass>(this));
}
private class EventHandler extends Handler
{
private TestClass mTestClass;
public EventHandler(TestClass testclass, Looper looper) {
super(looper);
mTestClass = testclass;
}
反向调用
static jint
test (JNIEnv *env, jobject thiz,jobject weak_this) {
JNIEnv *env = AndroidRuntime::getJNIEnv();
jclass clazz = env->GetObjectClass(thiz);
jobject object = env->NewGlobalRef(weak_thiz);
env->CallStaticVoidMethod(clazz, fields.post_event,
object, msgType, ext1, ext2, NULL);
/* ...... */
}
public class TestClass {
@SuppressWarnings("unused")
private int mNativeContext;
private EventHandler mEventHandler;
TestClass() {
/* ...... */
test(new WeakReference<TestClass>(this));
}
private class EventHandler extends Handler
{
private TestClass mTestClass;
public EventHandler(TestClass testclass, Looper looper) {
super(looper);
mTestClass = testclass;
}