# Android Mechanism Explanation
# 1. Why does the Android SDK use multi-process implementation?
The Android Runtime SDK adopts a multi-process mechanism, meaning Mini-Programs and the Host-App run separately in independent processes that do not interfere with each other, communicating via inter-process communication to transfer data. The main reasons for choosing multi-process are as follows:
- Does not occupy the Host-App's memory. The system allocates independent memory space for the Mini-Program process, so Mini-Programs do not consume the main process's memory, preventing issues like memory overflow in the App.
- Ensures secure and stable operation of the Host-App. Any exceptions occurring in the Mini-Program process will not affect the Host-App; even if the Mini-Program process crashes, the Host-App can continue to run normally.
- Facilitates data decoupling. Memory is naturally isolated between processes. Unless inter-process communication is used, data in one process cannot be directly read or written by another process, eliminating concerns about data coupling.
# 2. How is multi-process implemented?
- Since Android does not provide an API for dynamically creating processes, process creation is achieved by binding an Activity to a process. This is done by specifying the
android:processattribute for the Activity inAndroidManifest.xml. When the Activity is launched for the first time, the system creates a new process and places the Activity's stack within it. - To enhance the user experience, different Mini-Programs opened simultaneously need to be displayed in separate positions in the system's recent tasks bar, allowing users to clearly see which Mini-Programs are currently open and switch between them freely. To achieve this effect, the
android:taskAffinityattribute is set for the Activity inAndroidManifest.xml, pushing the Activities of different Mini-Program processes into separate task stacks.
# 3. How are Mini-Program processes managed?
- The SDK supports creating up to 5 Mini-Program processes simultaneously. When the number of Mini-Program processes is below 5, launching a new Mini-Program creates a new process. Once the maximum number is reached, new Mini-Programs will reuse existing Mini-Program processes.
- When opening the same Mini-Program, the system first checks if it is already running. If so, the corresponding process is directly activated.
- The logic for creating, reusing, and activating Mini-Program processes is handled uniformly by the main process, which coordinates all scheduling.
# 4. How do Mini-Program processes and the main process communicate?
Bidirectional communication between the Mini-Program process and the Host-App process is implemented using AIDL. The Mini-Program process and the main process bind to the same remote service, obtain the service's proxy object via the Binder mechanism, and call AIDL interfaces to achieve inter-process communication.
# 5. How are process and thread safety issues handled?
- Currently, all resources that need to be written during Mini-Program operation are stored in the Mini-Program's corresponding directory within the application sandbox. There are no write operations to globally shared resources, so there is no issue of processes competing for the same system resources. For potential future process safety concerns, process-safe methods will be used during development for data reading and writing, such as using file locks to access files and replacing SharedPreferences with ContentProvider for data sharing.
- There are no direct thread safety issues between processes. Thread safety issues only arise when different Mini-Program processes concurrently access the main process's memory via inter-process communication or when concurrent read/write operations occur within a process. The SDK addresses thread safety-related issues using locks, synchronized methods, Java concurrent collections, and other mechanisms.