Turn-the Android implementation process based on plugin development

Source: Internet
Author: User

Original address: http://www.2cto.com/kf/201403/284689.html

This article describes the "plug-in" architecture based on the class loader in the Android kernel Anatomy of "New Year's Day".

Plug-in concept:

1, plug-ins can not run independently, but must run in a host program, that is, the host program to call the plug-in program.

2, plug-ins can generally be installed alone.

3. The host program can manage different plugins, including viewing the number of plugins, disabling or using a plugin.

4, the host program should ensure that the plug-in compatibility, that the new version of the host program can run older version of the plugin.

Here is a detailed look at this architecture:

1. Host Program:

New Android Project Plugindevandroid

2. Plug-in Project 1:plugin1

3, plug-in Project 2:

4. Summary:

4.1, the interface class is generally defined in the host project, such as the Iplugindev.java in this example

4.2, plug-in projects need to apply Iplugindev, you must pass an external jar package, and the jar package is added to the plugin project's build Path in the form of a library, instead of being added as an "external" jar.

4.3, the host program wants to know which plug-ins in the system, you can define an action, in this case, the following action is used.

In this case, the host program can query the list of related plug-ins through the Queryintentactivities function of the Packagemanager class.

With the plugin 's PackageName, you can access the content of the plugin's resources. For example:

12345 Resources res = pm.getResourcesForApplication(packageName);intid = 0;id = res.getIdentifier(version, string, packageName);String version = res.getString(id);Log.d(hlwang, MainActivity test version is:+version);

In this code, first get the resource object of the plug-in, then get the string ID value of the Version field, and then call GetString to get the value of the variable, so the host program knows the contents of the resource in the plug-in program.

Key code:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 private void test(){        Intent intent = new Intent(com.example.plugindevandroid.plugin,null);        final PackageManager pm = getPackageManager();        final List<resolveinfo> 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<pluginobject>(this,                android.R.layout.simple_list_item_1, mList));    }</pluginobject></resolveinfo>

In this code, you first get the list of plugins.

Then get the plugin's PackageName, as well as the plugin's Dexpath directory.

Again, get the Dexoutputdir directory.

Libpath a library file that is generally used by C + + only.

The parameter meaning of Dexclassloader:

Dexpath: The path of the plugin apk or jar package file

Dexoutputdir: The directory where the files extracted from the target apk or jar package are stored. Because the host program only has access to the directory where the application resides.


Turn-the Android implementation process based on plugin development

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.