Hands on implementation of Android App plug-in
At present, there are many open source projects in China, but it is difficult to master them if they are not actually developed again.
The following is to start from 0, combined with the current open source projects and blogs, to develop plug-in solutions.
Plug in as needed mainly solves the following problems:
1. Code loading
(1) To solve the loading of pure java code
(2) Android component loading, such as activity, service, broadcast receiver and ContentProvider, requires special processing because they have a life cycle
(3) Loading of Android native code
(4) Handling of Android special controls, such as notification, etc
2. Resource loading
How are the resources of different plug-ins managed? Is it a common set or a plug-in managed independently?
Because access to resources in Android is realized through R,
Let's solve the above problems step by step
1. Loading of pure java code
It mainly adds the path of the plug-in to the original array through classloader and changing dexelements.
For detailed analysis, please refer to an article I reprinted. Because I feel that the naming and structure of the original post are a little messy, reprint it and record it.
https://my.oschina.net/android520/blog/794715
Android provides dexclassloader and PathClassLoader. Both inherit basedexclassloader, but the parameters of the construction method are different, that is, the path of optdex is different. The source code is as follows
The optimized directory is used to store the DEX directory after opt, and must be an internal storage path.
Dexclassloader can load external DEX or APK, as long as the path of OPT sets an internal storage path through the parameter.
PathClassLoader can only load installed apks, because the opt path will use the default DEX path, and external ones cannot.
The following describes how to load java code through dexclassloader. Refer to Nuwa
This method is similar to hot fix. If the plug-in and host code have access to each other, you need to use instrumentation technology in packaging.
The activity started by the above method has a life cycle. It should use the default activity creation method instead of its own new activity object, so the open activity life cycle is normal.
However, in the above way, you must ensure that the activity is registered in the host androidmanifest.xml.
2. The following describes how to load unregistered activity functions
Activity loading principle reference https://my.oschina.net/android520/blog/795599
It is mainly completed through the iactivitymanager of the hook system
3. Resource loading
Resources are accessed through R. mode. In fact, Android will generate an int constant value in 0x7F *************************************************************.
If the resource changes, such as layout, ID, drawable, etc., the r.java content will be regenerated, and the int constant value will also change.
Because the resources in the plug-in do not participate in the resource compilation of the host program, they cannot be accessed through R.
Refer to the following for specific principles: https://www.oudahe.com/p/27137/
After adding the plug-in path to the host program by using the addassetpath method, because the plug-in is packaged independently, the resource ID also starts from 1, and the host program also starts from 1, which may lead to a conflict between the plug-in and the host resource. When the system loads the resource, the latest found resource shall prevail, so it is impossible to guarantee whether the interface displays the host or the plug-in.
For this method, you can change the generated range of the resource ID of each plug-in during packaging. Please refer to the introduction of public.xml.
Code reference Amigo
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.