13.Android plug-in Getting Started Guide

Source: Internet
Author: User

Reprint: http://lruheng.com/2017/07/01/Android%E6%8F%92%E4%BB%B6%E5%8C%96%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/One, Android Plugin introduction 1. Meaning

The so-called plug-in, is to let our application no longer the same as the original to put all the content in an apk , you can put some features and logic to put in the plug-in apk, and then the main apk to do [on-demand], the benefit is to reduce the size of the main apk, Make the application more portable, and can be hot-swappable, more dynamic.

2. Background

3. Advantages

Second, Android plug-in base 1. Plug-in Features

1) When the application is running, some specific functions are implemented by loading some executable files that are not present locally.

2) These executables can be replaced;

3) Replace static resources (such as change the start diagram, change the theme, or use the server parameter switch control advertising hidden reality, etc.) is not dynamic loading;

4) The core idea of dynamic loading in Android is to dynamically invoke external Dex files, in extreme cases, the Android apk itself with the Dex file is just the portal of a program (or shell), all the functions are done by downloading the latest Dex files from the server;

2. Problem code load to be solved

Class loading can use the Java ClassLoader mechanism, but for Android, it is not that class loading can be used, many components are "life", so for these flesh-and-blood classes, they must be injected with vitality, so-called component life cycle management;

Resource loading

Resource loading scheme Everyone uses the same principle, is the hidden method of Assetmanager Addassetpath.

3. Plug-in prerequisite base ①classloader class Loader

To implement a hot deployment by loading an external Dex file (that is, a plug-in), it is necessary to load the class file into memory.

Two types of Classloader:dexclassloader and Pathclassloader are involved. and dexclassloader can load external files such as Jar,dex, which is exactly what we need.

About ClassLoader detailed, see ClassLoader full analysis.

②java Reflex

Because the plug-in APK and the host apk are not in an APK, some class access must be obtained through reflection. So understanding reflection is a must for plug-in learning.

For Java reflection, see the Java reflection detail.

③ plug-in resource access

Every resource in res generates a corresponding integer type in the R.java. Id,app starts by registering the R.java with the current context, and we are using these IDs to access res resources when we use them as r files, but the plug-in's R.java is not registered to the Context, the res resource of the plugin cannot be used by ID.

View the source code and regenerate a new resource object with the "Addassetpath" method to save the resources in the plugin and avoid conflicts.

For access to plug-in resources, see Using R resources in plugins.

④ Proxy Mode

Plug-in implementation of the process is mainly by Masinier, swindling to achieve. Think of the load of components such as activity, but it is only the most object to exist, and is not registered in the androidmanifest, there is no life cycle of callbacks, and does not achieve the effect we want. Therefore, whether dynamic_load_apk through agent activity to manipulate the plug-in activity, or droidplugin through the hook activity startup process to start the plugin activity, is the application of proxy mode.

For proxy mode, see static agent and dynamic agent.

At this point, through ClassLoader loading, and then through the proxy mode, the activity and other components have a life cycle to achieve true functionality, and solve the problem of resource access. Perhaps the plug-in has been able to easily implement some preliminary functions, but the plug-in never stop there. More content still needs to be explored, but the above knowledge is the foundation of the foundation, must be necessary.

Third, Android plug-in open source project


Introduction of the more important of the two, the realization of different ideas, but also the introduction of the plug-in can learn two.

dynamic-load-apk

dynamic-load-apk abbreviation DL, this open source framework author is Ningyugang, his implementation is to bury an agent activity in the host, change the ClassLoader after the activity in the loading plug-in, using the activity in the host as the agent, Callback to the plug-in activity so the corresponding life cycle. This idea and androiddynamicloader a bit like, are to do a proxy, but dynamic-load-apk loaded plug-in activity.
Project Address: https://github.com/singwhatiwanna/dynamic-load-apk

Droidplugin

Droidplugin is a set of plug-ins implemented by Zhang Yong, and its principle is to hook the client side of the system API.
Project Address: Https://github.com/DroidPluginTeam/DroidPlugin

Since the emphasis on the introduction of two projects, it is necessary to learn, how to learn?

Fortunately, there are already predecessors to share their learning experience, then we only need to combine the source to learn.

Four, dynamic-load-apk detailed

Android Plugin Learning Path (i) Dynamic loading overview
Android plug-in Learning Path (ii) ClassLoader full analysis
Android plug-in Learning Path (iii) calling code in an external. dex File
Android plug-in Learning Path (iv) using R resources in plugins
Android plug-in Learning Path (v) agent activity
Android plug-in Learning Path (vi) dynamic creation of activity
Android plug-in Learning Path (vii) DL plug-in development of the pit of attention
Android plug-in Learning Path (eight) dynamicloadapk source Analysis (on) Android plug-in Learning Path (ix) of the DYNAMICLOADAPK source analysis (the next)

Five, Droidplugin detailed

Dynamic agent of Hook mechanism
Hook mechanism of binder hook
The Ams&pms of hook mechanism
Activity Life Cycle Management
Plugin loading mechanism
Management of broadcasting
Plug-in of service
ContentProvider Plug-in

Vi. Summary

The above content is only the Android plug-in knowledge of getting started, the current awareness is still shallow.

Plug-in, hot-update technology began to develop rapidly in 2016, which is the need for business development, but also we need to follow the forefront of learning technology.

13.Android plug-in Getting Started Guide

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.