Search code examples
androidaidl

Android AIDL: Returning Map works at service level but breaks at client level


As discussed in How to return hashmap in an aidl file thread, I am returning "Map" and not generics map like "Map" or an implementation of Map like a Hashmap. When I check the bound service side output, I get the required key-values in the map, i.e. the map is populated correctly. But the same function at the service side, when called from the client side, returns an empty Map because I initialized my Map in service as:

Map myMap = new HashMap();

Please note that the client service contract is good as the service has other methods which returns boolean, and some of them are void. All of them are working correctly. The problem is just with this method which is returning a Map, and I suppose I am doing some syntactical mistake.

Below is my service side of implementation:

public Map getStatus() {
    List<MyObject> MyList = new ArrayList<>();
    if (myDaggerInjectedObject != null) {
        MyList = myDaggerInjectedObject.getMyList();
    }
    Map myMap = new HashMap();
    for (MyObject myObject : MyList) {
        myMap.put(myObject.getScannableId(), myObject.getStatus().name());
    }
    return myMap;
}

Below is my client code which calls the above bound service method:

public Map getStatus() throws RemoteException {
    try {
        Map status = myService.getStatus();
        // the status object is set to blank hashmap, it should have some data i.e key-value pair
        return status;
    } catch (RemoteException e) {
        Log.e(TAG, "Service connection not available");
        throw e;
    }
}

Can somebody help pointing out the mistake here?


Solution

  • I did just two things and that resolved this fishy behaviour (Fishy because the same server-client code was working on my dummy client and server android apps but not working on my production client and server apps):

    1. Different AIDL files on server and client: I noticed that the AIDL file on client side was a superset of that on the server side (the interface at the client side had 3 methods whereas at the server side 2 methods)
    2. Cleaning all environments: Did a clean build before rebuilding all my AIDL, Client, Server modules/Components.

    Although the first point is not at all logical to me still, but I wanted to jot down in this answer for people to comment, if it COULD be of any issue in this scenario.