Android mechanism description
1. Why does the Android-side SDK use multi-processing to implement?
The Android side runtime SDK is implemented using the multi-process mechanism. By multi-process, we mean that the applet is separated from the host app, each running in a separate process, and the processes do not interfere with each other and pass data to each other through cross-process communication. The main reasons why multi-processing was chosen were as follows.
- not to occupy the memory of the host app. 1. the system allocates separate memory space for the applet processes, and the applets do not occupy the memory of the host process, so the App does not have to worry about problems such as memory overflow.
- ensure the safe and stable operation of the host App. Even if the applet process crashes, the host app can still continue to run normally.
- Easier data decoupling. The memory between processes is naturally isolated, so data in one process will never be read or written directly by another process unless by means of cross-process communication, so there is no need to worry about data coupling.
2. How is multi-processing implemented?
Because Android does not provide API for dynamic process creation, we use the way of binding process for Activity to achieve process creation, that is, specify android:process property for Activity in AndroidManifest.xml, when Activity is started for the first time, the system will create a new process and place the stack where the Activity is located inside this process.
- To enhance the user experience, it is necessary to display different applets that are open at the same time in different locations on the system's recent taskbar, so that the user can clearly see which applets are currently open and switch between them at will. To achieve this effect, you need to set the
android:taskAffinityproperty for Activity inAndroidManifest.xmlto press the Activity of different applet processes into different task stacks.
3. How are applet processes managed?
- the SDK supports the creation of up to 5 applet processes at the same time. when the number of applet processes does not exceed 5, a new process will be created for each new applet launched. when the maximum number of applets is reached, when a new applet is launched, the existing applet process will be reused to open the applet.
- when opening the same applet, it will first check if the applet has been started, and if so, it will directly invoke the corresponding process.
- the logic for creating, reusing and calling up applet processes is done in the main process, which dispatches them uniformly.
4. How do applet processes and host processes communicate with each other?
Two-way communication between the applet process and the host process is achieved through AIDL. The applet process and the host process bind to the same remote service, obtain the proxy object of the service through the Binder mechanism, and call the AIDL interface through the proxy object to achieve cross-process communication.
5. How are process safety and thread safety issues handled?
- At present, the resources that need to be written during the running of the applet are stored in the corresponding directory of the applet in the application sandbox, and there is no write operation to the global shared resources, so there is no problem of competition between processes for the same system resources. For possible future process security issues, process-safe ways to read and write data will be used during development, such as using file locks to access files, sharing data through ContentProvider instead of SharedPreferences, etc.
- There is no direct thread-safety problem between processes, only when different applet processes access the memory of the main process concurrently through cross-process communication, or when processes read and write memory concurrently internally, the SDK solves thread-safety related problems in the middle by means of locks, synchronous methods, java concurrent class sets, etc.
*** Translated with www.DeepL.com/Translator (free version) ***