In the Android app development process, there are often some framework layer bugs that cause the app process to crash, such as the crash caused by cookie synchronization. In this case, if we can intercept the API calls to the framework layer, we can avoid this problem by wrapping it in a wrapper to catch the exception.
At that time, the pure Java layer is not possible, and the proxy mechanism in Java can only be defined for their own classes, the system framework layer of the class is no use. This is a way to implement the process class arbitrary Java method interception by borrowing from Java's jni mechanism.
Any Java method in the C + + layer corresponding to a method class, if it is a Jni method, the method class member variable Nativefunc point is the JNI native implementation, if not the Jni method, then point to the Java method implementation, and call Dvminterpret to execute the Java method. Using this mechanism, we can implement the method interception. Assuming we have a Java layer approach Java_method needs hooks, we'll put the hook method Java_method_hook to Java_method's C-layer method, and put it in its member variable ins. Next, we define a C-layer method C_hookhandler, assign this method to the NATIVEFUNC member variable, and then, when C_hookhandler is called, you can invoke the Java_method_ in the INS member variable. Hook to do the Java_method intercept.
For a more detailed and systematic process, refer to the following article, or xposed source code.
http://blog.csdn.net/luoshengyang/article/details/8914953
Xposed The biggest difference is that by replacing the app_process to achieve the hack of other processes, the method interception can be applied to any process.
Implement arbitrary Java method interception in Android app development