How to Create Your Own Custom System Service in AOSP: A Complete Guide

Samir Dubey
5 min readAug 4, 2023

--

What is a System Service?

System Services in Android are fundamental components that run in the background and offer various functionalities and features to both system apps and third-party applications. These services are responsible for handling critical tasks, such as managing network connections, handling power management, handling media playback, and more. System services abstract the underlying hardware and system complexities, providing a unified interface for app developers to access platform features.

Now, let’s explore an example of a custom System Service in AOSP, called SdMathService. This service is responsible for performing basic math operations, such as addition and multiplication, and it demonstrates the implementation of an AIDL (Android Interface Definition Language) interface to communicate between processes.

AIDL (Android Interface Definition Language):

AIDL is a language used in Android to define the programming interface that clients can use to interact with a service running in a different process. It simplifies the process of inter-process communication (IPC) and allows apps to communicate seamlessly, even if they are running in separate processes. AIDL is especially useful when developing System Services, as it enables these services to expose methods for remote invocation.

File: frameworks/base/core/java/android/os/ISdMathService.aidl

Explanation:

The AIDL file ISdMathService.aidl defines the interface for the SdMathService. It declares two methods, add and multiply, which can be remotely invoked by clients to perform addition and multiplication operations.

File: frameworks/base/core/java/com/android/server/SdMathService.java

Explanation:

The SdMathService.java file is the implementation of the ISdMathService interface. It extends the ISdMathService.Stub class, which is an auto-generated class from the AIDL interface. The SdMathService class provides the actual implementation of the add and multiply methods to perform the specified mathematical operations.

File: frameworks/base/core/java/android/app/SdMathServiceManager.java

Explanation:

The SdMathServiceManager.java file acts as a client-side manager for the SdMathService. It takes care of invoking the add and multiply methods on the remote service, mService, while handling any potential exceptions.

File: frameworks/base/services/java/com/android/server/SystemServer.java

Explanation:

The SystemServer.java file is a crucial part of the Android system. In the startOtherServices() method, it initializes various system services, and here it starts the SdMathService by registering it with the ServiceManager.

File: frameworks/base/core/java/android/app/SystemServiceRegistry.java

Explanation:

The SystemServiceRegistry.java file is responsible for registering various system services. Here, it registers the SdMathServiceManager as the manager for the SdMathService. It creates an instance of SdMathServiceManager by obtaining the ISdMathService interface through ServiceManager.

File: frameworks/base/core/java/android/content/Context.java

Explanation:

The Context.java file defines a constant SDMATH_SERVICE, which acts as the key to access the SdMathService through the getSystemService method.

File: frameworks/base/Android.bp

Explanation:

The Android.bp file lists the source files included in the build. Here, it includes the ISdMathService.aidl file, which contains the AIDL definition for the SdMathService.

Add Sepolicy rule

Root Path — AOSP/system/sepolicy

service_contexts

This line associates the newly defined “sdmath_service” security context with the object class “sdmath_service” and sets its SELinux type to “s0,” indicating a system service. This ensures that the service can operate securely within the Android system.

Service.te

type sdmath_service, system_api_service, system_server_service, service_manager_type;

This line defines a new security context called “sdmath_service” and grants it permissions to function as a system API service, system server service, and service manager type. This allows the service to be accessed and used by other system components.

Build source commands:

source build/envsetup.sh

lunch <build_variant>

make update-api -j12 : This command generates and updates the API documentation for the current AOSP build.

  • It compares newly generated API stubs with existing definitions, updating the “current.txt” file to reflect latest API changes. It ensures accurate documentation, maintains backward compatibility, and prevents compatibility issues for app developers, providing a reliable reference and a consistent Android ecosystem.

Now you can Access this Custom SystemService from any System Component as below.

Conclusion:

In this article, we explored the concept of System Services, and AIDL. We learned how AIDL simplifies inter-process communication and saw a practical example of a custom System Service, SdMathService. Understanding AOSP and the components involved in building a custom System Service helps developers contribute to the Android ecosystem and create innovative features for Android devices.

Furthermore, we discussed the necessity of updating the API documentation with the “make api-stubs-docs-update-current-api” command. By keeping the API documentation up-to-date, developers can build compatible applications that leverage the functionalities of the System Service effectively. Continuously updating the API documentation ensures that developers are informed of any changes and maintain compatibility with the current version of the Android system.

--

--

Samir Dubey

AOSP Engineer @ Zebra Technologies | AOSP Enthusiast | Android Framework | Linux