Two ways to dynamically load Dex

Source: Internet
Author: User

Dexclassloader loads a class that has no component life cycle, meaning that even if dexclassloader loads the component through dynamic loading of Dex, the load class fails when the system starts the component.
There are two ways to solve the problems that appear above:

Method One: http://blog.csdn.net/androidsecurity/article/details/8809542, change the classloader of the system to make it a custom loader.

Features: Two Dex has a distinct split line, the first Dex only starts, and the first Dex's class loading information is not followed by the so-called "shell-Up program".

Main points:

1.替换classloader:RefInvoke.setFieldOjbect("android.app.LoadedApk", "mClassLoader",wr.get(), dLoader);

2. Replace application:RefInvoke.setFieldOjbect("android.app.ActivityThread","mInitialApplication", currentActivityThread, app);

3.替换contentprovider RefInvoke.setFieldOjbect("android.content.ContentProvider","mContext", localProvider, app);

Method Two: Http://www.cnblogs.com/twlqx/p/4716026.html, the second Dex's class information is added to the first Dex's Pathclassloader element using a subcontracting method.

Features: Two DEX classes can be called each other, and in general, external Dex contains key hidden class information.

Main points:

1. Load external dex:

Dexclassloader Dexclassloader = new Dexclassloader (LibPath, Sapplication.getdir ("Dex", 0). GetAbsolutePath (), LibPath, Sapplication.getclassloader ());

2. Merge the element attribute of Pathclassloader and Dexclassloader and assign to the element of Pathclassloader

Object dexelements = Combinearray (getdexelements (Getpathlist (Pathclassloader)), Getdexelements (GetPathList ( (Dexclassloader)));

Object pathList = getpathlist (Pathclassloader);

SetField (PathList, Pathlist.getclass (), "dexelements", dexelements);

Two ways to dynamically load Dex

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.