Search code examples
androidandroid-serviceclasscastexceptionandroid-binder

Binding a service from a different APK


I need some help to bound a service by expending the Binder class.

I have 2 APKs. The first one declare a service and I want to bind it from an activity of the second APK.

Both APKs uses the same sharedUserId and the same android:process.

Since they are running in the same process, I don't want to use AIDL for IPC communication (I already try, it's working but I don't want to use it considering I uses only one process).

I can start / bind the service but I can't get the reference to the service :

LocalBinder binder = (LocalBinder);
mService = binder.getService();

I get the exception :

E/AndroidRuntime(6145): java.lang.ClassCastException: 

Is it possible to bound the service by expanding the IBinder class in my case?

Thanks


Solution

  • I've never tried this but I suspect that each .apk is being loaded using its own ClassLoader. This would mean that two identical classes with the same name from each .apk would be regarded as totally different classes by the Dalvik VM.

    I believe you will find this impossible to solve.

    You should therefore use aidl. I imagine you had two objections to aidl.

    • You probably didn't want to spend time marshalling all your data into Parcelable classes, etc. However, as discussed above, I don't believe you're likely to find a way to pass data directly as Java objects between the two .apks, even when running in the same process, so you have no choice.
    • Perhaps you were worried about performance. But you don't need to worry, because Binder calls (including aidl calls) become just plain function calls when they're in the same process.