-1

How do I implement an API so that other developers can add custom views, themes, complete modules with new functionality and have access to objects in my app?

In my case the Api is for a drawing application. I would like to support things like:

  • Custom drawing tools, f.e. straight lines, dashed lines. Should also support editing of the start and end point of a straight line.
  • Adding a new tool like a cutter with a menu entry
  • Adding support for other file formats
  • Theming support
  • Access to all the drawings (content provider)
  • Obtaining the current list of open documents
  • Opening a document
Diemex
  • 821
  • 2
  • 12
  • 19

3 Answers3

1

Apps can broadcast Intents that can be received by other apps. These intents can contain various forms of information that you would like to pass between the two.

So you could define a set of Intent actions that the other apps can implement.

Your app can search for any others that implement these actions, and call them when required.

This allows other developers to write modules for your app, and you can control what data gets passed between your app and the other one.

The answer by j__m seems to suggest this type of approach.


Beware of giving too much control to external modules through an API like this. There are obvious security issues if you allow 3rd party apps to control your own objects.

Check out these questions for related discussion:


You may also want to look at implementing a Service to deal with these requests, depending on how this API is meant to be used.

This will allow your app to be active, yet not visible.

Your API BroadcastReceiver classes that receive the Intent broadcasts could call into your Service in order to process the commands or requests.

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
1

use PackageManager to locate apps that should be treated as extensions of yours. you can do this in various ways. For example, you can use PackageManager.queryIntentActivities() to find all apps containing activities that handle a particular intent. or, you can use PackageManager.getInstalledPackages() to list all apps on the device and then look for extensions by package name, metadata, whether they hold a permission you define, etc.

once you find your extensions, you need to decide how you're going to interact with them. the most secure mechanism is using intents to call into activities, receivers, or services provided by the other app.

based on the kinds of things you're asking for - arbitrary access to objects and views - you may prefer to use DexClassLoader to load the code of the other app directly into your process; this lets the other app do whatever it wants, including as you say create views, change appearances, etc. it also lets the other app use all of your package's permissions, so you will definitely want to let the user know that the other app has that kind of access. as i mentioned before, you can define a permission like "act as a plugin for my app" and require the other app to list that permission in order to be loaded.

j__m
  • 9,392
  • 1
  • 32
  • 56
0

If it's just java files then you can package it as a jar file for distribution.

If you have xml files that contain layouts, dimens and other resources then it's best to create an android project in eclipse or just a simple eclipse java project and label it Is Library from Project Properties. You can host these files on github.

prometheuspk
  • 3,754
  • 11
  • 43
  • 58