Android uses dynamic loading framework DL for plug-in development

Source: Internet
Author: User

if reproduced, please declare the source: Time of the Sand: http://blog.csdn.net/t12x3456 (from the sands of the CSDN blog)

Overview:

With the continuous iteration of the application, the volume of the application is increasing, the project becomes more and more bloated, and the redundancy increases. The addition of new features of the project, unable to determine the match with the user, the occurrence of serious anomalies often reaching, only emergency release of the patch version, Force the user to update. Results are frequently updated, but easy to reduce user stickiness. Or the continuous development of the company's business, the use of the same system more and more, the traditional way through the largest number of users of the main project to download and install.

What do you do? Reference browser-plugin development mode:

One. To be able to split their own applications, some features can be implemented in the plug-in, use the time to download, and do not install. If there is a new feature to add, do not need to update the application, as long as the reservation plug-in management, we can add plug-ins to dynamically update their own applications, this feature needs to be improved or extended, update plug-ins, do not need to install or uninstall frequently (users are prone to dislike).

Two. Corresponding to the same system application, the normal drainage method can only guide users to download and install new applications, if the use of plug-in development, there is no need to install the application, close the plug-in function is also very convenient, eliminating the application installation and uninstallation process, can achieve seamless drainage.

Here to recommend an open source dynamic loading framework DL, the project initiated by Singwhatiwanna, currently a total of three people to develop, I was fortunate to become one of the contributor.

If you are unfamiliar with the DL dynamic loading framework, it is recommended to take a look at this article first:

APK Dynamic loading framework DL parsing http://blog.csdn.net/singwhatiwanna/article/details/39937639

If you have seen it, it will not be clear, please take a look at the DL plugin framework of the Panorama, as follows


Here I mainly introduce you to the use of the DL framework for the development of the specific steps:

1. First we need to get the project code from GitHub:

https://github.com/singwhatiwanna/dynamic-load-apk

Here we can see the downloaded directory as follows


The Lib directory is our public plug-in library.

Sample directory is the corresponding demo, the specific project can refer to the above DL panorama in the three modes:

Because of the relative independence of the code management and development teams in general projects, it is difficult for the general plug-in engineering team to reach the code of the main project team. Therefore, this is the first and most recommended way to do this, using plugins that do not rely on hosting for development. There is no need for two teams to have too much interaction, and the development efficiency is relatively high.


2 Import the Lib project as follows:



You can see the Dl-lib.jar in the bin directory, if we need to modify the Lib project, rebuild to get the corresponding Dl-lib.jar can be


3. Plug-in project development, import Main-plugin project in Demo


The first step is to emphasize the plug-in development considerations to avoid unnecessary errors

Plug-ins also need to reference the DL jar package, but not into the Libs directory of the plug-in project, in other words, the plug-in is compiled to rely on the jar package but when packaged as an apk, do not put the jar package in, this is because, Dl-lib.jar already exists in the host project, if the plugin also has this jar package, there will be a class link error, the reason is very simple, there are two copies of the same class in memory, repeated. As for SUPPORT-V4, the same is true. For eclipse it's easy to just create a directory in the plug-in project, such as External-jars, then put Dl-lib.jar and Support-v4.jar in, and add the following two sentences to the. classpath:

<classpathentry kind= "Lib" path= "External-jars/dl-lib.jar"/><classpathentry kind= "Lib" path= " External-jars/android-support-v4.jar "/>
And then the specific steps in plug-in development

(1) If the original activity, this need to change to inherit dlbasepluginactivity, if the original is fragmentactivity, then need to inherit dlbasepluginfragmentactivity, for Example

Testfragmentactivity extends Dlbasepluginfragmentactivity

(2) If you need plug-in standalone installation operation, as long as the jar is placed under the Libs can, if support dynamic loading, still need to add exteral-jars in accordance with the above considerations

(3) The required permissions of the plugin need to be declared in the host project

If it is the actual development, generally to get plug-ins from the server, here we are convenient to debug the demo, will run to generate the corresponding plug-in apk, put into the Dynamicloadhost directory on the SD card


4. Add the above generated Dl-lib.jar to Libs in the host project as shown below for the host project provided in the demo


In the host project, first we need to get the plugin to call the plug-in apk corresponding to the mainactivity,dl of the plugin path to the SD card on the Dynamicloadhost directory, no need to create, or according to their own needs to modify.


        String Pluginfolder = environment.getexternalstoragedirectory () + "/dynamicloadhost";        File File = new file (pluginfolder);        file[] plugins = File.listfiles ();        if (plugins = = NULL | | plugins.length = = 0) {            mnoplugintextview.setvisibility (view.visible);            return;        }        for (File plugin:plugins) {            Pluginitem item = new Pluginitem ();            Item.pluginpath = Plugin.getabsolutepath ();            Item.packageinfo = Dlutils.getpackageinfo (this, item.pluginpath);            if (item.packageInfo.activities! = null && item.packageInfo.activities.length > 0) {                Item.launcheractivityname = Item.packageinfo.activities[0].name;            }            Mpluginitems.add (item);        }

Next is the apk that responds to the response, and you need to use Dl-lib.jar:

(1) Get the Mainactivity class object in the plugin apk that we need to call by Class.forName

(2) As mentioned above, we need to judge whether the object inherits from Dlproxactivity or dlproxfragmentactivity, and gets the corresponding proxy class object.

(3) Use the corresponding proxy class object to adjust the plugin apk

 Pluginitem item = mpluginitems.get (position);        Class<?> proxycls = null; try {class<?> cls = Class.forName (Item.launcheractivityname, False, Dlclassloader.get            ClassLoader (Item.pluginpath, Getapplicationcontext (), getClassLoader ()));            if (Cls.assubclass (dlbasepluginactivity.class) = null) {proxycls = Dlproxyactivity.class;            }} catch (ClassNotFoundException e) {e.printstacktrace ();                    Toast.maketext (this, "Load plugin apk failed, load class" + Item.launcheractivityname + "failed.",        Toast.length_short). Show ();                } catch (ClassCastException e) {//ignored} finally {if (proxycls = = null) {            Proxycls = Dlproxyfragmentactivity.class;            } Intent Intent = new Intent (this, proxycls); Intent.putextra (Dlconstants.extra_dex_path, MpluginitemS.get (position). Pluginpath);        StartActivity (Intent); }

Finally run the host project Main-host, you can see the final effect:




I believe that everyone through the above steps, the DL dynamic loading framework for the development of a certain understanding. The DL framework is still expanding, and we welcome the star,fork of our projects, or make valuable suggestions. If you have any questions, please give us your feedback and we will fix or improve them in the subsequent release.


Android uses dynamic loading framework DL for plug-in development

Related Article

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.