With regard to personal understanding Instrumentation

Foreword

General application less likely to use instrumentation, so its online presentation is relatively small.

But because of its powerful functions and activity tracking application life cycle for android application testing framework, it is used as a base class.

instrumentation official document: http://developer.android.com/intl/zh-cn/reference/android/app/Instrumentation.html

Check method provides, such as: callActivityOnCreate, callApplicationOnCreate, newActivity, callActivityOnNewIntent etc. basically calls for all life and activity of the application will first call the appropriate method of instrumentation. And for all activity within the application are effective. It provides programmers with a powerful ability to have more possibilities to enter the android app framework of the implementation process.

For each android app, its total is ActivityThread :: main entrance process each application has a ActivityThread objects, and each object has a ActivityThread Instrumentation mInstrumentation;. Member variable. mInstrumentation in ActivityThread :: handleBindApplication initialization function:

if (data.instrumentationName !=null) {
...
    java.lang.ClassLoader cl = instrContext.getClassLoader();
    mInstrumentation = (Instrumentation)
                    cl.loadClass(data.instrumentationName.getClassName()).newInstance();
...
} else {
    mInstrumentation =newInstrumentation();
}

Custom instrumentation can only command line or by calling Context.startInstrementation am instrument start, the process will kill the target process and then restart (statement instrumention application and target application consistent signature required). If the application is not started, but under normal conditions (or launcher by startActivity) initiated by the above-described embodiment, a new default system Instrumentation.

If an application is to customize the application and instrumentation used to track the activity and the life cycle in this application, achieved by inheritance Instrumentation and methods modified ActivityThread.mInstrumentation reflection can be done. The following are the specific practices:

1, Custom Instrumentation
class MyInstrumentation extends Instrumentation {
  ...
};

2, reflecting calls modify ActivityThread.mInstrumentation
MyInstrumentation ins = new MyInstrumentation();

Class cls = Class.forName("android.app.ActivityThread"); // ActivityThread被隐藏了,所以通过这种方式获得class对象

Method mthd = cls.getDeclaredMethod("currentActivityThread", (Class[]) null); // 获取当前ActivityThread对象引用

Object currentAT = mthd.invoke(null, (Object[]) null);

Field mInstrumentation = currentAT.getClass().getDeclaredField("mInstrumentation");

mInstrumentation.setAccessible(true);//设置private变量为可读取

mInstrumentation.set(currentAT, ins); // 修改ActivityThread.mInstrumentation值

At this point you can track in-app activity and application life cycle by MyInstrumentation.

It is engaged in the development of the Android engineers for seven years, a lot of people ask me privately, 2019 Android how advanced the science, there is no method?

Yes, at the beginning I spent more than a month to sort out learning materials, hoping to help those who want to enhance the advanced Android development, but do not know how advanced learning friends. [ Including advanced UI, performance optimization, Architect courses, NDK, Kotlin, hybrid development (ReactNative + Weex), Flutter and other technical information architecture ], hoping to help review your pre-interview and find a good job, but also save in time they search for information online to learn.

Obtaining: Add Android architecture exchange QQ group chat: 513 088 520, into the group that is to receive the information! ! !

Click on the link to join a group chat Android mobile architecture [total population]: join a group chat

Sourcebook

Guess you like

Origin blog.csdn.net/weixin_43351655/article/details/91492601