extern "C"
int android_native_main(int, char**) {
// Implement your binder service here
}
创建应用代码
在应用的主机部分,创建用于准备配置文件、向虚拟机加载(或创建)句柄以及运行虚拟机的代码。例如:
// 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();
[null,null,["最后更新时间 (UTC):2025-03-21。"],[],[],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."]]