Search code examples
javaandroidaidlandroid-binder

Recommended approach for handling errors across process using AIDL (Android)


I have a binder service and a client that live in different processes. Using AIDL, when the client calls into my remote binder service, there are times that I need to relay an error (exception) back to the client.

However, from my understanding, this is not possible. I tried throwing a "RemoteException" from my binder service to see what will happen, and I get

Uncaught remote exception! (Exceptions are not yet supported across processes.)

in my logcat.

Since it looks like this is not possible, what is the best approach for informing the client of an error? I was thinking I can just convert my AIDLs to use C-style interfaces in which I just return an error code (and 0 on success), but this looks ugly.

Is there a better approach?


Solution

  • Your remote method can return a Parcel that contains the result data or an Exception if there is an error. See the Parcel#writeException method. I believe that this is how Android exceptions make it back when performing actions on a ContentProvider that lives in another process. There are many ways to return the result data including using the Bundle class.

    Your manager class can hide the implementation details by unparcelling and returning the data or throwing the unparcelled exception so users never interact with the Parcel.

    Here is a link to the source for Parcel#writeException.