Development of Android plug-ins-basics and android Basics

Source: Internet
Author: User

Development of Android plug-ins-basics and android Basics
Development of Android plug-ins

Android plug-ins have been discussed for a long time, but there is no very reliable and mature plug-in framework on the market for our use. Here we will try to compare the process of using plug-ins in Java, and how to port the process to the Android framework for use. A lot of code is internal information, so don't like it, thank you ~

Why plug-ins?

  • More and more features
  • Code and installation packages are getting bigger and bigger
  • Small Updates also need to be released
  • Frequent Updates and high installation cost
  • Users cannot selectively load required modules
  • ......

Benefits of plug-in

  • The main installation package is small.
  • Forced modularization to reduce Coupling
  • Reduce the number of overall updates
  • The plug-in can be updated silently separately.
  • You can select
  • ......

Plug-in requirements

  • No entry for independent operation
  • Master application control, download, install, delete, silent upgrade, open, and close
  • Primary Application and plug-in Resource Sharing

Composition of the installation package

Plug-ins to be installed

Compared with the composition of an installation package, there are many things to deal:

  • The main application can start a specific plug-in Intent mode, and bring Map type parameters or json string Parameters
  • Share Resource data using the same android: sharedUserId
  • Search for plug-ins Based on sharedUserId
  • QueryIntentActivities: finds all the activities (or other) that match the action, that is, the plug-in.
  • The query method can obtain the plug-in path and the class name of the implemented interface class.
  • You can retrieve the sharedUserId to obtain the path but cannot obtain the class name.
  • Generally, you can use a description file (xml or json) to describe the plug-in structure.
  • CreatePackageContext ()
  • GetResourcesForApplication ()

Dynamic Loading of common classes
-Can I use ClassLoader to dynamically load Class files in. jar in Java? Can I use android?
-PathClassLoader
-DexClassLoader

Interface Class

    package com.plug;    public interface InterfacePlug {        public String function_01();        public int function_02(int a,int b);    }

Implementation class

import com.plug.InterfacePlug;    public class PlugImpl  implements InterfacePlug{        @Override        public String function_01() {            return null;        }        @Override        public int function_02(int a, int b) {            return a+b;        }    }

Main Application

package com.host;    public class MyhostActivity extends Activity {        public void useDexClassLoader() {            DexClassLoader cDexClassLoader =                    new DexClassLoader("/mnt/sdcard/Myplugdex.jar", "/data/data/com.host", null, this.getClass()                            .getClassLoader());            try {                Class<?> class1 = cDexClassLoader.loadClass("com.plug.PlugImpl");                InterfacePlug interfacePlug = (InterfacePlug) class1.newInstance();                int ret = interfacePlug.function_02(12, 13);                tv.setText(" return value :" + ret);            } catch (Exception e) {            }        }    }

Only the implementation class can be exported when the plug-in package is exported; otherwise

Classref in pre-verified class resolvedtounexpected implementation duplicate definition Error

Of course, the installation package parsing and plug-in installation are far from loading class files. We also need:

  • What should I do if it is not a common class?
  • How do system components receive callbacks?
  • How to load resources?
  • How to handle PackageInfo?
  • Processing of Resources?
  • Assets processing?

Dynamic Loading of system components and resources

PluginLib

PluginHost

PluginTest

A Simple plug-in framework is shown in:

The main application lists the installed Plugins, and click Start corresponding PluginActivity

Of course, we can also use this simple mechanism to complete a complete plug-in system.

  • You can basically implement an Activity

  • You can add plug-ins for other components to complete more functions (Service, explorer, ContentProvider, Application ?)

Disadvantages of this plug-in mechanism

  • The information registered in Manifest is controlled by the system. Therefore, the permission information required by the plug-in must be pre-registered in the main program.
  • The current implementation has certain restrictions, such as mutual calls between plug-ins and mutual calls between plug-ins.

Github plug-in framework, an open-source plug-in framework based on Fragment

  • Https://github.com/mmin18/AndroidDynamicLoader
  • Fragment-based UI
  • Reflection to get Resources
  • Page jump through uri

/*
* @ Author zhoushengtao (Zhou Shengtao)
* @ Since 14:02:22, January 1, January 27, 2015
* @ Weixin stchou_zst
* @ Blog http://blog.csdn.net/yzzst
* @ Exchange and learning QQ group: 341989536
* @ Private QQ: 445914891
/

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.