Android Application plug-in development solution and android Solution

Source: Internet
Author: User

Android Application plug-in development solution [go], android Solution
I. Realistic Requirement Description

Generally, after an Android Application is developed to a certain stage, more and more functional modules will be available, and the APK installation package will become larger and larger, during use, you cannot selectively load the required functional modules. In this case, you may need to consider how to split the entire application.

Ii. solution proposal

Generally, there are two methods. One is to split an application into multiple applications based on its functions. Users can download either of them or both of them. Applications can be associated at the code level to share part of the information. Another method is similar to other Platform Plug-ins. You can choose to download the required plug-ins in the main application. If you do not need this function, you do not need to download the plug-ins.

The first method is to develop multiple applications. The second method is a little complicated and requires a lot of extra work. Here we will briefly discuss the general implementation method of the second method.

Iii. Implementation Method Overview

Someone may think, can someone download a dll file or jar package like other platforms to automatically identify and load this function? Unfortunately, on the Android platform, the jar package cannot be directly loaded dynamically. The author did not think of a similar method. Therefore, to implement this function, you still need to load it in the form of an independent APK. Different from the first method, from the design perspective, a specific plug-in does not have an entry for independent operation, and desktop icons are not allowed to exist. It must be opened from the main application, after the plug-in is disabled, return to the main application (since the plug-in is APK, how does it have no entry after it is installed? Why is there no desktop icon? QQ games such as parkour are not plug-ins? Is there an icon portal ?). From the user's perspective, you can load and use the required functions in the application, which is similar to other Platform Plug-ins.

In order to implement this method, from the design point of view, we need to consider which functions are provided to users as independent plug-ins. We will not detail them here. The following describes what needs to be done from the development perspective.

Ø framework functions to be developed in the main application:

Identify whether a specific plug-in has been installed (based on the package name of the plug-in)

If the version has been installed, determine whether the upgrade is required (the server obtains the latest version and compares it with the local version)

Download and install (or upgrade) the plug-in

Uninstall this plug-in

Precautions for plug-in APK development:

Do not provide the Start entry in the Manifest File

Notification on interaction between the main application and plug-in:

It is best to use the same android: sharedUserId. The plug-in can easily obtain the resources and databases of the primary application.

The main application can start a specific plug-in Intent mode, and bring Map type parameters or json string parameters to parse specific parameters in the plug-in APK to implement business logic.

Iii. Other Instructions

The content of this blog does not specifically describe how to implement it, and there is no code-level description. However, through the method described above, developers can basically understand how to implement plug-in Android applications. Specifically, developers may encounter many problems during the development process and need to explore and improve them themselves.

 

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ///

The framework has been released:

Android-application-plug-ins-frame-work android application plug-in development Framework-AAP Framework

 

 

 

 

In android project development, there will be real conflicts between the later feature expansion enhancement and the main program code change, that is, the flexibility of the program.

The linux platform's security mechanism, coupled with the special dalvik mechanism, and various permission barriers make it difficult to develop a flexible and variable program, unlike the pc platform.

Elipse plug-ins and 360 plug-ins. In android, it is difficult for us to write a main program at first, and then use plug-ins to cope with future function expansion, as a result, the program becomes less flexible and variable.

For example, for a security software in android, the new version adds a function, such as text message interception, which often recompiles an apk package due to the addition of a module, even if you only add 50 kb of function code, you also need to upgrade a complete apk, which is usually 5 ~ 6 MB.

 

I recently thought about it and thought of a method. Since tencent qq can change the skin in the form of apk in android, it is easy to expand the resource file, why cannot I expand the functionality?

Come up with two solutions.

Let's talk about the first one.

 

The demo is downloaded at the end

 

Let's talk about the analysis.

In android, by default, each apk is independent of each other. Basically, each application is a dalvik virtual machine with a uid, Which is used together with the linux permission mechanism, this makes it difficult to directly connect the apk. However, as an Independent Application Integration, no matter how many apk files can be combined into a single dalvik virtual machine, which is intuitively reflected to developers as listing processes under shell, after these apk files are loaded at the same time, a process will exist.

This is mainly configured in the Mainfest file of the project. You only need one sentence. Take my test demo as an example:

... <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "org. igeek. plugintest. main" <! -- This is the key code --> android: sharedUserId = "org. igeek. plugintest" android: versionCode = "1" android: versionName = "1.0"> .....


In the above Code, android: sharedUserId refers to the project that shares a uid, that is, all projects with the same property share the same uid. In this way, the permission barriers are eliminated, dalvik will also be integrated into one. You can test it and write a few projects. If this attribute is not available and it is run at the same time, the current process will be listed and intuitively described.

 

The plug-in of program expansion, of course, requires a main program, the main program is to implement basic functions, as well as the UI, as well as plug-in retrieval and plug-in calls.

The main activity code in my demo is posted here:

1 public class AndoirdpluginActivity extends ActivityGroup implements OnClickListener, OnScrollCompleteListener {2 private LinearLayout llMainLayout; 3 4 // workspace. Check the luncher source code. This is the desktop multi-screen Implementation of 5 private WorkSpace wkMain; 6 private Button btnFindPlugins; 7 private CheckBox chbAttachMain; 8 9 private LocalActivityManager m_ActivityManager; 10 11 // a collection of this bean, it is equivalent to the description set of the plug-in 12 // Each bean is also a description of the plug-in 13 priv Ate List <PluginBean> plugins; 14 15 @ Override 16 public void onCreate (Bundle savedInstanceState) {17 super. onCreate (savedInstanceState); 18 setContentView (R. layout. main); 19 20 llMainLayout = (LinearLayout) findViewById (R. id. main_llMainLayout); 21 wkMain = (WorkSpace) findViewById (R. id. main_wkMain); 22 btnFindPlugins = (Button) findViewById (R. id. main_btnFindPlugins); 23 chbAttachMain = (CheckBox) findVie WById (R. id. main_chbAttachMain); 24 25 m_ActivityManager = getLocalActivityManager (); 26 27 wkMain. setOnScrollCompleteLinstenner (this); 28 btnFindPlugins. setOnClickListener (this); 29} 30 31 @ Override 32 public void onClick (View v) {33 attachPlugin (findPlugins (); 34 btnFindPlugins. setVisibility (View. GONE); 35} 36 37/** 38 * load plug-in list 39 * @ param plugins 40 */41 private void attachPlugin (final Lis T <PluginBean> plugins) {42 Log. e ("ydt", ""); 43 Log. e ("ydt", "----- List ins"); 44 this. plugins = plugins; 45 for (final PluginBean plugin: plugins) {46 Button btn = new Button (this); 47 btn. setTextColor (Color. RED); 48 btn. setText (plugin. getLabel (); 49 50 llMainLayout. addView (btn); 51 // Add event 52 btn. setOnClickListener (new OnClickListener () {53 54 @ Override 55 public void onClick (View v) {56 boolean isAtt Ack = chbAttachMain. isChecked (); 57 58 Intent it = new Intent (); 59 it. setAction (plugin. getPakageName (); 60 61 // whether to append it to view 62 if (isAttack) {63 // here, this is the 64 for (PluginBean plugin: plugins) {65 66 Intent itt = new Intent (); 67 itt. setAction (plugin. getPakageName (); 68 ViewGroup view = (ViewGroup) (m_ActivityManager.startActivity ("", itt )). getDecorView (); 69 wkMain. addView (view); 70} 71/ /After the one-time attaching is completed, delete all the buttons and look clean. These are not the focus 72 llMainLayout. removeAllViews (); 73 chbAttachMain. setVisibility (View. GONE); 74 wkMain. setToScreen (0); 75} else {76 // here, the plug-in form will not be appended to the main program, purely useless demonstration 77 startActivity (it ); 78} 79} 80}); 81 82} 83} 84 85/** 86 * Find plug-in 87 * @ return 88 */89 private List <PluginBean> findPlugins () {90 91 List <PluginBean> plugins = new ArrayList <PluginBean> (); 92 93 94 // retrieve the package name to obtain the 95 ins 95 Pac KageManager pm = getPackageManager (); 96 97 98 List <PackageInfo> pkgs = pm. getInstalledPackages (PackageManager. GET_UNINSTALLED_PACKAGES); 99 for (PackageInfo pkg: pkgs) {100 // package name 101 String packageName = pkg. packageName; 102 String sharedUserId = pkg. sharedUserId; 103 104 // sharedUserId is agreed upon during development, so as to determine whether it is 105 if (! "Org. igeek. plugintest ". equals (sharedUserId) | "org. igeek. plugintest. main ". equals (packageName) 106 continue; 107 108 // process name 109 String prcessName = pkg. applicationInfo. processName; 110 111 // label, that is, the appName is 112 String label = pm. getApplicationLabel (pkg. applicationInfo ). toString (); 113 114 PluginBean plug = new PluginBean (); 115 plug. setLabel (label); 116 plug. setPakageName (packageName); 117 118 plugins. add (plug); 119} 120 121 122 return plugins; 123 124} 125 126 127/** 128 * WorkSpace scroll to that screen, this event is triggered 129 *, and every screen in worksapce is a plug-in 130 *. This event is used to list the applications provided by the current screen plug-in, and the user is allowed to call 131 */132 @ overridemo-public void onScrollComplete (final ScrollEvent e) {134 try {135 final Context context = createPackageContext (plugins. get (e. curScreen ). getPakageName (), Context. CONTEXT_INCLUDE_CODE | Context. CONTEXT_IGNORE_SECURITY); 136 llMainLayout. removeAllViews (); 137 // the description of the current plug-in is obtained through reflection, just like the xml of most frameworks, here we simulate IOC control reversal 138 Class clazz = context. getClassLoader (). loadClass (plugins. get (e. curScreen ). getPakageName () + ". pluginApplication "); 139 Object o = clazz. newInstance (); 140 Map <String, List <String> r = (Map <String, List <String>) clazz. getMethod ("getDesciption "). invoke (o); 141 List <String> classes = r. get ("classes"); 142 List <String> methods = r. get ("methods"); 143 144 145 // here, based on the functions provided by the obtained plug-in, several buttons are generated for us to call 146 for (final String clas: classes) {147 for (final String method: methods) {148 Button btn = new Button (this); 149 150 btn. setText (clas + "->" + method + ""); 151 152 153 // click it to execute the method 154 btn provided by the plug-in. setOnClickListener (new OnClickListener () {155 156 @ Override157 public void onClick (View v) {158 try {159 Class c = context. getClassLoader (). loadClass (plugins. get (e. curScreen ). getPakageName () + ". "+ clas); 160 Object o1 = c. newInstance (); 161 162 // note that context is actually a handle. If the form is involved, the handle of the plugin is actually not good, because it does not have a 163 // attached form 164 165 // This context is plugin, through testing, dialog does not work, Toast is OK, because 166 // Toast is attached to the 167 // c. getMethod (method, Context. class ). invoke (o1, context); 168 169 // here the handle of the main program is passed 170 c. getMethod (method, Context. class ). invoke (o1, AndoirdpluginActivity. this); 171 172} catch (Exception e) {173 // TODO Auto-generated catch block174 e. printStackTrace (); 175} 176} 177}); 178 llMainLayout. addView (btn); 179} 180} 181 182 183} catch (Exception e1) {184 // TODO Auto-generated catch block185 e1.printStackTrace (); 186} 187 188} 189}


Let's take a look at the notes. There are two main points:

Agent Scanning

In this solution, each plug-in is released with a separate apk, so that you can flexibly know whether there is a new plug-in the program, prompting the user to download and install it, the apk list of the plug-in is described as non-Luncher for Action and Default for Category.

The main program listens on the apk of packgeManager after the installation is completed, and then scans the apk with the same package name (the plug-in must be defined as needed, as long as packgeManager can determine whether it is its own plug-in, and then columns them to allow the user to choose whether to load them.

 

Plug-in loading and calling

After obtaining the package, you can call the system api to obtain the context of the apk with the same sharedUserId as the main program, that is, the handle, and obtain the classloader through this context, then it's easy. How do I know the functions provided by this plug-in?

This can be described in xml. For example, this xml is a resource of the plug-in apk, just like the spring framework. Xml describes the classes and methods provided by this plug-in, the parameters required for these methods, and the types returned. In my demo, interfaces are used for convenience. Each plug-in has a class that provides the same method to obtain a map set and obtain the description of this plug-in.

 

OK. Now you will know what functions the loaded plug-in provides.

 

In the Code posted above, each plug-in is traversed cyclically, and the functions provided by each plug-in are displayed to the user as a Button. click the Button to execute the plug-in function, during execution, it is not the activity redirection (this is meaningless), but the execution in the context handle of the main program itself, that is, the execution in its own form.

There is a comment in the code, indicating that if the plug-in is useful to the context, remember to pass in the context of the main program so that the form can be appended to this handle, if the context of the plug-in is passed and there is no form instance, some forms cannot be appended without any effect.

 

Here we only provide ideas. If you have time to study it, you can see if you can build a general framework. There is another way, which will be written later without the apk format.

There are some issues to be verified, such as plug-in permissions. If some permissions required by the plug-in are not declared in the main program, what is the situation? Can I apply in real time? This requires expert advice. Or you can declare the declared permissions in advance in the main program. There is also the native-Layer Code problem. If the plug-in contains the native-layer code, what will happen? This also requires verification.

 

Here is the demo download:

Http://files.cnblogs.com/hangxin1940/android_plugin_program.rar

 

Original, reprinted please indicate http://hangxin1940.cnblogs.com


When installing the android development environment and importing the adt plug-in, why does the import fail and sometimes wait for a long time? How can this problem be solved?

In this case, you need to disconnect the network, that is, when you select the ADT plug-in to start installation, you need to remove the Contact all update sites during install to find required software at the bottom of the left, we will not allow him to update and fix the plug-in during installation. This is a problem that occurs during my own installation and is solved in this way. I hope you will adopt my answer.
Reference: crazy Android Development

How does android use the supermap plug-in to develop maps?

2 books recommended
SuperMap GIS Application and Development tutorial

SuperMap plugin pro. NET 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.