2025 年 3 月 27 日より、AOSP のビルドとコントリビューションには aosp-main
ではなく android-latest-release
を使用することをおすすめします。詳細については、AOSP の変更をご覧ください。
AIDL サービスの動的な実行
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Android 11 以降、システム パーティションで実行されるネイティブ AIDL サービスは、必要に応じて動的に開始および停止できます。動的なサービスは、最初にリクエストされたときに開始し、使用されなくなると自動的に停止します。
動的な実行が可能なサービス
この機能は、ライフサイクルを init
と servicemanager
で制御できるネイティブ サービスでのみ使用できます。アプリ パッケージ内のサービスはサポートされていないため、代わりにバインドされたサービスを使用します。
動的なシャットダウンは、そのサービスを実行しているプロセスをシャットダウンすることで動作します。
同じプロセス内に複数のサービスが存在する場合、この機能との互換性を確保するためには、すべてのサービスを動的として登録する必要があります。そのプロセスは、すべてのサービスが使用されていないときにシャットダウンします。
サービスを動的に実行するには、サービスの init .rc
ファイルの先頭行「service <name> <cmd>
」の下に次のオプションを追加します。
interface aidl serviceName
disabled
oneshot
上記のオプションの意味は次のとおりです。
interface aidl serviceName
: servicemanager
がサービスを検出できるようにします。
サービスが複数のインターフェースを使用する場合は、各インターフェースを別個の行で宣言します。これらの名前は servicemanager
が想定するものと完全に一致する必要があり、プロセス名とは異なる場合があります。
disabled
: 起動時にサービスが自動的に開始しないようにします。
oneshot
: サービスが停止するたびに自動的に再開しないようにします。
詳細については、AOSP の Android Init 言語の README をご覧ください。
例:
サービスの登録
各サービスは servicemanager
によって作成され登録されます。登録は main.cpp
という名前のファイルで行うのが一般的ですが、さまざまな実装が可能です。通常の登録は次のように行います。
using android::defaultServiceManager;
defaultServiceManager()->addService(serviceName, service);
上記のコードを呼び出す BinderService::publish
または BinderService::instantiate
で登録を抽象化する場合もあります。
サービスを動的として登録するには、登録に使用するコードを次のコードに置き換えます。
#include <binder/LazyServiceRegistrar.h>
using android::binder::LazyServiceRegistrar;
auto lazyRegistrar = LazyServiceRegistrar::getInstance();
lazyRegistrar.registerService(service, serviceName);
servicemanager
は LazyServiceRegistrar
と通信し、参照数に基づいてサービスをシャットダウンします。
例:
サービスの取得
遅延サービスを取得するには、サービスを開始して取得する必要があります。サービス マネージャーで getService
を呼び出すとサービスが開始されますが、通常はそのサービスが使用可能になった時点で取得するために waitForService
バリアントを使用する必要があります。これらの使用方法については、バックエンド固有のドキュメントをご覧ください。
サービスの解放
動的なシャットダウンは参照数に基づいて行われるため、クライアントは使用していないサービスを参照し続けるべきではありません。
例:
シャットダウンの一時的な無効化
サービスを非動的に実行し、特定のタスクが完了した後で動的に切り替える場合は、LazyServiceRegistrar::forcePersist
を使用して動的なシャットダウンのオンとオフを切り替えます。これをサーバー側から呼び出す場合は、registerService
の前に呼び出す必要があります。
例: apexservice
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-03-26 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-03-26 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)"]]