Write in front
"Replugin will be open source at the end of June, and this will be our best gift to the Android world. "When we announce this news, the excitement in our hearts is not a word." Yes, three years of "thick product", today's "thin hair", seemingly ordinary words, actually full of us too much excitement, bitterness and tears.
So today, we will come to the detailed chat with you, this from mid-2014, officially launched in the mobile phone defender, and will be open-source replugin, what can we, more you can bring what.
GitHub Address: Https://github.com/Qihoo360/RePlugin. You are welcome to add star to the Replugin project, send pull Request, and mention issue. We will do everything we can to answer your questions.
What is Replugin?
Replugin is a set of complete, stable, suitable for all-round use, the hole-type plug-in solution. The main advantages are:
- Extremely flexible: The main program does not need to be upgraded (no need to embed components in manifest), can support the new four components, even new plug-ins;
- Very stable: Only one spot at the Hook (Classloader). Its crash rate is only "one out of 10,000" and is perfectly compatible with nearly all Android ROMs on the market;
- Rich features: Support almost all the features in the "single product" development, including static receiver, task-affinity, custom theme, process pit, APPCOMPAT, etc.
- Process arbitrary: Allows the components to run in the UI, resident, or even "arbitrary pit-bit process";
- Easy integration: No matter the plug-in or the main program, only "a few lines" to complete the access;
- Free Isolation: Isolate the isolation (such as unstable or resource-based plug-ins, easy to release), do not want to isolate the module is mixed (such as a variety of basic, UI plug-ins, all running in the UI process, excellent performance);
- * * Management maturity: with mature and stable "plug-in management scheme", support plug-in installation, upgrade, uninstall, version management, even including process communication, protocol version, security check, etc.
- Hundreds of millions of support: There are 360 mobile phone defenders large user volume to do support.
By the end of June 2017, Replugin:
- The number of plugins has reached 102 (including 57 core base plug-ins).
- The plug-in accounts for the application ratio (refers to the spread of code resources, plug-ins accounted for the entire application) up to 83%.
- The annual number of releases is up to 596 times (average 2-3 times per working day).
In addition, at present, nearly all of the 360 companies of billions of users of the app, as well as a variety of mainstream third-party apps, have adopted the Replugin solution.
Replugin's core strengths
Comparison of Replugin with existing plug-in frameworks
Why to do replugin plug-in benefits
In telling us why we should design a set of our own plug-ins at the end of 2013, let's start with a brief talk about how convenient it is to have a plug-in solution, and what problems it solves.
For users
- Everything on demand: using a plug-in solution, you can make your application "small and fine". Only when a user needs to use a particular feature can it be downloaded and turned on, and the plugin can be uninstalled at any time. This can not only reduce the size of the APK, save traffic, but also significantly reduce memory, internal storage footprint, more space to the precious photos, documents and other information;
- Experience the new version at any time: no need to apply to the market until the big package upgrade, users can experience the new version of the application at any time. Now smash hit's plug-in, dynamic (RN Class), hot new technology, are more or less around this point, it is visible to the user's enormous value.
For developers
- Flexible version: Do not wait for the market to go online, and other users actively upgrade, the results missed valuable time. The plug-in solution allows you to "release" at any time, and is not subject to the "Release window period" limit. Even for different regions, different user groups, different time to update, and can quickly verify their own ideas;
- Flexible organizational structure: Once the release becomes flexible enough, the organizational structure can be from the original "unified combat" into "Hundred-Regiment combat", each team in the development of "their own plug-in", to develop their own version of the plan;
- Modular thinking: Allows the team to form "module awareness". Of course, plug-ins, plug-ins and hosts allow moderate coupling, but not "uncontrolled". This makes it easy for developers to realize that we are "protocols between plugins" rather than "fooling around under the same roof", forcing teams to develop applications in new and appropriate ways;
- Android Native Advantage: Unlike dynamic (RN), you can use the most familiar Java/kotlin language and a variety of native APIs to develop your plugins. This allows the application to be more "fit" with the system, taking advantage of the advantages of the native and almost unaffected by performance.
As mentioned above, the use of the plugin framework is beneficial both to users and developers, and should be "flying into the ordinary".
However, in the course of the actual investigation, we found a strange phenomenon that does not match these benefits completely. What is it exactly?
Since the plug-in is so good, why ...
Although I had done my homework beforehand, the findings at a technical conference were surprisingly--only less than 5% of the 200 Android developers attended, using a plug-in solution. More than 90% of developers are currently not using plug-in applications in software development.
In fact, this is basically consistent with the results we observe online. In conjunction with previous surveys, we found that there are three major challenges that constrain the ubiquity of plugins in the Android Development community:
- Not stable enough: there are many more flexible plug-in framework, although the support features are numerous, but because the hook point more, so not very stable. So many large projects are not very willing to use them to develop plug-ins, worry about the application crashes, plug-ins do not work properly and so on.
- Not flexible: There are some relatively stable plug-in framework, there are "not flexible, free" problem, once the plug-in has a large change, such as new activity, Service, process, etc., you need the main program, not to mention "a year ago the main program, no upgrade, you can use new plugins and components." As a result, the "access motivation" of many projects has also been compromised.
- Feature-rich project-specific: The current market of plug-in solutions, mostly only in large-scale functional projects, is considered for use, and more for the edge function, such as "Red envelopes", "Weather", "Shake" and so on, they think that only "non-core" modules, will consider making plug-ins. This also makes the application of the plug-in a very narrow range.
However, through our years of practice has proved that the above three challenges, in fact, can be overcome. This is what we are going to introduce to you today, 360 mobile defender the first Android open source project--replugin.
Now that we are so bold, how do we do that?
How do we do "not stable enough" how to break?
As mentioned earlier, the main reason for not being stable is that there are too many hooks. So the more flexible plug-in framework on the market, what is the hook?
Note: the "Hook" here refers to the behavior of getting and modifying the internal API that interacts with the system server, such as the ones listed above, by means of Java reflection. Reflection under normal circumstances (for example, the Reflection class's own field) does not belong to the hook.
Seemingly flexible, however, the following three scenarios are likely to lead to plug-ins and even applications that do not work completely:
- Android Upgrade: Since it is an internal API, then Android will not be considered to be "cannot be modified", once the system upgrade to make a change, the light is not functional, heavy is directly crash. For example, some plug-in framework has encountered an exception on Android 7.0, must upgrade the main program to resolve the incident, vividly.
- Rom modification: More frightening than Android upgrade, is the third-party ROM on the internal API "various changes." This adaptation difficulty is conceivable. For example, custom resource, custom wifiservice, etc. caused by "plug-in Murder", and so on.
- Improper use: "Often in the river stand, where there are not wet shoes", hook more points, once the realization of a certain principle of understanding and error. As a result, naught is not said, there may be more serious, and more difficult to detect the collapse of the incident, the fear of extreme.
Based on the above situation, our team at the beginning of 2014 to study the new occupied pit plug-in framework (note that at this time the Droidplugin class scheme has not yet appeared), then set a "small target": the less the hook the better. After a study and discussion, we finally decided to hook only one point: ClassLoader, and asked to "stick to the end", all the changes are based on this to unfold.
For us, this is a landmark decision, even now.
Unique Hook--classloader
It is not difficult to modify the ClassLoader point, as shown in the gradual reflection can be. However, it should be noted that this classloader must inherit from the Pathclassloader, to prevent Android 7.x because of the use of Adddexpath problems.
In addition, the location of the ClassLoader is also very stable. For now, there has been no change in location or name from Android 2.1, which can be used for a long time.
In this regard, we will later have an article to elaborate, please look forward to.
"Not flexible" how to break?
As mentioned earlier, as far as the plug-in framework in the market is concerned, if you do enough stability, you will lose some flexibility. This is also unacceptable for products where we have so many modules.
To this end, we in the "adhere to a hook point" principle, under the premise of continuous innovation, and ultimately solve the above problems.
Our core idea is that the word "cliché" began after 2015, and that is: pit position.
The idea of pit position scheme
- Non-pit position scheme: standard one by one correspondence relationship. For example, the plugin has a xxxactivity, then the main program requires that there must also be a xxxactivity (the name may not be the same) to correspond.
Once the plugin is adding a new activity, the main program must also be added, otherwise it will not be able to use the activity.
- Quasi-pit plan: Some (such as 2013 years of us) through the fragment to simulate activity, so as to achieve a certain degree of flexibility.
However, there is a real need, such as the interaction with other applications of activity, and so on, it is difficult to call it a complete pit-position thought.
- Pit position scheme: can achieve "one-to-many" relationship. For example, the plugin has a xxxactivity, then the main program can be used to map its own n1st1 to this xxxactivity. Once the activity exits, the n1st1 is "idle". And when Yyyactivity comes in, it will re-occupy N1st1. This allows a pit position (such as n1st1) to correspond to multiple entities.
In addition, this yyyactivity can be either existing or add-ons. This way, no matter how the plugin is upgraded, the main program can support the new activity without moving.
Of course, we originally designed the pit-bit thought (I would like to stress, is the beginning of 2014), but also not only for the activity, but the entire four components, even to the later stage, even theme, process, task-affinity, etc. have done "pit", but the way to achieve different.
Due to space limitations, this is simply a summary of the usual activity. For more details, please continue to follow our "Replugin depth Analysis" series of articles.
Activity Pit Bit
At present, the complete pit position in the market, the location of the hook can be said to be "all the same", from the AMS, instrumentation to Contextimpl have, and in order to make the plug-ins become more flexible.
And our plan is somewhat different from them: except ClassLoader is hook, the rest does not need. So how do we start a plug-in activity?
In a nutshell, we have five core steps:
- Record: "Record" the name of the activity to be opened by means of the Pm.startactivity method;
- Find a pit: A series of processes to find a usable pit position (e.g. n1st1) and record;
- Open pit: The pit is opened directly by the startactivity of the system (note that there is no hook here);
- Intercept: When the system is tuned to our hostclassloader (the only hook point), we "block one" to find the real activity (xxxactivity) corresponding to this pit bit (N1ST1);
- Return: Loads the plug-in and gets the real activity's class object and returns it to the system.
Where pm.startactivity can be called directly by a plugin/host. If the inside of the plug-in, you can directly through the StartActivity method to open, more convenient.
Of course, this is only the core idea, and every step we will do the free collection system software logic processing, especially the "Find Pit" section, which is one of our core.
Activity layered Pit level
Looking for a pit is a lot of attention points:
- From the perspective of "stratification", it is necessary to support: various launchmode, all transparent/non-transparent theme, taskaffinity pits, process pits, etc., even appcompat situation to consider.
- From the perspective of "management", it is necessary to consider the recovery and release of pits, the allocation of pits, and even the treatment strategies when the pits are insufficient.
Space is limited, will be written in detail later, please look forward to.
Forward to perfection--dynamic compilation scheme
Through the narrative just now, like pm.startactivity, there are ways to make the plugin "cost-free". However, calls like provider (essentially Icontentprovider), service stopself (final), and activity involving the allocation of a pit-bit, have to be replicated accordingly. There are two conflicting points in this:
- If not hook, you have to plug-in developers "self-processing", slightly cumbersome, not perfect;
- Once the hook (such as trying to hook AMS, Icontentprovider, etc.), but also destroyed our adherence to the "1 hook principle", and then worry about future compatibility issues
The pros and cons are troublesome.
In response to this problem, our core philosophy is: "No compromise on hooks and stability", instead of innovative to do a set of "dynamic compilation scheme", trying to solve this problem from the "compile time".
In general, there are some classes and methods that we think developers need to modify, with the magic of javaassist to make automated changes, which saves the developer's cost of change and achieves the desired effect.
A picture of:
And when you do this, you will find that the following "dream" becomes reality:
For example, there is an app called "360 desktop" that wants to turn itself into a "plugin" to run. Then, with the "dynamic compilation scheme", combined with "plug-in class library" and framework support, the ultimate effect is-just a few lines of gradle, you can directly generate an APK. This apk:
- Can be used as a plugin to run directly in the main program;
- can also be installed as a single product directly into the device.
Yes, it is such a magic!
That's it?
Of course it's much more than that. I've shown a video that turns a huge, complex 360 desktop into a plug-in that runs in 360 mobile phone guards.
Imagine, a desktop plug-in involves the function is "all aspects", small to taskdesription and so's use, big to four components, task-affinity pit, static receiver and process pit bit processing, all need one by one. So, to do this, is not just the points mentioned above can be done.
Of course, "making 360 desktops into plugins" is not the most meaningful. Really let Replugin become more meaningful, is to take our 360 mobile phone guard to "surgery", let hundreds of-even say, almost everything-the module, have become Replugin plug-in, and perfect run up.
How to Break a "feature-rich project"?
As mentioned earlier, the reason for "special feature-rich projects", mainly and the current market plug-in solutions to the positioning of the developers think: "plug-in = Free Installation", "Basic in the main program more assured", "plug-in development costs" and so on.
However, after careful analysis of the underlying causes, the most important reason is that the "positioning" of the plug-in and related frameworks (including the thermal update scheme, etc.) is different. What I know is:
- Component: As represented by ATLAS/ACDD, the official definition is "to complete the installation of bundles, load classes and resources on demand in a running environment" to address the challenges of large team collaboration and to provide thermal remediation capabilities. Its dependence on the "compile period" is more but stable. And the addition of large components, you still need the main program to solve, after all, as the official said, "modular ≠ plug-in";
- Dynamic plug-in: dynamic-load-apk as the representative, the purpose is to solve the issue of the release, upgrade. Most use "non-Occupy pit" thinking, when adding new components, will still require upgrading the main program. Such programs are more, and is the "originator" of the plug-in, worthy of our respect and learning;
- App-Free: Droidplugin is the official definition of "APK file can be run without installation or modification". The scene is more similar to "app Fen"
In addition, the project was developed by the mobile phone Assistant team of our 360 company, which was released in mid-2015.
- Hot FIX: With Tinker/robust as the representative, the goal is to quickly hit a variety of patches at minimal cost, so that the application can be continuously updated. Its core advantage is "patching without restarting the process". Similarly, the newly added bulk feature will still need to be upgraded to the main program, which is not the primary goal of the "hot fix".
So, are we one of these three goals?
Obviously, it's not. So, what exactly is our goal?
Answer: Full plug-in
Our goal is only one: Full plug-in.
- Full Plug-in: Replugin as the representative. The goal is to "make as many modules as possible plugins", and to be as flexible as possible with the development of "single product", and enjoy the benefits of a plug-in solution, on a very stable basis.
That is, whether it is the UI, core business, partner plug-ins, background services, or basic functions, can become plug-ins, and within the framework of Replugin stable and flexible operation. Not only can large projects be used, but small projects--even just calculators--can use replugin to improve their flexibility and ultimately the magic effect of "plug-ins are full of the world."
And this is the main difference between us and most of the current plug-in frameworks on the market.
Current status of the defender plugin
At present, the mobile phone defender has a plug-in, can be divided into the following categories for the application Developer Reference:
- UI plug-in: such as home page (yes, you did not read wrong), physical examination, information flow, etc.;
- Business plug-ins: such as cleaning, harassment interception, suspended windows, etc.
- Cooperation Plug-in: such as program lock, free WiFi, secure desktop, etc.;
- Background plug-ins: such as push, service management, PROTOBUF, etc.;
- Basic plugins: such as security webview, sharing, positioning, etc.
And such plugins, we have 102 of them. Can imagine, once these plug-ins can not be used, then the mobile phone defender instantly become empty shell. Three years has gone, think back, it is worth remembering.
Together to create a higher value
Speaking of which, reminds me of Lody (Virtualapp author, high school Daniel) said in an interview:
"The sophistication of the plug-in technology has risen in recent years, but it is still in the early and middle stages." The advent of app sandbox technology is the innovation of plug-in development and the product of the first stage. In the future, I believe that a lot of plug-in technology will be more application, if the plug-in stabilized to a certain extent, even can subvert the way app development. ”
This, in fact, is also the ultimate value of replugin, that is-let the plug-in can "fly into the ordinary application", to achieve stability, flexibility, freedom, the size of the project concurrently.
Of course, in the "full plug-in" or even "Universal plug-in" road, we have too much to go, and such a large and complex replugin, is more than 10 research and development staff to work together the results, and has been led by the department, and the company's technical committee of the strong support. I believe that Replugin's open source is a new beginning.
360 Mobile Defender Plugin Replugin today open source