Android Plugin thinking lucky airship Platform Rental latest full summary

Source: Internet
Author: User

Introduction of Plug-in
Baidu Encyclopedia is the lucky Airship Platform Rental (www.1159880099.com) QQ1159880099 so the definition of plug-ins: "is a certain specification of the application interface written out of the program, can only run in the program specified by the system platform, and can not be separated from the specified platform to run alone. , that is, the plug-in can provide a dynamic extension that allows the application to load functionality that was not part of the application at runtime and to dynamically update and replace it.

So in Android, what is "plug-in", as the name implies, is to some of the core complex dependencies of high-level business modules into a separate plug-in, and then according to different business needs of different combinations, dynamic replacement, can be management of plug-ins, updates, late to plug-ins can also be version management operations. There are two concepts to be explained in the plugin:

Host

The so-called host, is the need to provide a running environment, to provide a context for resource invocation, the general is our main APK, to run the application, it as the main project of the application, the implementation of a set of plug-in loading and management framework, plug-ins are based on the host apk and exist.

Plug - ins

Plug-ins can be imagined as each standalone function module encapsulated as a small apk that can be configured and updated online to implement plug-in apk in the host apk in the online and offline, as well as dynamic updates and other features.

So why use plug-in technology, what advantages it can bring to us, here is a brief list of the following points:

Allows users to upgrade app functionality without reinstalling the APK, reducing the frequency of releases and increasing the user experience.

Provides an ability to quickly fix bugs and updates on the line.

Load different modules on demand for flexible feature configurations and reduce server-to-previous interface compatibility pressures.

Modularity, decoupling, parallel development, 65535 issues.

Introductory knowledge
First of all, we need to know that the plug-in technology is a more complex area, the complex point is that it involves a wide range of knowledge, not only the upper layer to do the application architecture capabilities, but also requires us to the Android system underlying knowledge needs to have a certain understanding, here is a simple list of the knowledge involved in the point

640?wx_fmt=png

First of all, to introduce the binder, we all know that the Android multi-process communication core is binder, if not it really difficult. Binder involves two layers of technology, and you can think of it as a mediator mode, where binders act as intermediaries between the client and server side. If you want to implement the four components of the plug-in, you need to modify the binder, Binder service side of the content can not be modified, only the client's code, and the four components of each component of the client are not the same, this needs to be researched. The best way to learn binder is aidl, there is a lot of information on the Internet, the simplest way is to write a aidl file to automatically generate a Java class, and then to see the Java class of each method and variable, and then to see the four components, in fact, are similar to AIDL implementation.

Second, it is the process of App packaging. Once the code is finished, a package operation is performed, and the process of resource packaging, Dex generation, and signing is experienced halfway through. One of the most important is the packaging of resources, that is, aapt this step, if the host and the plug-in resource ID conflict, a workaround is to make changes here.

Third, the APP's installation process on the phone is also important. Familiarity with the installation process is not only helpful for plug-ins, but also very important when encountering installation bugs. When the mobile phone installs the app, often has the download exception, prompts the resource package to be unable to parse, then needs to know installs the app this piece of code where, this is only the first step. The second step needs to know what to do when the APP is downloaded locally. Some directories cannot be accessed, the APP is downloaded to a local directory, and then the files are generated. Plug-in has an incremental update concept, how to download an incremental package, where to remove a package from the local location, what the specific naming rules are, and so on. These details have to be clearly understood.

The start-up process is the App. How many ways does Activity start? One is to write a startactivity, the second is to click the Mobile app, through the mobile phone system in the Launcher mechanism, launch the APP's default Activity. Typically, the way the App developer loved it was the second. So what's the starting principle of the first way? Also, where is the Main function when it starts? The location of this main function is important, and we can make modifications to the class in which it resides, allowing for plug-ins.

5th more important, do Android plugin need to control two places. The first is the plugin Dex load, how to load the class in the plugin dex into memory? In addition, the problem of resource loading. Plugins may be Apk or so format, no matter which, will not generate r.id, so no way to use. There are several solutions to this problem. One is to rewrite the Context of the Getasset, GetResource and other methods, the concept of the plug-ins to read the resources in the plug-in, but the disadvantage is that the host and the plugin's resource ID will conflict, need to rewrite aapt. The other is to rewrite the list of plugins saved in AMS, allowing the host and plugin to load their own resources without conflict. The third way, after packaging, executes a script that modifies the resource ID in the build package.

6th, after the implementation of the plug-in, how to solve the different plug-in developer's work area problem. For example, plug-in 1 and plug-in 2, you need to download which code separately, how to run independently? Just like ticket and train tickets, how do you just run your own plugin instead of running someone else's plugin? This is a matter of co-operation. Train tickets and tickets, the respective workspaces of the two Android teams are not the same, this time to use the Gradle script, each project has its own warehouse, have their own different packaging scripts, only need to put their own plug-in and the host project to run together, instead of introducing other plug-ins, What's more, it's also possible to pack and run your plugin as an App.

The above introduces the introduction of the plug-in knowledge, a total of six points, each point needs to spend a lot of time to understand. Otherwise, in the face of the plug-in project, many places you will confused. And as long as you understand the six-point core, everything can be solved.

Implementation principle
The application of plug-in technology in Android, in fact the dynamic loading process, is divided into the following steps:

Copy the executable (. so/dex/jar/apk, etc.) to the inside of the app app.

Load executable file, replace static resource

Invoke concrete methods to execute business logic

In Android projects, dynamic loading techniques can be broadly divided into two types depending on the executable file being loaded:

Dynamic load. So library

Dynamically loading the dex/jar/apk file (now dynamically loaded is generally said to be this)

1th, the NDK in Android actually uses dynamic loading, dynamically loading. So libraries and invoking their encapsulated methods through JNI. The latter is generally compiled from C/s + + and runs on the Native layer, which is much more efficient than executing Java code at the virtual machine layer, so it is often loaded dynamically in Android. So library to perform some performance-specific tasks (such as Bitmap decoding, image Gaussian blur processing, etc.) 。 Also, since. So libraries are compiled from C/s + + and can only be disassembled into assembler code, it is more difficult to crack than the Smali code that is compiled in the Dex file, so the. So library can also be used in the security realm.

Second, "Dynamically loading dex/jar/apk files based on ClassLoader" means that we mean dynamically loading the Dex package compiled by Java code in Android and executing the code logic in it, a technique that is less used in conventional Android development, and is now said Dynamic loading refers to this.

Android project, all Java code will be compiled into the Dex file, the Android application run, is to execute the Dex file in the business code logic to work. With dynamic load technology, you can load external dex files while the Android app is running, while downloading new Dex files over the network and replacing the old Dex files to upgrade the app without installing the new APK file (changing the code logic).

So, the ClassLoader mechanism in Android is primarily used to load Dex files, and the system provides two APIs to choose from:

Pathclassloader: You can only load APK files that are already installed on your Android system. therefore does not conform to the plug-in demand, does not consider.

Dexclassloader: Support for loading external apk, Jar or Dex files, just in line with the requirements of the file, all the plug-in solution is to use Dexclassloader to load the plugin apk for the. class file.

Mainstream framework
The main issues that need to be addressed in implementing the plug-in framework in Android are as follows:

Loading of resources and code

Android life cycle Management and component registration

Conflict resolution for host APK and plugin APK resource reference

The following analysis of several current mainstream open source framework, see each framework concrete implementation ideas and advantages and disadvantages.

DL Dynamic loading Framework (end of 2014)

is a proxy-based implementation of the plug-in framework, the application of the surface of the processing, by registering the agent component in the Manifest, when the plug-in component is started, first start a proxy component, and then through this proxy component to build, start the plug-in component. Need to follow certain rules to develop the plug-in APK, the components in the plug-in need to implement the modified Activity, fragmentactivity, Service, and other sub-classes.

The advantages are as follows:

Plugins need to follow certain rules, so the security aspects can be controlled.

The scheme is simple and suitable for the plug-in transformation of its own small amount of code.

Disadvantages are as follows:

The method that calls the component through this is not supported and needs to be called through that.

Because the activity in the APK is not registered, it is not supported to implicitly invoke the activity inside the APK.

In the process of writing and rebuilding plug-ins, it is necessary to consider compatibility issues more, and it will be more time-consuming and laborious to tune up.

Droidplugin (August 2015)

Droidplugin is a 360 mobile phone helper implementation of a plug-in framework, it can directly run a third-party standalone APK file, no need to modify or install the APK. A new plug-in mechanism, a free-to-install operating mechanism, is a sandbox (but not a complete sandbox). Is the basis of modularity for users who do not know what he will do with the APK.

Implementation principle:

Shared process: Provides Android with a process to run multiple apk mechanisms, through the API spoofing mechanism to deceive the system.

Occupy pit: Through the way of pre-occupying pits to realize service management without registering in manifest, through many ways.

Hook mechanism: Dynamic proxy implementation function hook, Binder Agent bypass part of the system service restrictions, IO redirection (first get the original object–> Read, and then dynamic agent Hook Object –> Write back, to achieve the purpose of cheat).

Program architecture for Plug-in Host:

640?wx_fmt=png

The advantages are as follows:

Supports Android four components, and the components in the plugin do not need to be registered in the host APK.

Support Android 2.3 and above system, support all system API.

Between plug-ins and plugins, the plug-in is completely separated from the code and resources of the host.

Implementation of process management, plug-in empty process will be timely recovery, low memory consumption.

Disadvantages are as follows:

Plugin APK does not support custom resource Notification, notification bar restrictions.

The four components with special Intentfilter cannot be registered in the plugin APK.

Lack of Hook operation on Native layer, for some plug-in APK with Native code is unfriendly and may not function properly.

Because plug-ins and plugins are completely isolated from the code between the plug-in and the host, plug-ins and plug-ins communicate with the host only through the Android system-level communication.

Security concerns (can be modified, hook some important information).

Model adaptation (not available on all machines, because a large number of reflection-related, if the ROM vendor depth customization of the framework layer, reflection of the method or class is not, easy to use the plug-in failure)

Small (end of 2015)

Small is a lightweight, cross-platform plug-in framework, based on the concept of "light weight, transparent, minimal, cross-platform", the following three points are implemented.

Dynamic load classes: We know that many plugins have a dexpathlist list from the Dexclassloader class, support the dex/jar/zip/apk file format, but do not support the. So file format, so the Small framework wraps the. So file into a zip file format, inserting into the Dexpathlist collection, overwriting dynamically loaded code.

Resource segmentation: Because the Android resource format is 0xPPTTNNNN, PP is the package ID, 00-02 belongs to the system, 7f belongs to the application, 03-7e is retained, can make a fuss in this range, TT is Type such as, attr, layout, str ing and so on, NNNN is the resource global ID. The framework then repackaged the resource bundles, each of which re-assigns the resource ID, which guarantees that the host and plug-in's resources do not conflict.

Dynamic Agent registration: In Android to use the four components, all need to register in the manifest list, so that can be used, then how to use the non-registration situation can be used, here is the use of dynamic agent mechanism to Hook, before sending AMS with the pit components to deceive the system, through the certification , and then restore the components that are actually being called back to the cheat purpose.

Android Plugin thinking lucky airship Platform Rental latest full summary

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.