Implement arbitrary Java method interception in Android app development

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.