Method, device, equipment and storage medium for calling native function
Patent Information
- Application Number
- CN202210367654.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-04-08
- Publication Date
- 2026-10-27
- Estimated Expiration
- 2042-04-08
AI Technical Summary
由于Pigeon工具不支持Flutter应用作为独立应用单独运行的场景,因此,在Flutter应用单独运行时,无法调用原生功能
[0053]在本申请实施例中,在Flutter应用请求调用原生系统的目标原生功能时,首先调用远程过程调用层的目标接口;接着,确定目标接口中封装的调用方法代码,且调用方法代码采用目标代码生成器生成的,简化编写调用方法代码的操作,进一步简化调用channel和对接协议所带来的成本;最后,基于调用方法代码执行:检测Flutter应用当前的运行模式,并在确定Flutter应用当前处于独立运行模式时,调用套接字接口,建立网络通道;在Flutter应用独立运行时,实现网络通道的建立,因此,通过网络通道实现Flutter应用和原生系统对应的服务端之间的通信;故,通过网络通道,将原生功能对应的请求数据,发送给原生系统对应的服务端,并可接收服务端返回的目标原生功能对应的目标功能代码,并将目标功能代码嵌入到Flutter应用中执行,实现Flutter应用在独立运行模式下,调用原生系统的原生功能,且简化调用原生功能的能力。
Smart Images

Figure CN116932240B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a method, apparatus, device, and storage medium for invoking native functions. Background Technology
[0002] With the development of network technology, applications, as one of the products of network technology, have been widely used in various fields. In order to reduce the development and maintenance costs of applications, a cross-platform development approach has been proposed, and applications developed using this approach support various operating systems.
[0003] Currently, cross-platform development is achieved through cross-platform application development frameworks. Flutter is one such framework; it corresponds to a UI system, handling only the rendering layer and lacking access to underlying native functionalities such as Bluetooth, camera, and GPS. However, a complete application requires more than just a UI; it also needs some underlying capabilities of the native system. Therefore, to enable the development of a complete application using Flutter, a channel mechanism is provided as a communication bridge between the Flutter application and the native system, allowing them to communicate and access relevant native functionalities.
[0004] To simplify the costs associated with channel calls and protocol integration, the Flutter framework includes the Pigeon tool, which automatically generates the call method code. Currently, Pigeon only supports scenarios where a Flutter application runs as a plugin within its host application. In this case, when the Flutter application needs to call certain native functions, it executes the call method code generated by Pigeon to simplify communication between the channel and the native system, thus obtaining the corresponding native functionality. Because Pigeon does not support scenarios where a Flutter application runs as a standalone application, native functions cannot be called when the Flutter application is running independently.
[0005] Therefore, while simplifying the ability to call native functions, how to implement native function calls when a Flutter application runs independently is a technical problem that needs to be solved. Summary of the Invention
[0006] This application provides a method, apparatus, device, and storage medium for calling native functions, which enables native function calls when a Flutter application runs independently, and simplifies the ability to call native functions.
[0007] In a first aspect, embodiments of this application provide a method for calling native functions, applied to the Flutter application side, the method including:
[0008] When a Flutter application requests to call a target native function of the native system, it calls the target interface of the remote procedure call layer and performs the following operations:
[0009] Detect the current running mode of the Flutter application and obtain the detection results;
[0010] Based on the detection results, when it is determined that the Flutter application is currently in stand-alone running mode, the socket interface is called to establish a network channel;
[0011] The request data corresponding to the target native function is sent to the corresponding server of the native system through the network channel.
[0012] Receive the target function code corresponding to the target native function returned by the server, and embed the target function code into the Flutter application for execution.
[0013] Secondly, embodiments of this application provide a method for invoking native functions, applied to the native system side, the method including:
[0014] When request data is detected, the target native function corresponding to the request data is determined, and the target function code corresponding to the target native function is obtained; the request data is obtained by monitoring the network channel and is used to characterize the Flutter application's request to call the target native function of the native system;
[0015] Return the target functionality code to the Flutter application so that the target functionality code can be embedded into the Flutter application for execution.
[0016] Thirdly, embodiments of this application provide a device for calling native functions, applied to the Flutter application side. The device includes an execution unit, wherein the execution unit includes a detection unit, an establishment unit, a sending unit, and a receiving unit.
[0017] The execution unit is used to invoke the target interface of the remote procedure call layer when a Flutter application requests to call a target native function of the native system, and to perform the following operations:
[0018] The detection unit detects the current running mode of the Flutter application and obtains the detection results.
[0019] By establishing a unit and based on the detection results, when it is determined that the Flutter application is currently in stand-alone running mode, the socket interface is called to establish a network channel;
[0020] Through the sending unit and the network channel, the request data corresponding to the target native function is sent to the server of the native system.
[0021] The receiving unit receives the target function code corresponding to the target native function returned by the server and embeds the target function code into the Flutter application for execution.
[0022] Optionally, after obtaining the detection result through the detection unit and before receiving the target function code corresponding to the target native function returned by the server through the receiving unit, it is also used for:
[0023] Based on the detection results, when it is determined that the Flutter application is currently in embedded running mode, a specific platform channel defined by the Flutter framework is invoked to send the request data corresponding to the target native function to the server of the native system; whereby, embedded running mode is used to indicate that the Flutter application and the current host application are in the same process.
[0024] Optionally, after invoking the target interface of the remote procedure call layer, before sending the request data corresponding to the target native function to the native system server through the sending unit, it is also used for:
[0025] The context information defined in the call request corresponding to the target native function is encapsulated to obtain the corresponding language request protocol; wherein, the context information includes at least one of the following: request service name, request method name, and request identifier;
[0026] The language request protocol is serialized to obtain the corresponding binary data, and the binary data is used as the request data.
[0027] Optionally, for the target interface applied to the Flutter application proxy interface of the channel connection, the target native function of the native system requested by the Flutter application is determined in the following way:
[0028] When a native function call request is received from a Flutter application via the Flutter application proxy interface, it is determined that the Flutter application is requesting to call a native function of the native system.
[0029] Optionally, the remote procedure call layer may also include:
[0030] Define other proxy interfaces besides the Flutter application proxy interface, and each other proxy interface corresponds to a business scenario.
[0031] Optionally, the execution unit calls the target interface of the remote procedure call layer, specifically for:
[0032] The method call code is encapsulated in the target interface of the remote procedure call layer; the method call code is generated by the target code generator based on the method protocol defined in the interface file and the configuration parameters.
[0033] Fourthly, embodiments of this application provide a device for invoking native functions, applied to the native system side, the device including: a monitoring unit and a return unit;
[0034] The monitoring unit is used to determine the target native function corresponding to the request data when the request data is detected, and to obtain the target function code corresponding to the target native function; wherein, the request data is obtained by monitoring the network channel and is used to characterize the Flutter application requesting to call the target native function of the native system;
[0035] The return unit is used to return the target functionality code to the Flutter application so that the target functionality code can be embedded into the Flutter application for execution.
[0036] Optionally, the monitoring unit is specifically used for:
[0037] Decode the request data to obtain the context information within it;
[0038] Based on the requested service name in the context information, determine the target service corresponding to the requested service name;
[0039] Based on the request method name in the context information, determine the target method proxy in the target service that corresponds to the request method name, and call the method executor stored in the target method proxy to obtain the target function code used to implement the target native function.
[0040] Optionally, the requested data is binary data; the monitoring unit is specifically used for:
[0041] Decode the binary data to determine the corresponding language request protocol;
[0042] The language request protocol is constructed and processed to obtain context information from the request data.
[0043] Optionally, the return unit is specifically used for:
[0044] The target function code is returned to the Flutter application via a network channel.
[0045] Optionally, the target service is configured as follows:
[0046] When starting the Flutter framework engine, obtain the native system server;
[0047] When the target service is obtained, the method proxy of the target service is initialized, and the method proxy and the service name of the target service are stored on the server.
[0048] Optionally, the monitoring unit is also used to: monitor request data through specific platform channels defined by the Flutter framework;
[0049] The return unit is also used to return the target function code to the Flutter application through a specific platform channel.
[0050] Fifthly, embodiments of this application provide a device for invoking native functions, comprising: a memory and a processor, wherein the memory is used to store computer instructions; and the processor is used to execute the computer instructions to implement the steps of the method for invoking native functions provided in embodiments of this application.
[0051] Sixthly, embodiments of this application provide a computer-readable storage medium storing computer instructions, which, when executed by a processor, implement the steps of the method for invoking native functions provided in embodiments of this application.
[0052] In a seventh aspect, embodiments of this application provide a computer program product, which includes computer instructions stored in a computer-readable storage medium; when the processor of an electronic device reads the computer instructions from the computer-readable storage medium, the processor executes the computer instructions, causing the electronic device to perform the steps of the method for calling native functions provided in embodiments of this application.
[0053] In this embodiment, when a Flutter application requests to call a target native function of the native system, the target interface of the remote procedure call layer is first called. Next, the calling method code encapsulated in the target interface is determined. Since the calling method code is generated by a target code generator, the writing of the calling method code is simplified, further reducing the cost associated with calling channels and interfacing protocols. Finally, based on the execution of the calling method code: the current running mode of the Flutter application is detected, and when it is determined that the Flutter application is currently in independent running mode, a socket interface is called to establish a network channel. Since the network channel is established during independent running of the Flutter application, communication between the Flutter application and the corresponding server of the native system is achieved through the network channel. Therefore, the request data corresponding to the native function is sent to the corresponding server of the native system through the network channel, and the target function code corresponding to the target native function returned by the server can be received. This target function code is then embedded into the Flutter application for execution, enabling the Flutter application to call the native functions of the native system in independent running mode, while simplifying the calling of native functions. Attached Figure Description
[0054] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0055] Figure 1 This is a schematic diagram of an application scenario provided by an embodiment of this application;
[0056] Figure 2 A technical architecture diagram for calling native functions provided in this application embodiment;
[0057] Figure 3 This application provides a schematic diagram illustrating the invocation of a native function in an embodiment.
[0058] Figure 4 A flowchart illustrating a method for calling native functions from a Flutter application side, as provided in this application embodiment;
[0059] Figure 5 This is a schematic diagram illustrating a standalone Flutter application provided in an embodiment of this application.
[0060] Figure 6 A flowchart illustrating another method for calling native functions from a Flutter application side, provided in an embodiment of this application;
[0061] Figure 7 This is a schematic diagram illustrating a Flutter application as a part of a host application, provided as an embodiment of this application.
[0062] Figure 8 This application provides a schematic diagram of a Flutter application calling native functions.
[0063] Figure 9 A flowchart illustrating a specific implementation method for calling native functions from a Flutter application side, as provided in this application embodiment;
[0064] Figure 10 This application provides a schematic diagram illustrating the process of a Flutter application calling a native function.
[0065] Figure 11 A flowchart illustrating a method for calling native functions from the native system side, as provided in this application embodiment;
[0066] Figure 12 A flowchart illustrating a specific implementation method for calling native functions from the native system side, as provided in this application embodiment;
[0067] Figure 13 A flowchart illustrating another specific implementation method for native system-side invocation of native functions provided in this application embodiment;
[0068] Figure 14 This application provides a schematic diagram of a data processing procedure for a native system to call a native function.
[0069] Figure 15 This is a schematic diagram illustrating a service registration method provided in an embodiment of this application;
[0070] Figure 16 A structural diagram of a device for calling native functions is provided in an embodiment of this application;
[0071] Figure 17 Another device structure diagram for calling native functions provided in this application embodiment;
[0072] Figure 18 This is a structural diagram of an electronic device provided in an embodiment of this application. Detailed Implementation
[0073] To make the objectives, technical solutions, and beneficial effects of this application clearer, the technical solutions in the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments in this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0074] To facilitate a better understanding of the technical solutions of this application by those skilled in the art, some of the concepts involved in this application are introduced below.
[0075] Cross-platform refers to an application that can run on multiple operating platforms. In this embodiment, the operating platform is also called an operating system, which includes, but is not limited to, Android, iOS, and macOS. That is, an application developed for one operating system can still run on another. For example, a video playback application developed for Android can not only run on Android, but also run normally on iOS.
[0076] Cross-platform application development frameworks are frameworks used to develop cross-platform applications, including the Flutter framework. The Flutter framework is an open-source platform. Therefore, in this embodiment, the Flutter framework can also be referred to as the Flutter development platform. Furthermore, the Flutter development platform has the following characteristics:
[0077] Feature 1: It has the function of rendering a user interface (UI);
[0078] Feature 2: It has a responsive framework, so you don't need to manually update the UI content; you only need to update the variables.
[0079] Feature 3: It can develop minimum viable products, has a rapid development process, low development costs, and high stability.
[0080] Pegion is a code generation tool that automatically generates generic template code from Dart scripts, which in turn generates method call code for invoking functions (or methods). By executing the method call code generated by Pegion, Flutter applications and their native operating systems (such as iOS and Android) can communicate more securely, simply, and quickly through channels. On the application side, messages corresponding to function calls can be sent through channels; on the native system side, function calls can be received and results returned through channels.
[0081] Remote Procedure Call (RPC) is a protocol that allows a program on a remote computer to request a service over a network without needing to understand the underlying network technology. RPC uses a client / server model, where the caller is a client and the service provider is a server. In this embodiment, a remote procedure call refers to a Flutter application running independently, meaning the Flutter application and the native application are not in the same process; in this case, the Flutter application acts as the caller, and the native system acts as the provider.
[0082] In this embodiment of the application, a Local Procedure Call (LPC) refers to a Flutter application being embedded within a native application, meaning that the Flutter application and the native application are in the same process.
[0083] A socket is a communication connection on a network through which two programs exchange data. One end of the connection is called a socket. Establishing a network communication connection requires at least one pair of sockets. Sockets are designed for a client / server model, providing different socket system calls for the client and server. A client randomly requests a socket, and the system assigns it a socket number. The server has globally recognized sockets, and any client can send connection requests and service requests to the server. In this context, the caller requesting service is a client, and the provider offering the service is a server.
[0084] A data structure is a way of storing and organizing data in a computer. It refers to a collection of data elements that have one or more specific relationships with each other; that is, the data structure defines configuration parameters and the data format corresponding to each configuration parameter.
[0085] The word “exemplary” as used below means “serving as an example, embodiment, or illustration.” Any embodiment illustrated as an “exemplary” need not be construed as superior to or better than other embodiments.
[0086] The terms "first" and "second" used in this document are for descriptive purposes only and should not be construed as indicating relative importance or implying the number of technical features indicated. Therefore, features defined as "first" or "second" may explicitly or implicitly include one or more features. In the description of the embodiments of this application, unless otherwise stated, "multiple" means two or more.
[0087] The design concept of the embodiments of this application is briefly introduced below:
[0088] With the development of network technology, applications, as one of the products of network technology, have been widely used in various fields; moreover, the development and operation of applications have always been a key focus in network technology; specifically, the development of applications has evolved from native development methods to cross-platform development methods.
[0089] When developing applications using cross-platform development methods, the Flutter framework is provided. Flutter is a cross-platform application development framework. While Flutter corresponds to a UI system, it only manages the rendering layer and cannot provide underlying native system functionalities such as Bluetooth, camera, or GPS. However, a complete application requires some underlying capabilities of the native platform in addition to the UI. Therefore, to develop a complete application using the Flutter framework, a channel mechanism is provided as a communication bridge between the Flutter application and the native system. This allows the Flutter application, whether running Kotlin or Java on Android or Swift or Objective-C on iOS, to call native system functions through the channel.
[0090] However, calling methods through Channels in the Flutter framework has some problems, such as: poor consistency and maintainability of multi-platform interface definitions; inability to guarantee type safety and low communication fault tolerance; and the need for manual decoding and de-decoding of complex object conversions. Therefore, to solve these problems, the Flutter framework also includes the Pigeon tool, which automatically generates the method calling code. Pigeon's main principle is to define the Channel's protocol interface and data structure on Flutter Dart. The data structure is represented by Dart Classes. The Pigeon tool is introduced at compile time. Through Dart reflection, Pigeon converts the corresponding Classes into Map data structures and generates interfaces for each platform, simplifying the writing of Channels and reducing the cost associated with protocol integration.
[0091] In related technologies, the Pigeon tool only supports scenarios where Flutter applications run as plugins within their respective host applications. In this case, when a Flutter application needs to call certain native functions, it executes the calling method code generated by the Pigeon tool to simplify communication between the channel and the native system to obtain the corresponding native functionality. Because the Pigeon tool does not support scenarios where a Flutter application runs as a standalone application, native functions cannot be called when a Flutter application runs independently.
[0092] Meanwhile, Pigeon's data type support is limited, not supporting List, Map, and enumeration types, and it does not support the reuse of existing data structures. The code generated by Pigeon is difficult to reuse in other business scenarios and is difficult to maintain.
[0093] Considering that RPC technology supports List, Map, and enumeration types, which are all the data types supported by RPC technology; and that the calling method code generated by the RPC code generator can simplify the writing of code, and that the code generated by the RPC code generator can be reused on different frameworks, thereby improving the maintenance efficiency between different frameworks.
[0094] In view of this, this application proposes an implementation method for embedding RPC technology in the Flutter framework, so as to generate calling method code through the RPC code generator in the RPC technology, simplifying Flutter's ability to call native functions. That is, whether developing Flutter applications through the Flutter framework or running Flutter applications, the native functions can be implemented by executing the calling method code, and the native functions can be called locally or remotely. At the same time, the built-in functions of RPC technology are supported.
[0095] In this embodiment of the application, a method for calling native functions is provided. In this method, when a Flutter application requests to call a target native function of the native system, the calling method code encapsulated in the target interface of the remote procedure call layer is invoked to perform the following operations:
[0096] The current running mode of the Flutter application is detected, and the detection result is obtained. Based on the detection result, if it is determined that the Flutter application is currently in stand-alone running mode, the socket interface is called to establish a network channel. Through the network channel, the request data corresponding to the native function is sent to the server corresponding to the native system. The target function code corresponding to the target native function returned by the server is received and embedded into the Flutter application for execution.
[0097] The method call code is generated by the target code generator based on the method protocol defined in the interface file and the configuration parameters.
[0098] In this embodiment, when a Flutter application requests to call a target native function of the native system, the target interface of the remote procedure call layer is first called. Next, the calling method code encapsulated in the target interface is determined. Since the calling method code is generated by a target code generator, the writing of the calling method code is simplified, further reducing the cost associated with calling channels and interfacing protocols. Finally, based on the execution of the calling method code: the current running mode of the Flutter application is detected, and when it is determined that the Flutter application is currently in independent running mode, a socket interface is called to establish a network channel. Since the network channel is established when the Flutter application is running independently, communication between the Flutter application and the corresponding server of the native system is achieved through the network channel. Therefore, the request data corresponding to the native function is sent to the corresponding server of the native system through the network channel, and the target function code corresponding to the target native function returned by the server can be received. This target function code is then embedded into the Flutter application for execution, enabling the Flutter application to call the native function of the native system in independent running mode.
[0099] In one possible implementation, the hardware environment required for calling native functions provided in this application embodiment includes, but is not limited to: Macos, Linux, Windows, etc.
[0100] refer to Figure 1 , Figure 1 This is a schematic diagram illustrating an application scenario according to an embodiment of this application. The application scenario includes multiple terminal devices 101 and one server 102. The number of terminal devices 101 can be one or more, and the number of servers 102 can also be one or more. This application does not specifically limit the number of terminal devices 101 and servers 102.
[0101] In one alternative implementation, terminal device 101 and server 102 can communicate via a communication network. The communication network can be a wired network or a wireless network. Therefore, terminal device 101 and server 102 can be directly or indirectly connected via wired or wireless communication. For example, terminal device 101 can be indirectly connected to server 102 via a wireless access point, or terminal device 101 can be directly connected to server 102 via the Internet; this application does not impose any limitations on this.
[0102] In this embodiment, the terminal device 101 includes, but is not limited to, mobile phones, tablets, laptops, desktop computers, e-book readers, smart voice interaction devices, smart home appliances, in-vehicle terminals, and other devices. Various clients can be installed on the terminal device. The client can be an application (such as a browser, game software, etc.), or a webpage, a mini-program, etc., and the client can be a standalone Flutter application or a host application that embeds a Flutter application. The Flutter application is developed using the Flutter framework that embeds RPC technology.
[0103] Server 102 is the backend server corresponding to the client, providing services to the client. Server 102 can be a standalone physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, content delivery networks (CDN), and big data and artificial intelligence platforms.
[0104] Based on the above application scenarios, the following describes the method for calling native functions provided by the exemplary embodiments of this application, in conjunction with the application scenarios described above and with reference to the accompanying drawings. It should be noted that the above application scenarios are only shown to facilitate understanding of the spirit and principles of this application, and the embodiments of this application are not limited in any way in this respect.
[0105] refer to Figure 2 , Figure 2 An exemplary technical architecture for calling native functions is provided in this application embodiment; wherein, the technical architecture includes at least: an RPC layer and a runtime layer.
[0106] RPC layer: It is an abstraction of data processing within the Flutter framework and inherits some framework capabilities, such as name service, interceptors, timeout mechanism, etc.
[0107] In this embodiment, the RPC layer supports both Flutter clients and native system servers, and also supports RPC calls between Flutter applications and the native system. Specifically, an RPC call is a request from the Flutter client to send a target native function call to the native system server through the RPC layer, and a request to receive the target function code corresponding to the target native function returned by the native system server through the RPC layer.
[0108] Runtime layer: This is the runtime environment that Flutter applications rely on to call native functions of the native system, such as Chromium base, multithreading, and the basics of the buffer protocol (Protocol Buffer, Protobuf).
[0109] The Chromium base library includes some commonly used C++ utility classes, including container classes, smart pointers, callbacks, and threads. Due to the capabilities of the Chromium base library itself, the Runtime layer in this embodiment provides a stable runtime environment and a simple development environment for Flutter applications to call native functions of the native system.
[0110] Protobuf is a structured data serialization method; it boasts advantages such as support for multiple languages and operating platforms, high efficiency, good scalability, and good compatibility. In the embodiments of this application, the data encoding and decoding processes involved in RPC calls are all handled using Protobuf.
[0111] It should be noted that the Runtime layer proposed in this application embodiment also includes: GRPC, which is an open-source, high-performance, general-purpose RPC framework; this RPC framework is designed based on the HTTP / 2 protocol standard, and uses GRPC as a remote call service for communication between different applications.
[0112] based on Figure 2 The technical architecture for invoking native functions is illustrated in this application embodiment, which provides a method for invoking native functions. In this method, the Flutter client sends an RPC call request (RPCRequest) to the native system server and receives an RPC call response (RPCResponse) returned by the native system server to realize the invocation of native functions.
[0113] Specifically, taking the call to a HelloWorld program as an example, the Flutter application calls the native system's SayHello method, where SayHello is a native function invoked by the Flutter application. The process of the Flutter application calling the native function from the native system server is as follows: the Flutter client sends an RPC request (RPCRequest) to the native system server through a channel, and receives the RPC response (RPCResponse) returned by the native system server through the same channel. Furthermore, to achieve this, an RPC runtime environment is provided on the native system server to implement the RPC call and obtain the corresponding native function from the RPC request. (Reference) Figure 3 , Figure 3 An exemplary diagram illustrating the invocation of a native function is provided in an embodiment of this application.
[0114] Therefore, in this embodiment, calling native functions requires interaction and cooperation between the Flutter application and the native system. The methods for calling native functions from the Flutter application and the native system side are described in detail below.
[0115] Example 1: Flutter application side.
[0116] In this embodiment of the application, the Flutter application side corresponds to the Flutter client supported in the RPC layer, and the features included in the Flutter client include, but are not limited to: RPC client proxy, timeout control, and protocol encoding.
[0117] The RPC client proxy has the following functions:
[0118] Feature 1: It shields Flutter applications from the RPC call and protocol encoding / decoding process, meaning that Flutter applications do not need to understand the operation process of calling native functions, as well as the encoding / decoding operations involved.
[0119] Function 2: Implement the invoke method to realize the actual RPC request. In the Flutter framework of this application embodiment, the invoke method can simplify the code and improve code reusability.
[0120] Function 3: Implements a target interface for RPC calls, through which native function call requests sent by the Flutter application are received; at the same time, the target interface is used to process the data of the native function call requests sent by the Flutter application and obtain the binary data used in the data transmission process.
[0121] In business scenarios where a Flutter application calls native functions, the target interface (ChannelApi) corresponds to the Flutter application proxy interface (ClientStub) used for channel connection; specifically, the Flutter client creates a ClientStub class and calls native functions through ClientStub.
[0122] Taking a Flutter application calling a HelloWorld program through a channel as an example, the target interface is HelloWorldChannelApi, the Flutter application's proxy interface is WKChannelStub derived from ClientStub, and the implementation code corresponding to the channel call is:
[0123] final service=HelloWordChannelApi(WKChannelStub());
[0124] service.sayHello(request).
[0125] Specifically, the Flutter client receives native function call requests from the Flutter application by calling the Flutter application proxy interface in the remote procedure call layer. Upon receiving the request, the client determines that the Flutter application is requesting a native function from the native system. Then, it calls the call method code encapsulated in the Flutter application proxy interface. By executing this call method code, a connection channel is established between the Flutter client and the native system server. This channel facilitates the interaction between the Flutter client and the native system server, enabling the invocation of native functions. In other words, within the Flutter framework, native function calls can be achieved by executing the call method code and using the Flutter application proxy interface as a parameter.
[0126] In this embodiment of the application, the method calling code is encapsulated in the target interface of the RPC layer. The encapsulation of the interface supports more capabilities and reduces the repetitive work caused by writing method calling code.
[0127] Optionally, the method call code is generated by the target code generator in the Flutter framework based on the method protocol defined in the interface file and the configuration parameters. The target code generator is an RPC generation tool, the method protocol adopts the ProtoBuffer protocol, and the configuration parameters include: the method to be called and its input and output.
[0128] For example:
[0129] The defined interface definition file (IDL) is shown below:
[0130]
[0131] Based on the above interface file, the Flutter client's calling method code is generated using the target code generator, as shown below:
[0132]
[0133]
[0134] In this embodiment, the target code generator automatically generates calling method code based on the interface file, simplifying the operation of writing calling method code and further simplifying the ability of Flutter applications to call native functions; and through the method protocol defined in the interface file, the consistency between the generated Flutter client interface and the interfaces of various operating platforms is ensured, improving maintainability and ensuring data security; and the encoding and decoding of data is transparent to developers, improving development efficiency.
[0135] Optionally, a Flutter application can run as a standalone application or as a plugin embedded within a host application. Therefore, Flutter applications have two running modes: standalone and embedded. Standalone mode indicates that the Flutter application runs in its own process, while embedded mode indicates that the Flutter application and its host application run in the same process. Furthermore, the data transmission channels used when calling native functions differ depending on the Flutter application's running mode.
[0136] For example, when a Flutter application is running in standalone mode, it uses a network channel to send the request data corresponding to the target native function to the server corresponding to the native system when calling native functions; when a Flutter application is running in embedded mode, it uses a platform-specific channel defined by the Flutter framework to send the request data corresponding to the target native function to the server corresponding to the native system when calling native functions.
[0137] The following section provides a detailed explanation of how Flutter applications can invoke native functions when they are in different running modes.
[0138] Method 1: The Flutter application is in stand-alone running mode.
[0139] refer to Figure 4 , Figure 4 An exemplary embodiment of this application provides a method for invoking native functions. Specifically, when a Flutter application requests to invoke a target native function of the native system, the target interface of the remote procedure call layer is invoked, that is, the invocation method code encapsulated in the target interface is invoked, and the following steps are achieved by executing the invocation method code:
[0140] Step S400: Detect the current running mode of the Flutter application and obtain the detection results.
[0141] Specifically, the current running mode of a Flutter application is distinguished by its runtime environment. Within the Flutter framework, a remote environment (isRemote) can be defined using dart-define to determine whether a Flutter application has remote calls enabled. Whether a Flutter application has remote calls enabled determines its current running mode; therefore, in this embodiment, the detection result can be represented by whether the Flutter application has remote calls enabled.
[0142] For example: when a Flutter application is running on a host application, isRemote is false, and the current running mode of the Flutter application is embedded running mode; while when the Flutter application is running as a standalone application, isRemote is True, and the current running mode of the Flutter application is either embedded running mode or standalone running mode. Therefore, isRemote is false and isRemote is True to represent the detection results.
[0143] Step S401: Based on the detection results, if it is determined that the Flutter application is currently in stand-alone running mode, call the socket interface to establish a network channel.
[0144] Specifically, when the detection result is True for isRemote, it is determined that the Flutter application is currently in stand-alone running mode, and then a network channel is established through the socket interface.
[0145] refer to Figure 5 , Figure 5 An exemplary diagram of a Flutter application as a standalone application is provided in this application.
[0146] Step S402: Send the request data corresponding to the native function to the server corresponding to the native system through the network channel.
[0147] Specifically, through a network channel, based on the Hyper Text Transfer Protocol (HTTP), the request data corresponding to the native function is called to the logic layer (C++ layer) of the Flutter engine, and then the C++ layer calls the corresponding server of the native system (such as Android and iOS).
[0148] Step S403: Receive the target function code corresponding to the target native function returned by the server, and embed the target function code into the Flutter application for execution.
[0149] In this embodiment, when a Flutter application requests to call a target native function of the native system, if the Flutter application is currently in standalone running mode, a socket interface is called to establish a network channel. The request data corresponding to the target native function is then sent to the server of the native system via this network channel. The target function code returned by the server is then received via the network channel and embedded into the Flutter application for execution. When the Flutter application is running independently, the network channel is established to enable communication between the Flutter application and the server of the native system, allowing the Flutter application to obtain the target function code returned by the server and thus enabling the Flutter application to call the native function of the native system in standalone running mode.
[0150] Furthermore, based on the remote invocation method provided in the Flutter framework of this application embodiment, the development efficiency of Flutter applications is improved when developing Flutter applications using the Flutter framework. This improvement in development efficiency can be specifically reflected in the following aspects:
[0151] 1. Web-level development experience, allowing hybrid projects to run independently without relying on a host application, with a more stable and faster compilation and debugging process;
[0152] 2. The Channel / Dart2CPP interface enables remote testing, improving testing efficiency;
[0153] 3. When a Flutter application is a standalone application, it is not limited by the operating platform and can run on operating platforms such as Android, iOS, MAC, and PC;
[0154] 4. UI walkthroughs can be replicated one-to-one using simulators, improving walkthrough efficiency and development efficiency during the computer troubleshooting (Debug) phase.
[0155] Method 2: The Flutter application is in embedded running mode.
[0156] refer to Figure 6 , Figure 6 This application provides another example of a method for calling native functions. Specifically, when a Flutter application requests to call a target native function of the native system, it calls the target interface of the remote procedure call layer, that is, calls the calling method code encapsulated in the target interface, and executes the calling method code to achieve the following steps:
[0157] Step S600: Detect the current running mode of the Flutter application and obtain the detection results.
[0158] For details, please refer to step S400, which will not be repeated here.
[0159] Step S601: Based on the detection results, when it is determined that the Flutter application is currently in embedded running mode, call the specific platform channel defined by the Flutter framework.
[0160] Specifically, when the detection result is True for isRemote, it is determined that the Flutter application is currently in stand-alone running mode, and then the specific platform channel defined by the Flutter framework is directly called.
[0161] The embedded running mode indicates that the Flutter application and its host application are in the same process; that is, the Flutter application is a part of the host application. (Reference) Figure 7 , Figure 7 An exemplary diagram is provided in this application, showing a Flutter application as a part of a host application.
[0162] Step S602: Send the request data corresponding to the target native function to the server corresponding to the native system through a specific platform channel.
[0163] Specifically, through a specific platform channel, the request data corresponding to the native function is called to the logic layer (C++ layer) of the Flutter engine, and then the C++ layer calls the corresponding server of the native system (e.g., Android, iOS).
[0164] Step S603: Receive the target function code corresponding to the target native function returned by the server, and embed the target function code into the Flutter application for execution.
[0165] refer to Figure 8 Taking the SayHello method as an example, Figure 8 An exemplary schematic diagram of a Flutter application calling native functions is provided in an embodiment of this application.
[0166] In this embodiment, when a Flutter application requests to call a target native function of the native system, and the Flutter application is currently in embedded running mode, a specific platform channel defined by the Flutter framework is invoked. The request data corresponding to the target native function is sent to the server corresponding to the native system through this specific platform channel. Then, the target function code returned by the server is received through the specific platform channel, and the target function code is embedded into the Flutter application for execution. When the Flutter application is embedded in the host application and running, communication between the Flutter application and the server corresponding to the native system is directly achieved through the specific platform channel to obtain the target function code returned by the server, enabling the Flutter application to call the native function of the native system in embedded running mode.
[0167] Because data transmission and reception require a specific format, the data can only be successfully transmitted and received after being converted to a specific format.
[0168] Therefore, in this embodiment, when a Flutter application requests to call a target native function of the native system, firstly, the context information (Rpc Context) defined in the call request corresponding to the target native function is encapsulated to obtain the corresponding Language Request protocol; then, the Language Request protocol is serialized to obtain the corresponding binary data, and the binary data is used as the request data; finally, the request data corresponding to the target native function is sent to the native system server to ensure successful transmission of the request data and achieve the call to the target native function. The context information represents the context of the current request, including: the request service name, the request method name, the request identifier (id), and the timeout period.
[0169] Specifically, in converting the context information defined in the call request into binary format, this embodiment performs two serialization processes: First, the input content is serialized to obtain the corresponding initial binary data, where the input content is used to represent the target native function being called; then, the initial binary data and context information such as the request service name and request method name are serialized to obtain binary data used to represent the request data.
[0170] In this embodiment of the application, the context information defined in the call request is converted into binary format to be successfully transmitted to the native system server, further ensuring that the target function code of the corresponding target native function can be accurately received.
[0171] refer to Figure 9 , Figure 9An exemplary implementation of calling native functions is provided in this application, including the following steps:
[0172] In step S900, the Flutter client creates a ClientStub class and receives call requests through the ClientStub class.
[0173] Step S901: After receiving the call request, the Flutter client encapsulates the context information defined in the call request corresponding to the target native function to obtain the corresponding language request protocol.
[0174] That is, the request method name and request service name defined in the call request are encapsulated in a unified data structure.
[0175] In step S902, the Flutter client serializes the language request protocol to obtain the corresponding binary data.
[0176] Step S903: The Flutter client detects the current running mode of the Flutter application and determines whether the current running mode is an independent running mode. If so, proceed to step S904; otherwise, proceed to step S905.
[0177] In step S904, the Flutter client directly calls the specific platform channel defined by the Flutter framework to send binary data to the native system server via the Channel method.
[0178] In step S905, the Flutter client calls the socket interface to establish a network channel and sends binary data to the native system server via HTTP.
[0179] refer to Figure 10 , Figure 10 To be consistent with the embodiments of this application Figure 9 A corresponding diagram illustrating the process of a Flutter application calling a native function.
[0180] It should be noted that the target interface proposed in this application embodiment can also be reused in different business scenarios, in which case only the corresponding Stub interface needs to be implemented.
[0181] Specifically, in addition to the Flutter application proxy interface, the target interface of the remote procedure call layer also defines other proxy interfaces, and each other proxy interface corresponds to a business scenario.
[0182] For example: When calling C++, the Flutter application proxy interface is WKDartFFiStub, which is derived from ClientStub. The corresponding implementation code for calling C++ is:
[0183] final service=HelloWordChannelApi(WKDartFFiStub());
[0184] service.sayHello(request).
[0185] Through the examples above, the target interface can enable Dart to call C++, achieving the effect of unified API interface.
[0186] In this embodiment of the application, different Stub interfaces are implemented for the target interface of the RPC layer to extend to different business scenarios and realize the extensibility of the Flutter framework.
[0187] When the Flutter client sends the request data corresponding to the target native function to the server corresponding to the native system, and receives the target function code corresponding to the target native function returned by the server, the server corresponding to the native system will inevitably receive the request data sent by the Flutter client and determine the target function code corresponding to the target native function based on the request data. Therefore, this application embodiment also provides a method for calling native functions on the native system side.
[0188] Example 2: Native System Side.
[0189] In this embodiment, the native system side corresponds to the native system server supported in the RPC layer, and the native system server implements methods for calling native functions through stub code; wherein, the stub code is generated by the target code generator, that is, in addition to generating the calling method code of the Flutter client, the target code generator also generates the stub code of the server. Moreover, the generated stub code is different for different native system servers.
[0190] For example, taking the call to the HelloWorld program as an example, the stub code for the server-side of Android, iOS, macOS, and PC systems are as follows:
[0191]
[0192] It should be noted that the server only needs to inherit and implement its interface.
[0193] refer to Figure 11 , Figure 11 An exemplary embodiment of this application provides a method for invoking native functions, namely, invoking stub code, and implementing the following steps by executing the stub code:
[0194] Step S1100: When request data is detected, determine the target native function corresponding to the request data and obtain the target function code corresponding to the target native function.
[0195] Specifically, during the startup process, the Flutter Engine in the Flutter framework initializes the native system server and creates and initializes channels. The channels monitor request data in real time, and the request data is used to represent the target native functions that the Flutter application requests to call from the native system.
[0196] Optionally, channels include network channels and platform-specific channels defined by the Flutter framework. However, regardless of whether the request data is detected through a network channel or a platform-specific channel, it is necessary to determine the target native function corresponding to the request data and obtain the target function code corresponding to the target native function.
[0197] Step S1101: Return the target function code to the Flutter application so that the target function code can be embedded into the Flutter application for execution.
[0198] Since request data is monitored through different channels, when request data is detected through one channel, in order to ensure that the target function code corresponding to the request data is sent smoothly, the same channel used when receiving the request data must be used to return the target function code to the Flutter client, that is, to return the target function code to the Flutter application.
[0199] Optionally, if requested data is detected through the network channel, the target function code is returned to the Flutter application via the network channel; specifically, the target function code is returned to the Flutter client via the network channel using HTTP.
[0200] In this embodiment, when the native system server detects a data request through a network channel, it first determines the target native function corresponding to the requested data and obtains the target function code corresponding to the target native function. Then, it returns the target function code to the Flutter application through the network channel so that the target function code can be embedded into the Flutter application for execution. Since the Flutter application uses a network channel to send data requests in standalone running mode, detecting a data request through the network channel and returning the target function code corresponding to the data request to the Flutter application enables the Flutter application to call the native functions of the native system in standalone running mode, and simplifies the ability to call native functions.
[0201] Optionally, if requested data is detected through a specific platform channel defined by the Flutter framework, the target function code is returned to the Flutter application through the specific platform channel; specifically, the target function code is returned to the Flutter client through the specific platform channel using the Channel method.
[0202] In this embodiment, when the native system server detects a data request through a specific platform channel, it first determines the target native function corresponding to the requested data and obtains the target function code corresponding to the target native function. Then, it returns the target function code to the Flutter application through the specific platform channel to embed the target function code into the Flutter application for execution. Since the Flutter application uses a specific platform channel to send data requests in embedded running mode, and the Flutter application and its host application are in the same process in embedded running mode, detecting a data request through a specific platform channel and returning the target function code corresponding to the data request to the Flutter application enables the Flutter application to call the native functions of the native system in embedded running mode, and simplifies the ability to call native functions.
[0203] Since the monitored request data is serialized binary data, when the request data is received, the request data is first decoded, then the context information of the current request is obtained from the decoded data, and finally, the target function code used to implement the target native function is obtained based on the context information.
[0204] Optionally, the binary data is first decoded to determine the corresponding Language Request protocol; then, the Language Request protocol is constructed to obtain the context information of the current request; next, the corresponding request service name is obtained from the context information, and the target service is found by hashing based on the request service name.
[0205] Specifically, in this embodiment, the native system server internally maintains a key-value (kv) structure for all services, where k represents the service name and v represents the specific implementation of the service, i.e., v represents the service corresponding to the service name. Therefore, after obtaining the corresponding request service name from the context information, the target service corresponding to the request service name can be found in the kv structure through a hash lookup based on the request service name.
[0206] After locating the target service, determine all method proxies (MethodAdapter) of the target service; then, based on the request method name in the context information, determine the target method proxy corresponding to the request method name among all method proxies, and call the method executor (MethodHandler) stored in the target method proxy to obtain the target function code used to implement the target native function.
[0207] Specifically, in this embodiment, the native system server also provides a ServiceAdapter function. This ServiceAdapter stores all method proxies for the current service, provides specific names for the service methods, and offers the ability to find method proxies externally. Furthermore, each method proxy stores the method executor and the message codec. Therefore, in this embodiment, the ability to find method proxies externally can be used to determine the target method proxy based on the request method name, and the method executor and codec stored in the target method proxy can be identified.
[0208] Since the request data is binary data that has undergone secondary serialization, after decoding the binary data and determining the corresponding Language Request protocol, the context information and the initial binary data corresponding to the input content can be obtained from the Language Request protocol. The input content is used to represent the target native function being called.
[0209] At this point, to obtain the output content corresponding to the input content, i.e., the target function code of the target native function, it is necessary to determine the target native function. Therefore, it is necessary to decode the initial binary data corresponding to the input content to obtain the corresponding target native function, and based on the target native function, obtain the target function code used to implement the target native function.
[0210] Specifically, after determining the target method proxy corresponding to the request method name, the codec stored in the target method proxy is called to decode the initial binary data corresponding to the input content and obtain the target native function of the response; then, the execution method of the method executor stored in the target method proxy is called to obtain the target function code used to implement the target native function.
[0211] In this embodiment, after the native system server obtains the binary data, it first decodes the binary data to determine the corresponding language request protocol and obtains the context information in the request data from the language request protocol. Then, based on the request service name in the context information, it determines the target service corresponding to the request service name, and based on the request method name in the context information, it determines the target method proxy in the target service corresponding to the request method name, and calls the method executor stored in the target method proxy to obtain the target function code used to implement the target native function. This can accurately obtain the target function code of the target native function and ensure the accuracy of the transmitted data.
[0212] It should be noted that, in order to meet data transmission requirements, in this embodiment, after obtaining the target function code corresponding to the target native function, the target function code will be encoded to obtain corresponding encoded data. Then, the encoded data will be returned to the Flutter application according to the channel through which the requested data was detected. For details, please refer to... Figure 12 and reference Figure 13 Provides methods for calling native functions from the native system side.
[0213] refer to Figure 12 , Figure 12 An exemplary embodiment of this application provides a method for calling native functions from the native system side. This method corresponds to a scenario where the Flutter application is in embedded running mode, and includes the following steps:
[0214] In step S1200, the native system server detects the binary data used to represent the request data through a specific platform channel defined by the Flutter framework.
[0215] Step S1201: The native system server decodes the binary data to obtain the language request protocol;
[0216] Step S1202: The native system server obtains the context information of the current request from the language request protocol;
[0217] Step S1203: The native system server searches for the corresponding target service based on the requested service name in the context information;
[0218] Step S1204: Based on the request method name in the context information, the native system server searches for the corresponding target method proxy among all method proxies of the target service.
[0219] Step S1205: The native system server decodes the binary data used to represent the input content obtained from the language request protocol based on the codec stored in the target method proxy, and determines the target native function corresponding to the input content.
[0220] Step S1206: The native system server obtains the target function code corresponding to the target native function based on the method executor stored in the target method proxy;
[0221] In step S1207, the native system server encodes the target function code and returns the encoded data to the Flutter application via a specific platform channel using the Channel method.
[0222] refer to Figure 13 , Figure 13 This application provides an exemplary embodiment of another method for native system-side invocation of native functions, which corresponds to a scenario where the Flutter application is in stand-alone running mode, and includes the following steps:
[0223] In step S1300, the native system server detects the binary data used to represent the request data through the network channel;
[0224] Step S1301: The native system server decodes the binary data to obtain the language request protocol;
[0225] Step S1302: The native system server obtains the context information of the current request from the language request protocol;
[0226] Step S1303: The native system server searches for the corresponding target service based on the requested service name in the context information;
[0227] Step S1304: Based on the request method name in the context information, the native system server searches for the corresponding target method proxy among all method proxies of the target service.
[0228] Step S1305: The native system server decodes the binary data used to represent the input content obtained from the language request protocol based on the codec stored in the target method proxy to determine the target native function corresponding to the input content.
[0229] Step S1306: The native system server obtains the target function code corresponding to the target native function based on the method executor stored in the target method proxy;
[0230] In step S1307, the native system server encodes the target function code and returns the encoded data to the Flutter application via the network channel using HTTP.
[0231] refer to Figure 14 , Figure 14 To be consistent with the embodiments of this application Figure 12 and Figure 13A diagram illustrating the corresponding data processing procedure where the native system calls native functions.
[0232] In one possible implementation, services are determined through the RegistryService method provided by the native system server; this RegistryService method is mainly used to add services, and the native system server is created during the Flutter engine startup process.
[0233] In this embodiment, the target code generator provides a specific name for each service, and then registers the service name and its implementation class with the native system server. When the native system server obtains or creates a service, it initializes the service's method proxy (MethodHandler) and stores the method proxy and the service name in the native system server, specifically in the service's internal proxy class. (See reference...) Figure 15 , Figure 15 An exemplary method for service registration is provided in an embodiment of this application.
[0234] In this embodiment of the application, the native system server provides a service registration function so that when the requested service name is determined, the corresponding method proxy can be found based on the requested service name, and the function code of the target native function can be obtained more accurately.
[0235] In this embodiment of the application, the native system server also provides an interceptor function through aspect-oriented programming. Through this interceptor function, log reporting and marking can be performed on each path involved in the RPC call process. In addition to logging and marking during the RPC call process, this interceptor function can also log and mark message requests on the server side for other business scenarios.
[0236] In this application embodiment, an efficient ability of the Flutter framework to call native functions is provided, which simplifies the compilation of functional code, improves efficiency, and maintains the consistency between the Flutter client and the interfaces of various operating platforms through protocols, ensuring data security. At the same time, native function calls can be implemented in both Flutter standalone and embedded running modes, and the target interfaces in the Flutter framework can be extended to other business scenarios to achieve functional expansion.
[0237] Based on the same inventive concept as the above-described method embodiments of this application, this application also provides a device for calling native functions. The principle of the device in solving the problem is similar to the above-described implementation method on the Flutter application side. Therefore, the implementation of the device can refer to the implementation of the above-described method on the Flutter application side, and the repeated parts will not be described again.
[0238] refer to Figure 16 , Figure 16 An exemplary embodiment of this application provides a device 160 for invoking native functions. The device 160 for invoking native functions includes an execution unit 1600, wherein the execution unit includes a detection unit 1601, an establishment unit 1602, a sending unit 1603, and a receiving unit 1604.
[0239] Execution unit 1600 is used to call the target interface of the remote procedure call layer when a Flutter application requests to call a target native function of the native system, and to perform the following operations:
[0240] The detection unit 1601 detects the current running mode of the Flutter application and obtains the detection results.
[0241] By establishing unit 1602, based on the detection results, when it is determined that the Flutter application is currently in stand-alone running mode, the socket interface is called to establish a network channel;
[0242] The sending unit 1603 sends the request data corresponding to the target native function to the corresponding server of the native system through the network channel.
[0243] The receiving unit 1604 receives the target function code corresponding to the target native function returned by the server and embeds the target function code into the Flutter application for execution.
[0244] Optionally, after obtaining the detection result through the detection unit 1601 and before receiving the target function code corresponding to the target native function returned by the server through the receiving unit, it is also used for:
[0245] Based on the detection results, when it is determined that the Flutter application is currently in embedded running mode, a specific platform channel defined by the Flutter framework is invoked to send the request data corresponding to the target native function to the server of the native system; whereby, embedded running mode is used to indicate that the Flutter application and the current host application are in the same process.
[0246] Optionally, after invoking the target interface of the remote procedure call layer, and before sending the request data corresponding to the target native function to the native system server via the sending unit 1603, it is also used for:
[0247] The context information defined in the call request corresponding to the target native function is encapsulated to obtain the corresponding language request protocol; wherein, the context information includes at least one of the following: request service name, request method name, and request identifier;
[0248] The language request protocol is serialized to obtain the corresponding binary data, and the binary data is used as the request data.
[0249] Optionally, for the target interface applied to the Flutter application proxy interface of the channel connection, the target native function of the native system requested by the Flutter application is determined in the following way:
[0250] When a native function call request is received from a Flutter application via the Flutter application proxy interface, it is determined that the Flutter application is requesting to call a native function of the native system.
[0251] Optionally, the remote procedure call layer may also include:
[0252] Define other proxy interfaces besides the Flutter application proxy interface, and each other proxy interface corresponds to a business scenario.
[0253] Optionally, execution unit 1600 calls the target interface of the remote procedure call layer, specifically for:
[0254] The method call code is encapsulated in the target interface of the remote procedure call layer; the method call code is generated by the target code generator based on the method protocol defined in the interface file and the configuration parameters.
[0255] Based on the same inventive concept as the above-described method embodiments of this application, this application also provides another device for calling native functions. The principle of the device in solving the problem is similar to the implementation method on the native system side described above. Therefore, the implementation of the device can refer to the implementation of the method on the native system side described above. Repeated parts will not be described again.
[0256] refer to Figure 17 , Figure 17 An exemplary embodiment of this application provides a device 170 for invoking native functions, which includes: a monitoring unit 1701 and a return unit 1702;
[0257] The monitoring unit 1701 is used to determine the target native function corresponding to the request data when the request data is detected, and to obtain the target function code corresponding to the target native function; wherein, the request data is obtained by monitoring the network channel and is used to characterize the Flutter application requesting to call the target native function of the native system;
[0258] Return unit 1702 is used to return the target function code to the Flutter application so that the target function code can be embedded into the Flutter application for execution.
[0259] Optionally, the monitoring unit 1701 is specifically used for:
[0260] Decode the request data to obtain the context information within it;
[0261] Based on the requested service name in the context information, determine the target service corresponding to the requested service name;
[0262] Based on the request method name in the context information, determine the target method proxy in the target service that corresponds to the request method name, and call the method executor stored in the target method proxy to obtain the target function code used to implement the target native function.
[0263] Optionally, the requested data is binary data; the monitoring unit 1701 is specifically used for:
[0264] Decode the binary data to determine the corresponding language request protocol;
[0265] The language request protocol is constructed and processed to obtain context information from the request data.
[0266] Optionally, return unit 1702 is specifically used for:
[0267] The target function code is returned to the Flutter application via a network channel.
[0268] Optionally, the target service is configured as follows:
[0269] When starting the Flutter framework engine, obtain the native system server;
[0270] When the target service is obtained, the method proxy of the target service is initialized, and the method proxy and the service name of the target service are stored on the server.
[0271] Optionally, the monitoring unit 1701 is also used to: monitor request data through a specific platform channel defined by the Flutter framework;
[0272] Return unit 1702 is also used to: return the target function code to the Flutter application through a specific platform channel.
[0273] Based on the same inventive concept as the method embodiments described above, this application also provides an electronic device, which can be a terminal device. In this embodiment, the structure of the electronic device can be as follows: Figure 18 As shown, it includes components such as: communication component 1810, memory 1820, display unit 1830, camera 1840, sensor 1850, audio circuit 1860, Bluetooth module 1870, processor 1880, etc.
[0274] The communication component 1810 is used to communicate with the server. In some embodiments, it may include a Circuit-Based Wireless Fidelity (WiFi) module, which is a short-range wireless transmission technology. Electronic devices can use the WiFi module to help users send and receive information.
[0275] The memory 1820 can be used to store software programs and data. The processor 1880 executes various functions of the terminal device 101 and performs data processing by running the software programs or data stored in the memory 1820. The memory 1820 may include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device, flash memory device, or other volatile solid-state storage device. The memory 1820 stores the operating system that enables the terminal device 101 to run. In this application, the memory 1820 may store the operating system and various applications, and may also store code that executes the native function methods called in the embodiments of this application.
[0276] The display unit 1830 can also be used to display information input by the user or information provided to the user, as well as various menus of the terminal device 101, in a graphical user interface (GUI). Specifically, the display unit 1830 may include a display screen 1832 disposed on the front of the terminal device 101. The display screen 1832 may be configured as a liquid crystal display, a light-emitting diode, or the like. The display unit 1830 can be used to display pages of the Flutter application in this embodiment.
[0277] The display unit 1830 can also be used to respond to input digital or character information and generate signal inputs related to user settings and function control of the terminal device 101. Specifically, the display unit 1830 may include a touch screen 1831 disposed on the front of the terminal device 101, which can collect touch operations of the user on or near it, such as clicking a button, dragging a scroll box, etc.
[0278] The touchscreen 1831 can be placed over the display screen 1832, or the touchscreen 1831 and the display screen 1832 can be integrated to realize the input and output functions of the terminal device 101. After integration, it can be referred to as a touch display screen. In this application, the display unit 1830 can display the application program and the corresponding operation steps.
[0279] Camera 1840 can be used to capture still images, which users can then post comments on via the application. There can be one or multiple cameras 1840. An object is projected onto a photosensitive element through a lens, generating an optical image. This photosensitive element can be a charge-coupled device (CCD) or a complementary metal-oxide-semiconductor (CMOS) phototransistor. The photosensitive element converts the light signal into an electrical signal, which is then transmitted to the processor 1880 to be converted into a digital image signal.
[0280] The terminal device may also include at least one sensor 1850, such as an accelerometer 1851, a proximity sensor 1852, a fingerprint sensor 1853, and a temperature sensor 1854. The terminal device may also be equipped with other sensors such as a gyroscope, barometer, hygrometer, thermometer, infrared sensor, light sensor, and motion sensor.
[0281] Audio circuitry 1860, speaker 1861, and microphone 1862 provide an audio interface between the user and terminal device 101. Audio circuitry 1860 converts received audio data into an electrical signal and transmits it to speaker 1861, where it is converted into a sound signal for output. Terminal device 101 may also be equipped with volume buttons for adjusting the volume of the sound signal. On the other hand, microphone 1862 converts the collected sound signal into an electrical signal, which is then converted back into audio data by audio circuitry 1860. This audio data is then output to communication component 1810 for transmission to, for example, another terminal device 101, or to memory 1820 for further processing.
[0282] The Bluetooth module 1870 is used to interact with other Bluetooth devices that also have a Bluetooth module via the Bluetooth protocol. For example, a terminal device can establish a Bluetooth connection with a wearable electronic device (such as a smartwatch) that also has a Bluetooth module through the Bluetooth module 1870, thereby exchanging data.
[0283] The processor 1880 is the control center of the terminal device, connecting various parts of the terminal through various interfaces and lines. It executes various functions and processes data by running or executing software programs stored in the memory 1820 and calling data stored in the memory 1820. In some embodiments, the processor 1880 may include one or more processing units; the processor 1880 may also integrate an application processor and a baseband processor, wherein the application processor mainly handles the operating system, user interface, and applications, and the baseband processor mainly handles wireless communication. It is understood that the baseband processor may not be integrated into the processor 1880. In this application, the processor 1880 can run the operating system, applications, user interface display and touch response, as well as the native function invocation method of this embodiment. Furthermore, the processor 1880 is coupled to the display unit 1830.
[0284] In some possible implementations, various aspects of the method for invoking native functions provided in this application can also be implemented in the form of a program product, which includes a computer program that, when the program product is run on an electronic device, causes the electronic device to perform the steps in the method for invoking native functions according to the various exemplary embodiments of this application described above.
[0285] The program product may employ any combination of one or more readable media. A readable medium may be a readable signal medium or a readable storage medium. A readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples (a non-exhaustive list) of readable storage media include: electrical connections having one or more wires, portable disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof.
[0286] The program product of the embodiments of this application may employ a portable compact disc read-only memory (CD-ROM) and include a computer program, and may run on a computing device. However, the program product of this application is not limited thereto. In this document, the readable storage medium may be any tangible medium that contains or stores a program that may be used by or in conjunction with a command execution system, apparatus, or device.
[0287] A readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, carrying a readable computer program. This propagated data signal may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A readable signal medium may also be any readable medium other than a readable storage medium, capable of sending, propagating, or transmitting a program for use by or in conjunction with a command execution system, apparatus, or device.
[0288] Computer programs contained on readable media can be transmitted using any suitable medium, including but not limited to wireless, wired, optical fiber, RF, etc., or any suitable combination thereof.
[0289] It should be noted that although several units or sub-units of the device have been mentioned in the detailed description above, this division is merely exemplary and not mandatory. In fact, according to embodiments of this application, the features and functions of two or more units described above can be embodied in one unit. Conversely, the features and functions of one unit described above can be further divided and embodied by multiple units.
[0290] Furthermore, although the operations of the method of this application are described in a specific order in the accompanying drawings, this does not require or imply that these operations must be performed in that specific order, or that all the operations shown must be performed to achieve the desired result. Additionally or alternatively, certain steps may be omitted, multiple steps may be combined into one step, and / or one step may be broken down into multiple steps.
[0291] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing a computer-usable computer program.
[0292] Although preferred embodiments of this application have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments as well as all changes and modifications falling within the scope of this application.
[0293] Obviously, those skilled in the art can make various modifications and variations to this application without departing from the spirit and scope of this application. Therefore, if such modifications and variations fall within the scope of the claims of this application and their equivalents, this application also intends to include such modifications and variations.
Claims
1. A method for invoking native functions, characterized in that, The method, applied to the Flutter application side supported by the Remote Procedure Call (RPC) layer in the Flutter framework, includes: When a Flutter application requests to call a target native function of the native system, it invokes the calling method code encapsulated in the target interface of the remote procedure call layer to perform the following operations: Before detecting the current running mode of the Flutter application, the context information defined in the call request corresponding to the target native function is encapsulated to obtain the corresponding language request protocol; wherein, the context information includes: request service name, request method name and request identifier; and the language request protocol is serialized to obtain the corresponding binary data, and the binary data is used as the request data corresponding to the target native function; Based on the runtime environment of the Flutter application, the current running mode of the Flutter application is detected, and the detection result is obtained; the runtime environment is used to indicate whether the Flutter application enables remote calls; When it is determined that the Flutter application is currently in standalone running mode based on the detection results, a socket interface is invoked to establish a network channel. Through the network channel, the request data corresponding to the target native function is invoked to the logic layer of the Flutter engine based on the Hypertext Transfer Protocol. The logic layer then sends the request data to the server corresponding to the native system. The standalone running mode is used to indicate that the Flutter application itself corresponds to a process. When it is determined that the Flutter application is currently in embedded running mode based on the detection results, the request number corresponding to the target native function is called to the logic layer of the Flutter engine through a specific platform channel defined by the Flutter framework, based on the Channel method. The logic layer then sends the request data to the server corresponding to the native system. The embedded running mode is used to indicate that the Flutter application and the current host application are in the same process. Receive the target function code corresponding to the target native function returned by the server, and embed the target function code into the Flutter application for execution; The method call code is generated by the target code generator based on the ProtoBuffer protocol defined in the interface file, as well as the method name, method parameters, and input / output.
2. The method as described in claim 1, characterized in that, The step of serializing the language request protocol to obtain the corresponding binary data includes: The input content used to characterize the invoked target native function is serialized to obtain initial binary data; The initial binary data and the context information are serialized to obtain the binary data.
3. The method as described in claim 1, characterized in that, The target interface is a Flutter application proxy interface applied to the channel connection; the target native function of the native system requested by the Flutter application is determined in the following way: When the Flutter application proxy interface receives a native function call request sent by the Flutter application, it determines that the Flutter application is requesting to call the native function of the native system.
4. The method as described in claim 1, characterized in that, The remote procedure call layer also includes: Other proxy interfaces besides the Flutter application proxy interface are defined, and each of the other proxy interfaces corresponds to a business scenario.
5. A method for invoking native functions, characterized in that, The method, applied to the native system side supported by the RPC layer in the Flutter framework, includes: Call the stub code, and by executing the stub code, achieve the following operations: When a network channel request is detected in the logic layer of the Flutter engine, the target native function corresponding to the request data is determined. Then, using the network channel and the Hypertext Transfer Protocol (HTTP), the target function code corresponding to the obtained target native function is returned to the Flutter application to embed the target function code into the Flutter application for execution. The network channel is established by calling a socket interface based on the Flutter application's runtime environment, specifically when the Flutter application is currently in standalone running mode. The runtime environment indicates whether remote calls are enabled in the Flutter application. The standalone running mode indicates that the Flutter application itself corresponds to a single process. When a request for data transmitted through a specific platform channel defined by the Flutter framework is detected in the logic layer of the Flutter engine, the target native function corresponding to the request data is determined. Then, through the specific platform channel, the target function code corresponding to the target native function is returned to the Flutter application in a Channel-based manner, so that the target function code is embedded into the Flutter application for execution. The specific platform channel is invoked based on the Flutter application's runtime environment, specifically when the Flutter application is currently in embedded running mode. The embedded running mode indicates that the Flutter application and its current host application are in the same process. The native system-side server, the network channel, and the platform-specific channel are all created and initialized during the startup of the Flutter framework engine. The stub code is generated by the target code generator for the native system side, and different stub code is generated for different native system sides; the request data is used to characterize the Flutter application's request to call the target native function of the native system.
6. The method as described in claim 5, characterized in that, Upon detecting requested data, the target function code corresponding to the target native function is obtained, including: The request data is decoded to obtain the context information in the request data; Based on the request service name in the context information, determine the target service corresponding to the request service name; Based on the request method name in the context information, the target method proxy corresponding to the request method name in the target service is determined, and the method executor stored in the target method proxy is called to obtain the target function code used to implement the target native function.
7. The method as described in claim 6, characterized in that, The request data is binary data; therefore, the decoding process of the request data to obtain the context information in the request data includes: The binary data is decoded to determine the corresponding language request protocol; The language request protocol is constructed and processed to obtain the context information in the request data.
8. The method as described in claim 6, characterized in that, The target service is configured as follows: When the target service is obtained, the method proxy of the target service is initialized, and the method proxy and the service name of the target service are stored in the server.
9. A device for invoking native functions, characterized in that, The device, applied to the Flutter application side supported by the RPC layer in the Flutter framework, includes an execution unit, wherein the execution unit includes a detection unit, an establishment unit, a sending unit, and a receiving unit; The execution unit is used to call the calling method code encapsulated in the target interface of the remote procedure call layer when the Flutter application requests to call the target native function of the native system, and to perform the following operations: Before detecting the current running mode of the Flutter application through the detection unit, the context information defined in the call request corresponding to the target native function is encapsulated to obtain the corresponding language request protocol; wherein, the context information includes: request service name, request method name and request identifier; and the language request protocol is serialized to obtain the corresponding binary data, and the binary data is used as the request data corresponding to the target native function; The detection unit detects the current running mode of the Flutter application based on its runtime environment and obtains the detection result; the runtime environment is used to indicate whether the Flutter application enables remote calls. When the establishment unit determines that the Flutter application is currently in an independent running mode based on the detection result, it calls the socket interface to establish a network channel. Then, through the sending unit, it uses the network channel to send the request data corresponding to the target native function to the logic layer of the Flutter engine via the Hypertext Transfer Protocol. The logic layer then sends the request data to the server corresponding to the native system. The independent running mode indicates that the Flutter application itself corresponds to a single process. When the detection result indicates that the Flutter application is currently in embedded running mode, the sending unit, through a specific platform channel defined by the Flutter framework, invokes the request number corresponding to the target native function to the logic layer of the Flutter engine, and the logic layer sends the request data to the server corresponding to the native system; wherein, the embedded running mode is used to indicate that the Flutter application and the currently host application are in the same process; The receiving unit receives the target function code corresponding to the target native function returned by the server, and embeds the target function code into the Flutter application for execution. The method call code is generated by the target code generator based on the ProtoBuffer protocol defined in the interface file, as well as the method name, method parameters, and input / output.
10. A device for invoking native functions, characterized in that, Applied to the native system side supported by the RPC layer in the Flutter framework, the device includes: a monitoring unit and a return unit; and calls stub code to perform the following operations by executing the stub code: The monitoring unit is used to detect request data transmitted through a network channel in the logic layer of the Flutter engine, determine the target native function corresponding to the request data, and then, through the return unit, return the target function code corresponding to the target native function to the Flutter application via the network channel using the Hypertext Transfer Protocol, so as to embed the target function code into the Flutter application for execution. The network channel is established by calling a socket interface based on the Flutter application's runtime environment, determining that the Flutter application is currently in standalone running mode. The runtime environment is used to indicate whether the Flutter application enables remote calls; the standalone running mode indicates that the Flutter application itself corresponds to a single process. The monitoring unit is used to detect when the logic layer of the Flutter engine has request data transmitted through a specific platform channel defined by the Flutter framework, determine the target native function corresponding to the request data, and then, through the return unit, return the target function code corresponding to the target native function to the Flutter application via the specific platform channel using a Channel method, so as to embed the target function code into the Flutter application for execution; wherein, the specific platform channel is invoked based on the runtime environment of the Flutter application when it is determined that the Flutter application is currently in embedded running mode; the embedded running mode is used to indicate that the Flutter application and the currently host application are in the same process; The native system-side server, the network channel, and the platform-specific channel are all created and initialized during the startup of the Flutter framework engine. The stub code is generated by the target code generator for the native system side, and different stub code is generated for different native system sides; the request data is used to characterize the Flutter application's request to call the target native function of the native system.
11. An electronic device, characterized in that, The electronic device includes: a memory and a processor, wherein: The memory is used to store computer programs; The processor is configured to execute the computer program to implement the steps of the method according to any one of claims 1-4 or claims 5-8.
12. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the steps of the method according to any one of claims 1-4 or claims 5-8.
13. A computer program product, characterized in that, It includes computer instructions that, when executed by a processor, implement the steps of the method described in any one of claims 1-4 or 5-8.
Citation Information
Patent Citations
Picture uploading method and device, computer equipment and storage medium
CN109710337A
Flutter-based page generation method and device, equipment and storage medium
CN113342447A