Creating Android App with plugin Architecture – Tutorial

There are times when your apps become overloaded with functionality which all users don’t need. You can create a MAIN Application and rest of the functionality can be provided by child apps as plugins. User can download the plugin with proper functionality he wants .

This way application is light and user only have functionality he needs . This architecture is seen mostly in application which provide themes. There will be main app which contain functionality and plugin themes can be added later.

We will be using AIDL interface to achieve this functionality in our application. It allows you to define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC). Core functionality will be implemented in plugins which will attach to the main application via AIDL so that main Application can utilize the functionality defined in plugin and perform the desired task.

In order to implement these functionalities, the following features of the Android framework were used.

  • Two applications will be created. One will act as main app and other will host two services which will act as plugins.
  • Each plugin is a service. Plugin exposes two of them. In order to identify our plugins, We defined a specific Intent (androidsrc.intent.action.PICK_PLUGIN) and We attached an intent filter listening to this intent in plugin1’s AndroidManifest.xml. In order to select a particular plugin, We abused the category field. One plugin can be accessed therefore by binding a service with an intent whose action is androidsrc.intent.action.PICK_PLUGIN and whose category equals to the category listed in AndroidManifest.xml of the package that exposes the plugin service.
  • Plugins are discovered using the Android PackageManager. Pluginapp asks the PackageManager to return the list of plugins that are bound to androidsrc.intent.action.PICK_PLUGIN action. Pluginapp then retrieves the category from this list and can produce an intent that is able to bind the selected plugin.
  • Pluginapp updates the plugin list dynamically by listening to ACTION_PACKAGE_ADDEDACTION_PACKAGE_REMOVED and ACTION_PACKAGE_REPLACED intents. Whenever any of these intents are detected, it refreshes the plugin list.

Lets get started :

1. Creating MainApplication

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details.

2. Now create package in src folder with name “com.example.aidl” and create a file named “IOperation.aidl” in this package. This file with same package name will be used in plugin app also. This will act as interface between app and plugin. Copy following code in this file.

3. Now add following code to activity_main.xml

4. Now create file serviceinvoker.xml and copy this code. This layout will be shown in while operating on plugin operations.

5. Now create service_row.xml and copy following code.

6. Now copy following code to ActivityMain.java

7. Now create file named “InvokeOp.java” in main package of the application. Copy following code.

8. finally update AndroidManifest.xml with this code.

2. Creating plugin app :

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details.

2. Now create package in src folder with name “com.example.aidl” and create a file named “IOperation.aidl” in this package. This file with same package name is also created in Main application. This will act as interface between app and plugin. Copy following code in this file.

3. Now create file PluginService1.java for first plugin and copy following code.

4. Now create PluginService2.java and copy following code.

5. Finally update AndroidManifest.xml for plugin.

6. Now build and run the project.

guru

Technology enthusiast. Loves to tinker with things. Always trying to create something wonderful using technology. Loves coding for Android, Raspberry pi, Arduino , Opencv and much more.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *