My company produces an SDK delivered as an Android Library aar file. As part of that SDK, we define a service:
<service
android:name=".session.internal.ChatSession"
android:description="@string/description"
android:enabled="true"
android:exported="false"
android:label="Network communication service"
/>
This service is then started and bound by more code in the SDK:
public boolean bindService(final Runnable whenBound) {
if (connection == null) {
// Make sure the service is running
boolean success = startService();
if(BuildConfig.DEBUG && !success) {
throw new AssertionError("startService failed");
}
connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
chatSession = (IChatSession) service;
if(whenBound != null) {
whenBound.run();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
chatSession = null;
connection = null;
}
};
success = context.bindService(new Intent(context, ChatSession.class), connection, Context.BIND_IMPORTANT);
if(BuildConfig.DEBUG && !success) {
throw new AssertionError("bindService failed");
}
return success;
}
if(whenBound != null) {
whenBound.run();
}
return true;
}
boolean startService() {
boolean success = true;
if(!isServiceRunning()) {
success = context.startService(new Intent(context, ChatSession.class)) != null;
}
return success;
}
This all works fine as long as there is only one application using the SDK installed on the mobile device.
Since the service is both explicitly not exported (android:exported="false"
) and implicitly not exported (there is no <intent-filter>
defined) we expected this to work fine with multiple applications installed as well, with each application getting it's own, private, instance of the service when bindService
is called.
What actually happens is that neither application works any more as neither ServiceConnection.onServiceConnected
or ServiceConnected.onServiceDisconnected
is ever called, although the calls to context.startService
and context.bindService
both return success.
Once both applications have been installed, the only way to get either of them to work is to uninstall both, and then reinstall only one. It's not sufficient to uninstall either independently.