Home » Android » android – Write an application that can use plugins

android – Write an application that can use plugins

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am trying to find a way to have plugins in an application.

My goal is to have a Core application, and offer plugins that can be downloadable on the market. (It can be anything, weather, radio player, etc…)

The plugins would not interact with each other, so the core application is more like a directory of multiple applications with kind of a SDK that the plugins use.

There is the Tic Tac Toe example in the Android doc, but the requires the main app to declare the external lib. My wish is that the core app detects the new installed plugins and shows them.

I found this other question but there is no answer.

Is there a way to do that?

Edit: There are also applications that can be unlocked by buying another app on the market. How do they work? I could not find anything interesting yet. You know what you find when you google “android unlock” 🙂

How to&Answers:

You can use PackageManager to look for another application. If you know the package names of all of the ‘plugins’ then you can just check for each of them this way.

      PackageManager pm = getPackageManager();
                    try {
                        ApplicationInfo appInfo = pm.getApplicationInfo("com.package.name.your.looking.for", 0);
                        //if we get to here then the app was found, do whatever you need to do.
                    } catch (NameNotFoundException e) {
                        //app was not found
                    }

Answer:

This is a little cleaner, so you don’t have to use a try catch block.
Also, this avoids someone creating an app with the same name and manually installing it on their phone.

public boolean checkIfAllowed()
{
    PackageManager pm = getPackageManager();
    int match = pm.checkSignatures("your.first.package", "your.second.package");
    if (match == PackageManager.SIGNATURE_MATCH)
    {
        Log.d("ALLOWED?", "signatures match");
        return true;
    }
    else
    {
        Log.d("ALLOWED?", "signatures don't match");
        return false;
    }
}

Answer:

if you want to decouple the main app from the plugins (using PackageManager.getApplicationInfo(package, int) MainApp has to know which package to search for) you can adopt this scheme:
at run time MainApp send a broadcast intent that every plugin has to listen to (by contract).
In response, every plugin send a direct intent to a component of MainApp that register information about available plugins and how to talk to them.
In this way, you don’t have to update MainApp each time a new plugin is created.