This article describes the plug-in architecture based on the class loader in the analysis of the Android kernel of "Ke.
Plug-in concept:
1. The plug-in cannot run independently, but must run in a host program, that is, the host program calls the plug-in program.
2. Plug-ins can be installed independently.
3. Different plug-ins can be managed in the Host Program, including viewing the number of plug-ins, disabling or using a plug-in.
4. The host program should ensure the downward compatibility of the plug-in, that is, the new version of the Host Program can run older versions of the plug-in.
The following describes the architecture in detail:
1. Host Program:
Create an Android PluginDevAndroid Project
2. Plug-in Project 1: Plugin1
3. Plug-in Project 2:
4. Summary:
4.1 interface classes are generally defined in the host project, for example, IPluginDev. java in this example
4.2 When IPluginDev needs to be applied to a plug-in project, an external jar package must be used, and the jar package is added to the build Path of the plug-in project in the form of Library, instead of adding an external jar.
4.3 if the host program wants to know which plug-ins are in the system, it can define an Action. In this example, the following action is used.
In this way, the host program can query the list of related plug-ins through the queryIntentActivities function of the PackageManager class.
ThePlug-insYou can access the resources of the plug-in. For example:
Resources res = pm.getResourcesForApplication(packageName);int id = 0;id = res.getIdentifier(version, string, packageName);String version = res.getString(id);Log.d(hlwang, MainActivity test version is:+version);
In this Code, first obtain the Resource object of the plug-in, then obtain the string id value named version field, and then call getString to obtain the value of this variable, the Host Program then knows the resources in the plug-in program.
Key code:
private void test(){Intent intent = new Intent(com.example.plugindevandroid.plugin,null);final PackageManager pm = getPackageManager();final List
plugins = pm.queryIntentActivities(intent, 0);for(ResolveInfo r:plugins){ActivityInfo activityInfo = r.activityInfo;String div = System.getProperty(path.separator);String packageName = activityInfo.packageName;String packageName0 = getPackageName();String dexPath = activityInfo.applicationInfo.sourceDir;String dexOutputDir1 = activityInfo.applicationInfo.dataDir;String dexOutputDir2 = getApplicationInfo().dataDir;String libPath = activityInfo.applicationInfo.nativeLibraryDir;Log.d(hlwang, MainActivity test div is:+div+,packageName is:+packageName+,packageName0 is:+packageName0+,dexPath is:+dexPath+,dexOutputDir1 is:+dexOutputDir1+,dexOutputDir2 is:+dexOutputDir2+,libPath is:+libPath);DexClassLoader dexCl = new DexClassLoader(dexPath, dexOutputDir2, libPath, getClassLoader());Log.d(hlwang, MainActivity test clazzName is:+packageName+.PluginVersion);try{Class clazz = dexCl.loadClass(packageName+.PluginVersion);IPluginDev plugin = (IPluginDev) clazz.newInstance();String name = plugin.getName();Log.d(hlwang, MainActivity test name is:+name);Resources res = pm.getResourcesForApplication(packageName);int id = 0;id = res.getIdentifier(version, string, packageName);String version = res.getString(id);Log.d(hlwang, MainActivity test version is:+version);PluginObject p = new PluginObject();p.name = name;p.version = version;Log.d(hlwang, MainActivity test p is:+p);mList.add(p);}catch(Exception e){Log.d(hlwang, MainActivity exception eeeeeeeeeeeee);e.printStackTrace();}}Log.d(hlwang, MainActivity test list size is:+mList.size());setListAdapter(new ArrayAdapter
(this, android.R.layout.simple_list_item_1, mList));}
In this Code, first obtain the List of plug-ins List.
Then obtain the packageName of the plug-in and the dexPath directory of the plug-in.
Again, get the dexOutputDir directory.
LibPath is generally only a library file used by c/c ++.
The parameter meaning of DexClassLoader:
DexPath: Path of the plug-in apk or jar package file
DexOutputDir: directory where files extracted from the target apk or jar package are stored. Because the Host Program only has access to the directory where the application is located.
Run: