Android plug-in development-Basics
Android plug-in development has been discussed for a long time for Android plug-ins, 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 functions • more code and installation packages • smaller Updates also need to be re-released • Frequent Updates • users cannot selectively load required modules •...
Benefits of plug-in
• The main installation package is small • Forced modularization, reducing coupling degree • Reducing the number of overall updates • the plug-in can be updated silently separately • users can choose •...
Plug-in requirements
• No portal for independent operation • Master application control, download, install, delete, silent upgrade, open and close • Master application and plug-in Resource Sharing
Composition of the installation package
Plug-ins to be installed
• The main application can start a specific plug-in the Intent mode and include Map-type parameters or json string parameters. • use the same android: sharedUserId, resource Data Sharing • search for ins Based on sharedUserId • queryIntentActivities find all the activities (or others) that match this action) plug-in: • query can obtain the plug-in path and Class Name of the implementation Interface Class • retrieve the sharedUserId to obtain the path, but cannot obtain the class name • a description file (xml, json) description plug-in structure • createPackageContext () • getResourcesForApplication ()
Dynamic LoadingNormalClass
• Can Java use ClassLoader to dynamically load Class files in. jar? Can 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{@Overridepublic String function_01() {return null;}@Overridepublic 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, the Classref in pre-verified class resolvedtounexpected implementation will appear.
Duplicate definition Error
• What should I do if it is not a common class? • How do system components receive callbacks? • How to load resources? • PackageInfo? • Resources? • Assets?
Dynamic Loading of system components and resources
• PluginLib
• PluginHost
• PluginTest
• The main application lists the installed Plugins and click Start PluginActivity.
• You can basically implement an Activity. • you can add plug-ins for other components to complete more functions (Service, aggreger, ContentProvider, Application ?) • Disadvantage-the information registered in Manifest is controlled by the system. Therefore, the plug-in requires permission information to be pre-registered in the master program. -Restrictions on the current implementation