In the process of Android APP development and testing, there are usually the following requirements: dynamically switching a function for comparison and verification, such as switching the interface of network request between the test environment and the production environment; If the online version is faulty, enable certain test functions or log functions to locate the fault. It would be too tedious to modify the code and repackage it. In similar cases, some common ways are as follows: pass some parameters to the APP externally to modify the configuration of the APP.

1. Leave eggs

To leave an Easter egg means to write Debug related functions in a hidden setting interface in advance. The path to open the design interface is deep or the operation is complex, and the hidden setting interface will not be triggered by ordinary users’ routine operations. Consider the “Developer options” screen on Android phones. The operation to enter the egg page is usually a series of button clicks + long press of the physical button, or search/input field to enter a specific string, or scan a specific QR code, etc.

2. Configuration files

Another option is to place a file with a fixed name on a specific path to the device’s SD card when we want to debug our application. When the application is started, you can search the specified path to check whether the corresponding file exists. If yes, you can enter the debug mode and read the corresponding configuration parameters from the file.

3. Android system related attributes

We want to debug an Intent (A), and create an application (B) whose sole purpose is to launch an Intent with the required debugging parameters. That is, we write the debugging parameter configuration interface of application A in application B. When we want to debug APPLICATION A, we do not directly enter A from the desktop icon, but enter APPLICATION B first. After configuring the required parameters in application B, we enter application A from the jump button in application B. The parameters configured in B can then be passed to application A via the Intent.

Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName("com.android.settings"."com.android.settings.wifi.WifiNoInternetDialog");
intent.putExtra("debugInfo"."api=debug&log=open");
mContext.startActivity(intent);
Copy the code

3.2 Setting System Attributes There is a system attribute service in the Android system. This service manages the global configuration and status of the system. These configurations and status are key-values. Get (“key”) can be used by every process in the system to obtain these global property values. These property values can also be queried and set using the getProp and setprop commands in adb shell. In this way, we can judge whether to enter debugging mode by reading our customized attribute value in APP. When we need to debug the application, we can set the corresponding attribute value through ADB command.

Dumpsys is a tool we often use for performance tuning. This tool can obtain information about system services. For example, adb shell Dumpsys Activity activities can get information about activities. When this command is executed, the system iterates through all Activity instances in the system and calls their dump method.

This way we can replicate this method in our Activity to receive data from the command line and then configure the appropriate debugging environment.