Android Java System Service - Beyond system_server


   To start with, this post is for developers who work on android platform and not for application developers. The default version of android open source platform has system services coded in both Java (services hosted by system_server) and C,C++ (media services hosted by mediaserver).

     Recently, i came across requests to have Java based customized services hosted in their own process (and definitely not by system_server). Personally, i preferred all customized services to be in their own process, that way any service specific exception is just going to bring down the respective process and wouldn't trigger a framework reboot and offer a better user experience, opportunity for graceful handling.
Besides, i don't like bloatware services being added to system_server just because its open source. These bloatware services when buggy (memory references, exceptions) has a direct negative impact on the android framework and is a big negative PR for android platform as such. Having said that, there are reasons to add custom services to android framework and it would only make more sense to have these changes reviewed by the Android Community and possibly be reused by various OEMs, Chip makers.

   Anyways, now to the real world, I just managed to create a custom java service hosted in its own process, with few cons. I just added the source code of the service to existing services directory,

      frameworks/base/services/java/com/android/server/NumberGeneratorService.java
       frameworks/base/core/java/android/os/INumberGeneratorService.aidl
       frameworks/base/Android.mk

The aidl is just to expose APIs and Android.mk is just to generate code from the aidl. Android's build system adds this new class to framework.jar.

    Next, the instance of the service isn't created by system_server. NumberGeneratorService.java has its own public static void main(), which creates the service and registers it with servicemanager. The process is  not forked by zygote instead is forked by init, similar to native services being forked by init and it needed the changes to init.rc,

     service number /system/bin/app_process /system/framework com.android.server.NumberGeneratorService
     class main
     user system
     group system
     onrestart restart number

    And there is the NumberGeneratorService in its own process space and yes this approach has its own drawbacks. The first being that the Java process is being forked by init and doesn't get to take advantage of the warm boot of zygote and the memory cost of shared classes too, but it was fun and i just hope bloatware services just move out of system_server.

2 comments:

Unknown said...

the method u have illustrated here is not working

. said...

Ok, what exactly is not working?