Android Framework에서 새로운 시스템 서비스 추가 및 활용
Android는 다양한 기능을 시스템 서비스(System Service) 형태로 제공하며, 개발자는 필요에 따라 새로운 시스템 서비스를 추가할 수 있습니다. 본 포스팅에서는 AOSP(Android Open Source Project) 환경에서 새로운 시스템 서비스를 추가하고 활용하는 방법에 대해 설명합니다.
1. 시스템 서비스 개요
Android의 시스템 서비스는 SystemServer
에서 실행되며, 다양한 기능을 앱 및 다른 시스템 서비스에 제공합니다. 대표적인 시스템 서비스에는 ActivityManagerService
, WindowManagerService
, PackageManagerService
등이 있습니다. 새로운 기능을 추가하기 위해서는 사용자 정의 시스템 서비스를 만들어 SystemServer
에 등록해야 합니다.
2. 새로운 시스템 서비스 추가 개요
새로운 시스템 서비스를 추가하기 위해서는 다음 단계가 필요합니다.
- AIDL 인터페이스 정의 (필요한 경우)
- 시스템 서비스 클래스 구현
- SystemServer에 서비스 등록
- 클라이언트에서 서비스 접근
이제 각 단계별로 자세히 설명하겠습니다.
3. AIDL을 이용한 System API 추가 방법
AIDL(Android Interface Definition Language)을 이용하면 IPC(Inter-Process Communication) 방식으로 시스템 서비스를 클라이언트에서 사용할 수 있습니다.
3.1 AIDL 인터페이스 정의
먼저 frameworks/base/aidl/android/os/
경로에 AIDL 파일을 생성합니다.
// ICustomService.aidl
package android.os;
interface ICustomService {
void customMethod();
}
이후, ICustomService.aidl
을 컴파일하면 Java 인터페이스가 자동 생성됩니다.
3.2 시스템 서비스 클래스 구현
AIDL에서 생성된 인터페이스를 구현하는 시스템 서비스 클래스를 작성해야 합니다.
package com.android.server;
import android.os.ICustomService;
import android.os.RemoteException;
import android.util.Slog;
public class CustomService extends ICustomService.Stub {
private static final String TAG = "CustomService";
public CustomService() {
Slog.i(TAG, "CustomService Created");
}
@Override
public void customMethod() throws RemoteException {
Slog.i(TAG, "customMethod() is called");
}
}
이제 CustomService
를 SystemServer
에서 실행하도록 추가해야 합니다.
3.3 SystemServer에 서비스 등록
SystemServer
는 시스템 부팅 시 다양한 시스템 서비스를 초기화하는 역할을 합니다. SystemServer.java
에 새로운 서비스를 등록해야 합니다.
3.3.1 SystemServer.java
수정
파일 위치: frameworks/base/services/java/com/android/server/SystemServer.java
import com.android.server.CustomService;
...
private void startOtherServices() {
Slog.i(TAG, "Starting CustomService");
try {
ServiceManager.addService("custom_service", new CustomService());
} catch (Exception e) {
Slog.e(TAG, "Failed to start CustomService", e);
}
}
위 코드를 추가하면 SystemServer
부팅 과정에서 CustomService
가 시작됩니다.
4. 새로운 System API 추가 방법
System API를 추가하려면 frameworks/base/core/java/android/os/
경로에 새로운 API를 정의합니다.
예를 들어, CustomManager.java
를 생성하여 CustomService
에 접근할 수 있도록 합니다.
package android.os;
import android.os.ICustomService;
import android.os.ServiceManager;
import android.os.RemoteException;
public class CustomManager {
private static ICustomService sService;
static {
sService = ICustomService.Stub.asInterface(ServiceManager.getService("custom_service"));
}
public void customMethod() {
try {
if (sService != null) {
sService.customMethod();
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
이제 CustomManager
를 통해 CustomService
의 기능을 클라이언트에서 사용할 수 있습니다.
5. 클라이언트에서 서비스 활용
시스템 서비스가 정상적으로 등록되었으면, 클라이언트 코드에서 CustomManager
를 이용하여 호출할 수 있습니다.
CustomManager customManager = new CustomManager();
customManager.customMethod();
클라이언트 코드에서 customMethod()
를 호출하면, CustomService
에 정의된 메서드가 실행됩니다.
6. AOSP 빌드 및 테스트
서비스를 추가한 후에는 AOSP를 다시 빌드해야 합니다.
source build/envsetup.sh
lunch aosp_arm-eng
make -j8
이후, 에뮬레이터 또는 디바이스에서 adb
명령어를 사용하여 서비스가 정상적으로 등록되었는지 확인할 수 있습니다.
adb shell service list | grep custom_service
정상적으로 실행되었다면 다음과 같은 결과를 확인할 수 있습니다.
custom_service: [android.os.ICustomService]
7. 결론
본 포스팅에서는 AOSP 환경에서 새로운 시스템 서비스를 추가하는 방법에 대해 설명하였습니다. 주요 과정은 다음과 같습니다.
- AIDL을 이용하여 IPC 인터페이스를 정의
CustomService
클래스를 구현하여 서비스의 핵심 기능을 정의SystemServer
에서 서비스를 초기화 및 등록CustomManager
를 통해 클라이언트가 서비스를 호출하도록 API를 제공- AOSP를 빌드하고
adb
를 사용하여 정상적으로 서비스가 실행되는지 확인
이를 기반으로 Android의 기본적인 시스템 서비스 구조를 이해하고, 필요에 따라 새로운 기능을 추가하는 방법을 익힐 수 있습니다.
'Android > Framework' 카테고리의 다른 글
Android의 주요 IPC 기법 (0) | 2025.04.15 |
---|---|
Application과 Framework의 관계 (0) | 2025.04.14 |
Binder IPC 개념 및 동작 방식 (0) | 2025.04.12 |
Android PowerManagerService 분석 (0) | 2025.04.11 |
Android PackageManagerService 분석 (0) | 2025.04.10 |