Android 11부터 시스템 파티션에서 실행되는 네이티브 AIDL 서비스를 필요에 따라 동적으로 시작하고 중지할 수 있습니다.
동적 서비스는 처음 요청될 때 시작되고 더 이상 사용되지 않으면 자동으로 중지됩니다.
동적으로 실행될 수 있는 서비스
이 기능은 수명 주기를 init 및 servicemanager로 제어할 수 있는 네이티브 서비스에서만 사용할 수 있습니다. 앱 패키지 내의 서비스는 지원되지 않으며 대신 바인드된 서비스를 사용해야 합니다.
동적 종료는 서비스가 실행되는 프로세스를 종료하여 작동합니다.
여러 서비스가 동일한 프로세스에 있다면 이 기능과 호환되도록 모든 서비스를 동적으로 등록해야 합니다. 그러면 모든 서비스가 사용되지 않을 때 프로세스가 종료됩니다.
서비스의 init .rc 파일 구성
서비스를 동적으로 실행하려면 앞의 service <name> <cmd> 줄 뒤에 다음 옵션을 서비스의 init .rc 파일에 추가합니다.
interface aidl serviceName
disabled
oneshot
이러한 옵션은 다음 작업을 실행합니다.
interface aidl serviceName: servicemanager가 서비스를 찾을 수 있습니다.
서비스에서 여러 인터페이스를 사용한다면 각 인터페이스를 자체 줄에 선언합니다. 이러한 이름은 servicemanager에서 예상하는 것과 정확히 일치해야 하고 프로세스 이름과는 다를 수도 있습니다.
지연 서비스를 가져오려면 서비스를 시작한 다음 가져와야 합니다.
서비스 관리자에서 getService를 호출하면 서비스가 시작되지만, 대부분의 경우 서비스가 사용 가능한 상태가 되자마자 가져와야 하며 waitForService 변형을 사용하는 것이 좋습니다. 변형을 사용하는 방법은 백엔드 관련 문서를 참고하세요.
서비스 출시
동적 종료는 참조 횟수에 기반하므로 클라이언트는 서비스가 사용되지 않을 때 서비스를 유지해서는 안 됩니다.
특정 작업이 완료될 때까지 서비스를 독립적으로 실행한 다음 동적 동작으로 전환하려면 LazyServiceRegistrar::forcePersist를 사용하여 동적 종료를 사용 설정하거나 중지할 수 있습니다. 이 작업을 서버 측에서 호출한다면 registerService 전에 호출해야 합니다.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-27(UTC)"],[],[],null,["# Run AIDL services dynamically\n\nStarting in Android 11, native AIDL services running in\nthe system partition can be started and stopped dynamically as they are needed.\nDynamic services start when they are first requested and automatically stop when\nthey are no longer in use.\n| **Warning:** Due to Java garbage collection, Java-based services might take a long time to shut down. If this is problematic, you might want to avoid Java client code for dynamic services.\n\nServices that can run dynamically\n---------------------------------\n\nThis feature is available only for native services whose lifecycles can be\ncontrolled by `init` and `servicemanager`. Services within app packages are not\nsupported and should use [bound services](https://developer.android.com/guide/components/bound-services)\ninstead.\n\nDynamic shutdown works by shutting down the process in which the service runs.\nIf multiple services exist in the same process, all of them must be registered\nas dynamic to be compatible with this feature. That process will then shut down\nwhen all of the services are unused.\n\nConfigure a service's init .rc file\n-----------------------------------\n\nTo run a service dynamically, add the following options to the service's init\n`.rc` file after the leading `service \u003cname\u003e \u003ccmd\u003e` line. \n\n interface aidl serviceName\n disabled\n oneshot\n\nThese options do the following:\n\n- `interface aidl serviceName`: Allows `servicemanager` to find the service. If the service uses multiple interfaces, declare each interface on its own line. These names must be exactly what `servicemanager` expects and may differ from the process name.\n- `disabled`: Prevents the service from automatically starting at boot.\n- `oneshot`: Prevents the service from automatically restarting each time it is stopped.\n\nFor more information, see the [Android Init Language\nReadme](https://cs.android.com/android/platform/superproject/+/android-latest-release:system/core/init/README.md)\nin AOSP.\n\nExamples:\n\n- [`apexd`](https://cs.android.com/android/platform/superproject/+/android-latest-release:system/apex/apexd/apexd.rc;drc=fa1746e827d09cf22b5dc9e60c48792da0c4e320;l=2)\n- [`gsid`](https://cs.android.com/android/platform/superproject/+/android-latest-release:system/gsid/gsid.rc;drc=67a2709553ab5d687da9af346b66e77bd7e4f1c1;l=2)\n\nRegister a service\n------------------\n\nEach service is created and registered with `servicemanager`. Registration often\noccurs in a file named `main.cpp`, but the implementation can vary. The\nregistration usually looks something like this: \n\n using android::defaultServiceManager;\n\n defaultServiceManager()-\u003eaddService(serviceName, service);\n\nRegistration is sometimes abstracted by `BinderService::publish` or\n`BinderService::instantiate`, which call the above code.\n\nTo register a service as dynamic, replace its registration code with the\nfollowing: \n\n #include \u003cbinder/LazyServiceRegistrar.h\u003e\n\n using android::binder::LazyServiceRegistrar;\n\n auto lazyRegistrar = LazyServiceRegistrar::getInstance();\n lazyRegistrar.registerService(service, serviceName);\n\n`servicemanager` communicates with `LazyServiceRegistrar` to shut down services\nbased on their reference counts.\n\nExamples:\n\n- [`apexservice`](https://cs.android.com/android/platform/superproject/+/android-latest-release:system/apex/apexd/apexservice.cpp;drc=b68f18b7ebc93617c44411b9bc906811501798c3;l=952)\n- [`gsi_service`](https://cs.android.com/android/platform/superproject/+/android-latest-release:system/gsid/gsi_service.cpp;drc=fc817e25fe44f3c62aa273268d9e2c2e9057699c;l=76)\n\nConfigure AIDL service clients\n------------------------------\n\n### Get the service\n\nTo retrieve a lazy service, the service must be started and then retrieved.\nCalling `getService` on the service manager will start the service, but usually,\nyou want to get the service as soon as it is available, and `waitForService`\nvariants should be used. See [backend-specific\ndocumentation](/docs/core/architecture/aidl/aidl-backends#service-management)\non how to use these.\n\n### Release the service\n\nDynamic shutdown is based on reference counting, so clients must not hold onto\nthe service when it is not in use.\n\nExamples:\n\n- [`ApexManager`](https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/base/services/core/java/com/android/server/pm/ApexManager.java;l=434;drc=0037e9c816a6086bdf80019f59c130f0f0eb9fc0)\n- [`libgsid`](https://cs.android.com/android/platform/superproject/+/android-latest-release:system/gsid/libgsid.cpp;drc=fc817e25fe44f3c62aa273268d9e2c2e9057699c;l=30)\n\nTemporarily disable shutdown\n----------------------------\n\nIf you want a service to run independently until certain tasks are complete and\nthen switch to dynamic behavior, you can use\n`LazyServiceRegistrar::forcePersist` to toggle dynamic shutdown on and off. If\nthis is called from the server side, it should be called before\n`registerService`.\n\nExample: [`apexservice`](https://cs.android.com/android/platform/superproject/+/android-latest-release:system/apex/apexd/apexservice.cpp;drc=b68f18b7ebc93617c44411b9bc906811501798c3;l=951)"]]