Plugins architecture for an Android app?

13,992

Solution 1

I have done a framework that works like Robo-guice with some of its primary IoC functions. (Cut down on boilerplate codes that load views/service etc...)

But the core of which, I believe is the solution to your problem, is the ability to load separate APK "plugin" files, that includes "res" as well as <layouts>.xml files. The <layouts>.xml file can be independently inflated by the classes within that APK. That is, you can load a CustomView (that inflates its own <layout>.xml) into an originating/parent App. All this is done without the Parent App APK knowing how the UI was inflated in the plugin APK.

Example of what I mean: I have a Mapping App, the framework will dynamically scan installed APK that matches the "contract" for a "add-on function" plugin, and loads the UI specific to it onto the App's View as a floating panel.

I would say a plugin framework for Android is do-able, as Android has most if not all of the necessary built in APIs to accomplish this.

These are your friends in this area of plugin development for Android:

  1. PackageManager (Scan install packages, aka Plugins)
  2. DexClassLoader (ClassNotFoundException will be a pain if you don't use the correct ClassLoader btw)
  3. Java Reflection

Solution 2

Where can I find a good example of a plugin architecture?

Roman Nurik from Google has implemented a nice plugins framework in his open source app dash clock. The plugins themselves are Services that extend the DashClockExtension class in the API and are installed as completely independent APK files with their own resources. It's quite a lot of work defining the communication protocol via the AIDL, but it's nice and clean and works very well.

one of the plugins must add a button on a particular screen, and clicking this button increments a value in the app's database.

The parts of the main Layout which can be modified by the plugin will need to be pre-defined by the core app, and exposed via the communication protocol. It should be possible for the plugin to inflate an arbitrary layout, and send it to the main app, which could put that inside a pre-allocated area of it's own Layout.

Solution 3

If you are just going for an increase in modularity, I would recommend using a dependency injection container such as PicoContainer, Guice or Spring.

If you want a light-weight plug-in architecture, then go for Java Plugin Framework (JPF).

It allows you to define extension points, which can be implemented by your modules. The primary job of the plug-in framework is to provide a way that you can bundle these modules (as jars), which are dynamically found by the core application and given as implementations of the extension point.

Share:
13,992
Nicolas Raoul
Author by

Nicolas Raoul

I am Nicolas Raoul, IT consultant in Tokyo. Feel free to copy/paste the source code from my StackExchange answers, I release it to the public domain.

Updated on June 15, 2022

Comments

  • Nicolas Raoul
    Nicolas Raoul about 2 years

    THIS QUESTION HAS MOVED TO https://softwarerecs.stackexchange.com/questions/27841/plugins-architecture-for-an-android-app


    I want to implement a plugin system for an Open Source app, because it has become really large, with many features that only a few users need. Releasing different apps is not a good solution, because userA wants feature7 and feature24 whilst userB wants feature39 and feature24.

    Where can I find a good example of a plugin architecture?

    Here is what I would like a plugin to be able to do:

    For instance, one of the plugins must add a button on a particular screen, and clicking this button increments a value in the app's database. This is not doable with Content Providers and Intents, as far as I know.

    I want to avoid making the core app's code complex with tons of hooks everywhere.

    The form of the plugin could be a file on the SD card, an app, or anything else.

  • Nicolas Raoul
    Nicolas Raoul about 12 years
    I added links, you might want to check the URLs indeed point to what you were referring to.
  • Francisco Corrales Morales
    Francisco Corrales Morales about 10 years
    But technically, one user could give a .jar that he downloaded to another user !, what if we don't want this to occur? It's there a way to just insert new code into the app ?
  • Nicolas Raoul
    Nicolas Raoul almost 10 years
    @FranciscoCorralesMorales: As the original asker of the question, I don't mind users sharing plugins.
  • Julian Suarez
    Julian Suarez over 9 years
    Since this is a pretty common problem, and a hard one, would you mind sharing some snippets of code on how you implemented such solution?
  • Saswat Anand
    Saswat Anand over 9 years
    @jucas Did you find any framework or code-snippet that implements this approach?
  • Nicolas Raoul
    Nicolas Raoul almost 9 years
    Great! I see all hooks have to be defined in advance, right? Or can the example in the 4th paragraph of the question be done easily via a plugin without having to modify the core app?
  • Tim Rae
    Tim Rae almost 9 years
    I updated the answer a bit... I'm not too sure how the callback would need to be implemented in that scenario without spending some time trying it myself, but I guess the onClick property of the button could be set to run a hook with a known name in the main app, or an onClickListener could be programmatically set by the plugin which performs some operation.