// Prepare the configuration fileVirtualMachineConfigconfig=newVirtualMachineConfig.Builder(getApplication(),"assets/vm_config.json").build();// Load (or create) the handle to a VMVirtualMachinevm=VirtualMachineManager.getInstance(getApplication()).getOrCreate("my_vm",config);// Run the VMvm.run();
アプリの VM 部分と通信する
アプリの VM 部分と通信するために、まず VM 上のバインダー サービスの準備が整ったときに通知されるようにコールバックを登録します。通知を受けたら、バインダー サーバーに接続し、カスタム AIDL インターフェースを使用してサーバーと通信します。例:
[[["わかりやすい","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-21 UTC。"],[],[],null,["# Write an AVF app\n\nA AVF-compatible app has two parts: The portion of the app running on the host\nAndroid OS and the portion of the app running on Microdroid within a pVM.\n\nThe portion of the app running on Android implements the user interface,\nnon-confidential business logic, and creates and manages the lifecycle of a\npVM.\n\nThe portion of the app running on Microdroid, within a pVM, is responsible for\nperforming any tasks that need to be performed securely.\n\nTo launch and communicate with the pVM portion of your app, your host\napplication creates a pVM and runs a native shared library\nwithin the pVM. This library implements a binder service that the host portion\nof the app uses to communicate with the portion of the app within a pVM. Figure\n1 shows the two parts of the application and the binder communication channel:\n\n\n**Figure 1.** AVF app loading and communication\n\n\u003cbr /\u003e\n\nSet up the configuration file\n-----------------------------\n\nYour `vm_config.json` file should have an entry for the pVM's operating system\nand shared library. The following `assets/vm_config.json` file shows config file\nentries for Microdroid and a shared native library: \n\n {\n \"os\": {\n \"name\": \"microdroid\"\n },\n \"task\": {\n \"type\": \"microdroid_launcher\",\n \"command\": \"MicrodroidTestNativeLib.so\"\n }\n }\n\nImplement the binder service\n----------------------------\n\nWithin your shared library, implement a binder service. For example: \n\n extern \"C\"\n int android_native_main(int, char**) {\n // Implement your binder service here\n }\n\nCreate app code\n---------------\n\nIn the host portion of your app, create code that prepares the configuration\nfile, loads (or creates) a handle to the VM, and runs the VM. For example: \n\n // Prepare the configuration file\n VirtualMachineConfig config = new VirtualMachineConfig\n .Builder(getApplication(), \"assets/vm_config.json\")\n .build();\n\n // Load (or create) the handle to a VM\n VirtualMachine vm = VirtualMachineManager\n .getInstance(getApplication())\n .getOrCreate(\"my_vm\", config);\n\n // Run the VM\n vm.run();\n\nCommunicate with VM portion of your app\n---------------------------------------\n\nTo communicate with the VM portion of your app, you first register a callback\nto be notified when the binder service on the VM is ready. When notified, you\nconnect to the binder server and then talk with the server using the custom AIDL\ninterface. For example: \n\n // Register the callback\n vm.setCallback(Executors.newSingleThreadExecutor(),\n new VirtualmachineCallback() {\n @Override\n public void onPayloadReady(VirtualMachine vm) {\n // Connect to the binder server\n IBinder binder = vm.connectToVsockServer(PORT).get();\n IMyService svc = IMyService.Stub.asInterface(binder);\n // Talk with server using custom AIDL interface\n Result res = svc.doSomething();\n }\n }); //exception handling & proper threading omitted\n vm.run();\n\nTo download source code for a demo app that demonstrates the steps in this\ndocument, refer to\n[MicrodroidDemo](https://android.googlesource.com/platform/packages/modules/Virtualization/+/refs/heads/android16-release/android/MicrodroidDemoApp/).\n| **Note:** This demo works only on Cuttlefish."]]