Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can multiple Android applications share a single process and application context?

Tags:

android

I am wondering if I could share a singleton on the application context across multiple applications? Each application would be in its own APK but This might sound like bad architecture but hear me out first.

The reason I would like to do this is because I have an existing library which controls an external device over bluetooth. The library is java but under the covers there is allot of native (c/c++) all wrapped by java. I have looked at putting this all in a service but the IPC (I was using aidl) becomes very cumbersome fast. Trying to reduce object to primitives is next to impossible (private fields, jni pointers etc) and trying to wrap everything with AIDL is very messy.

If each app could run in the same process and also have the same application context hence allowing me to keep a singleton object on there that would make things much easier. My googl-fu is failing me on this one. Maybe its not possible?

like image 231
startoftext Avatar asked Jul 15 '13 21:07

startoftext


1 Answers

It should be possible, but you'll run into problem with the fact that you are still running two different APKs, each has its own ClassLoader. Since the same Class loaded by different ClassLoader are considered completely different from each other, you cannot create a Class from APK A and expect APK B to be able to access that same Class. However, one loophole is that Android system classes in the same process are always loaded by the same ClassLoader, therefore the solution is to store your data through Android system classes (e.g. System.set/getProperty). You can read an excellent article on this technique here.

As you wish to store data more complex than Java primitives and considering the above approach would severely limits the data structure you can store, it may still end up that AIDL is your best bet. But at least your data does not need to cross process boundaries if you put both APKs in the same process, so it should be reasonably efficient.

like image 98
Kai Avatar answered Oct 31 '22 11:10

Kai