First, plug-in origin
Plug-in technology originally originated from the idea of running Apk without installation. This Apk without installation can be understood as a plug-in, and the app that supports plug-ins is generally called the host.
As we all know, in the Android system, applications exist in the form of Apk, applications need to be installed to use. In fact, the Android system installation is quite simple, in fact, the application Apk copy to a different directory on the system, and then extract so.
Common application installation directories are as follows:
/system/app
: System application/system/priv-app
: System application/data/app
: User application
You may wonder, since the installation process is so easy, how does Android run the application code, let’s first look at the composition of Apk, a common Apk will contain the following several parts:
classes.dex
:Java
Code bytecoderes
: Resource filelib
:so
fileassets
: static asset fileAndroidManifest.xml
: manifest file
When you open the application on Android, you just open the process and use the ClassLoader to load classs. dex into the process and execute the corresponding components.
Why can’t we execute code in Apk since Android itself uses a similar form of reflection to load code?
Two, plug-in advantages
Plugins allow code in Apk (mainly Android components) to run without installation, which brings many benefits:
- To reduce the installation
Apk
Volume, download modules on demand - Dynamic update plug-in
- Hosts and plug-ins are compiled separately to improve development efficiency
- The number of solutions exceeds 65535
Imagine that your app has the same high performance as a Native app, but still gets the same revenue as a Web app.
Well, it’s nice, isn’t it?
Third, the difference with componentization
- componentization: Will be one
App
Divided into multiple modules, each module is a component (module), the development process can make these components dependent on each other or independent compilation, debugging part of the components, but these components will eventually merge into a completeApk
Publish to the app market. - Plug-in: The whole App is divided into many modules, each module is an Apk (componentized module is a lib), and the host Apk and plug-in Apk are packaged separately in the final packaging process. Only the host Apk is released to the application market, and the plug-in Apk is dynamically delivered to the host Apk on demand.
4. Technical difficulties of plug-in
To make the plug-in Apk work, we need to find the location of the plug-in Apk first, and then we need to be able to parse and load the code in the Apk.
But it doesn’t make sense to execute Java code with light. There are four major components that need to be registered in the Android system. ActivityManagerService (AMS) and PackageManagerService (PMS) are registered in the Android system, and the four components depend on AMS and PMS for parsing and startup. Get him to acknowledge a component in an uninstalled Apk, How to make the host dynamically load Android components (Activity, Service, BroadcastReceiver, ContentProvider, Fragment) in Apk is the biggest difficulty of plug-in.
Application resource references are also a problem. Imagine what happens when you load a plug-in Apk using reflection in the host process and the id corresponding to R cannot refer to the correct resource.
To sum up, in fact, the main points of plug-in are these:
- How do I load and execute a plug-in
Apk
Code (ClassLoader Injection
) - Enable the system to call plug-ins
Apk
Components in (Runtime Container
) - Correctly identify plug-ins
Apk
Resources (Resource Injection
)
Plug-in is a very interesting of learning, there are many online “primer” and “principle” articles, but not about the actual combat of plug-in a share system, comprehensive, I want to share is the recent below small make up in tencent high hand gathers up to the “plug-in” practical learning manual, The boss has led the development of several APP projects with plug-in architectures. The manual includes the popular plug-in projects of the Internet giants, from the novice to the architect, this learning manual is enough.
Chapter one, the history of plug-in technology
- Plug-in feed
- History of plug-in development
Chapter 2 Principle of plug-in
- Class loading
- Parental delegation mechanism
- Resource to load
- Four components support
- The Proxy agent Activity
- Hook way
The third chapter is an exploration of Android plug-in
- Implement a plug-in framework from scratch
- The start process of the Activity
- To find a place to Hook
- Lu code phase
- Proxy objects
- Replace the original Intent
- ActivityThread
- Replace the intent for the broker back
Click on the blue font here to get the full document.
Chapter 4 Evolution of Architecture (Dachang)
- DroidPlugin for 360 plug-in development
Plug-in development 360 DroidPlugin source code analysis
The Hook mechanism of 360 DroidPlugin source code analysis for plug-in development
The package structure relationship of Hook mechanism
Hook mechanism class diagram relationship
Sequence diagram relationship of Hook mechanism
Manifest Permission Application
What does the base Hook do?
.
- Didi VirtualApk combat
configuration
application
The principle of
conclusion
The problem
- Plug-in for Android refactoring based on VirtualAPK
Plug-in Hello World
Choice of plug-in framework
Principle of plug-in
Introducing the pain of plugins
- Neptune framework of iQiyi plug-in principle analysis
Plug-in basic knowledge
Class loading
Resource usage restrictions in plug-in
Apk packaging process
Plug-in of four components
Plug-in Activity task stack
Handles broadcasts in plug-ins
Handle services in plug-ins
- Tencent plug-in framework Shadow project analysis
Sample Code androidmanifest.xml analysis
The Activity implementation
Replace the parent class of the plug-in Activity
The Service implementation
BroadcastReceiver implementation
ContentProvider implementation
The framework itself is dynamic
The above is Tencent internal strong launch of “plug-in actual combat learning manual”,Click on the blue font here to get the full document.
Write in the last
Plug-in is a very interesting knowledge, in a word to describe it is dark under the lamp, in a variety of pit restrictions and constantly game with the system to find a way out. With the understanding of the depth, we can certainly understand my sentence, this article is just to throw a brick to introduce jade, more fun or to explore their own.