12

I have a library, say LIB, which exposes the quite a few APIs and classes for use by application developers.

If there are more than one applications that use LIB on the phone, I want only one instance of LIB to be created and running. It is somewhat similar to what Android Platform Services like LocationManager, SensorManager, PackageManager etc.

Specifically, there are two problems that are to be addressed:-

  1. How to make sure that there is only one instance of LIB ?
  2. How to deploy LIB(separate apk/ bundled with every application that uses it, but only one of them creating the library instance at any point of time) etc.
  3. Is there any other way(other than service/AIDL) to make sure that only one instance of LIB runs irrespective of the number of applications using it.
Robin
  • 497
  • 5
  • 19
  • 4
    Are you talking about a shared library, or something more like a shared service? For shared libraries, it must be included in every app. You may be able to do something with a shared service, however, if it's installed as a separate app. You could possibly create a StartupIntent and if the service was running already it would ignore it. Otherwise it would initialize. – Austen Holmes Dec 02 '11 at 15:05
  • I am for sure talking about shared library. I don't want to use a shared service. Is there any other way to create and use shared library in Android? – Robin Dec 02 '11 at 16:21
  • In the android documentation, it says you can create a shared library, but it's not shared (in an SO or DLL sense.) Every app that needs it will have it installed separately so you end up wasting storage with copies. I believe I read somewhere they may be adding it, but can't find the link. See : http://developer.android.com/guide/developing/projects/index.html – Austen Holmes Dec 02 '11 at 17:12
  • Yes, I too found this thing missing in Android SDK. Library projects in the existing mechanism will lead to wastage of resources - both CPU and storage. Anyways, I can choose wrapping up the library in a service and then letting the apps bind to my service. But in this case, if my library projects exposes n number of classes to the developer, then exposing all of 'em via Serivce Interface/AIDL would be tedious! Is there some alternative to this? – Robin Dec 02 '11 at 17:50
  • I'm not sure. Too new to android development to have much experience with reusable libraries. My apps have been small and mostly self-contained. As I build up some good widgets/services, though I'll definitely have to find out a good solution to this problem. – Austen Holmes Dec 02 '11 at 18:32

2 Answers2

6

Okay, after spending time on this, here the answers to my questions:-

Q. How to make sure that there is only one instance of LIB ? - Only through exposing the LIB functionality through a service.

Q. How to deploy LIB(separate apk/ bundled with every application that uses it, but only one of them creating the library instance at any point of time) etc. -

For service, there would be a separate apk(or probably bundled with one of the service client app),

Q. Is there any other way(other than service/AIDL) to make sure that only one instance of LIB runs irrespective of the number of applications using it.

Yes, that would be like LIB is bundled with every application. But that would need a strong communication protocol between the libraries using intents, so that one of them acts as master, and shares the data with the other libraries. Effectively, the LIB packaged within an apk starts its engine, only if no other instance is running. One way of accomplishing this is using broadcasts and to maintain security, permissions can be associated with the involved broadcast actions.

Robin
  • 497
  • 5
  • 19
  • Hi Robin,I have the exact same issue for my library. Is there a better way of solving it now(since it's been 5 years now)? Also, when you say 'through a service', what exactly do you mean? An Android Service? Or An API endpoint? – Sid Jul 01 '16 at 13:40
0

With the library project, you have a static inclusion of the classes and resources from the library project. Nothing will be running that doesn't need to be.

That can increase the size of your app, but techniques such as ProGuard can remove unused classes from your final .apk.

If you want your library to be running independently of other apps, then it is indeed a service and you should consider what the lifecycle should be (e.g. started by the app, started at boot, etc.).

If your lib contains activities to be run, the Android OS already handles this - the other app just creates an intent and calls out your activity. This is how Android works - apps can share data and workflow between each other seamlessly using intents.

If there are services, it's the same thing - the calling app either interacts with a running service or triggers a service to start.

Library projects handle a lot of issues for most developers, but if you have a really large shared code package you'll want to install as a separate app.

For example, Google Maps is a separate app, but other apps can trigger an intent to load an activity from that app. The same with messaging and other activities that can be handled by different apps.

ProjectJourneyman
  • 3,566
  • 1
  • 27
  • 37
  • My Library is very CPU intensive and I want only a single instance of it to be running for all applications. There is not UI in the library. It generates output at say, per second and I am not sure if Broadcasts would be the ideal channel to use. Listeners can only be used if I encapsulate the library in the service. But the library has exposed n different classes / interfaces for the apps. Encapsulating it in a service will mean providing those many interfaces via service! – Robin Dec 04 '11 at 08:38
  • Maybe you should be looking into the NDK for lower-level implementations, which might have more of the capabilities you're looking for (and reduce your CPU load). It seems like you're trying to do something complicated which there isn't a clean way to accomplish in the Android framework. BTW you might not need to use broadcasts to share the actual data - look into Content Providers. – ProjectJourneyman Dec 13 '11 at 19:32