Table of contents
Log print log
All developers should be aware of using android.util
package Log
printing logs . Logs can be printed on the console by calling methods such as , , , , and so on. The number of logs printed in the console exceeds the limit . From now on, no more logs will be printed, and missing logs will not be displayed on the console.Android
Log
v
d
i
w
e
Logcat
Log
Logcat
String.length
4061
length
4062
Logcat
To solve this problem, it is recommended to use the logger component
1. Import project
implementation 'com.orhanobut:logger:2.2.0'
2. Initialization
Logger.addLogAdapter(AndroidLogAdapter())
3. Use
Logger.d("hello")
If logger
the printed log length
is larger than the size 4061
, it will automatically wrap into new lines 4062
and continue printing.
Junit unit testing
Unit testing is the basic test in the application testing strategy. By unit testing the code, you can easily verify whether the logic of a single unit is correct. Running unit tests after each build can help you quickly capture and fix changes caused by code changes ( Regression problems caused by reconstruction, optimization, etc.).
Series of articles:
Just read this article for Android unit testing
Android unit testing (1): Introduction
Android unit testing (2): What is unit testing
Android unit testing (3): Test difficulties and solution selection
Android unit testing (4) ): JUnit introduces
Android unit testing (5): JUnit advanced
Android unit testing (6): Mockito learns
Android unit testing (7): Robolectric introduces
Android unit testing (8): How to test asynchronous code
Debug breakpoint debugging
Debug
For breakpoint debugging, put a breakpoint on the left side of the line where the code needs to be tested. After connecting the device, just click on the menu bar in the upper right corner of Android Studio Debug app
or click after starting the application Attach Debugger to Android Process
to turn on breakpoint debugging.
Debug
Not only can you perform breakpoint testing, you can also for
specify the variable value of the breakpoint in the loop and if
modify the value of the specified judgment.
For specific usage details, please refer to the following articles
1. Debug your app
2. Detailed explanation of Android debugging practice and principles
Other article references
1. Debug web apps
2. Debug WorkManager
Monkey stress test
Monkey
is a program that runs on an emulator or device and generates a stream of pseudo-random user events (such as clicks, taps, or gestures) as well as many system-level events. Monkey
Applications under development can be stress tested in a random and repeatable manner .
Monkey
The use is based on executing Android system commands ADB
through ADB
scripts . shell
You can launch it using the command line on your computer Monkey
or via a script. Since it Monkey
runs in an emulator/device environment, shell
it must be launched from within that environment. You can do this by prefixing each command adb shell
, or by just going in shell
and typing Monkey
the command.
Monkey
Events are generated and sent to the system at runtime. It also monitors the system under test and looks for three special conditions:
- If you have
Monkey
restricted running in one or more specific packages, it will monitor and block attempts to go to any other package. - If the app crashes or receives any unhandled exception,
Monkey
it stops and reports an error. - If the app generates an "Application Not Responding" error,
Monkey
it will stop and report the error.
Basic usage:
adb shell monkey -p your.package.name -v number.of.events
Monkey
Not only can you generate pseudo-random user events, you can also specify the user event trigger location. For details, please see:
1. UI/Application Exerciser Monkey
2. Introduction to Android automated testing (1) Monkey and MonkeyRunner
3. [Geek Academy] Android testing
4. Android View apk package name, current Activity name, etc.
Profiler Performance Analyzer
The Android Profiler in Android Studio 3.0 and later replaces the Android Monitor tool. The Android Profiler tool provides real-time data to help you understand your application's CPU, memory, and battery resource usage.
Profiler
Tutorial: Android Profiler
Android Studio 3.0 uses Android Profiler to measure application performance
ADB wireless connection device
Why introduce ADB
? ADB
Isn’t it just a wireless connection device? Wouldn't it be more stable to connect the device with a data cable? Why use it ADB
?
ADB
The full name Android Debug Bridge
is that it can play the role of a debugging bridge.
ADB
You can connect to the device through the IP address in the LAN, so that Android Studio can use it to install and debug the device's application and a series of device operations. However, if the network is unstable, the connection may be interrupted from time to time.
It is relatively more convenient for mobile phones to connect devices using data cables ADB
. Android 11+ devices can also connect wirelessly by scanning QR codes. But this situation is relative to small devices and devices with cameras to scan codes and the system version Android 11+. Some Android devices modified by manufacturers do not have a Type-c interface. Some devices may have cameras, but there are very few devices with Android versions greater than 10. As for TV devices, there is neither a Type-c interface nor a camera. If you want to install the equipment and debug it, this should be the only ADB
way to go.
Regarding ADB
connecting device debugging, you can read my article: ADB wirelessly connects Android devices
In addition, Huawei's can also be connected HDB
through use .ADB
Appium automated testing
Appium
is an open source test automation framework for automated testing of native, hybrid and mobile web applications.
Regarding Appium
debugging connected devices, you can read my article: App Automation Testing - Use of Appium
For me personally, the Appium
limitations of .Monkey
Appium
In addition to the above Appium
, Monkey
there are many, many automated testing tools for everyone to choose from. The Google
most recommended one Espresso
is also a good automated testing framework. The disadvantage is that it requires writing code... If you are interested, you can learn about it: Introduction to Android Automated Testing (3) Espresso
BlockCanary interface stuck detection
BlockCanary
It is a non-intrusive performance monitoring component of the Android platform. The application only needs to implement an abstract class and provide some context required by the component. It can detect various slowdown problems on the main thread when using the application, and pass Various information provided by the component can be used to analyze the cause of the jam.
BlockCanary
It is extremely simple to use, just build.gradle
import the dependencies and then initialize them.
dependencies {
implementation 'com.github.markzhai:blockcanary-android:1.5.0'
// 仅在debug包启用BlockCanary进行卡顿监控和提示的话,可以这么用
// debugImplementation 'com.github.markzhai:blockcanary-android:1.5.0'
// releaseImplementation 'com.github.markzhai:blockcanary-no-op:1.5.0'
}
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
BlockCanary.install(this, BlockCanaryContext()).start()
}
}
If there are a series of requirements such as uploading lag logs, adjusting lag thresholds, etc., you can BlockCanaryContext
rewrite the relevant method
implementations through inheritance classes.
Run the code on the device and when the main thread is blocked, a blocking notification will be displayed on the top notification bar. Click to view the blocking details. As shown in the figure below, the main thread of MainActivity 16 lines of code has a problem of blocking for 1008ms.
Note: Some Android versions
BlockCanary.install(this, BlockCanaryContext()).start()
may cause errors when installing the installation program. If errors occur, it is recommended to test on another device.
For details, please see: AndroidPerformanceMonitor
App Inspection App Inspection
App Inspection
Through the menu bar at the bottom of Android Studio, Android API 26 and above devices can observe , and related data App Inspection
at runtime .Database
Network
Background Task
Database Inspector
Database Inspector
Used to observe local database data.
For specific details, please refer to: Using Database Inspector to debug the database
Network Inspector
Network Inspector
Used to observe network request related data, this function is similar to packet capture. Currently, Network Inspector
only HttpsURLConnection
the and OkHttp
network connection libraries are supported. If your app uses a different networking library, you may not be able to Network Inspector
view network activity in .
For specific details, please refer to: Use Network Inspector to inspect network traffic
Background Task Inspector
Background Task Inspector
Allows you to visualize, monitor, and debug your application's backend worker
.
For specific details, please refer to:
WorkManager code sample
using the background task inspector to debug WorkManager worker
Leakcanary memory leak (ML), memory overflow (OOM) detection
Memory Leak, or ML for short, means that after a program applies for memory, it cannot release the applied memory space, causing the system to be unable to reclaim memory in time and allocate memory for use by other processes. Usually a small number of times when the memory cannot be recycled in time will not have any impact on the program. However, if the memory itself is relatively small and the memory cannot be recycled normally many times, it will lead to insufficient memory allocation and eventually lead to memory overflow.
Out of Memory, or OOM for short, means that when a program applies for memory, there is not enough memory for the applicant to use, resulting in data being unable to be stored in the memory normally.
Leakcanary
It is a memory leak detection library for Android systems.
Leakcanary
You can use it by adding dependencies. When your application is installed on the device Leaks
, the App will be installed at the same time. If a memory leak is detected while running the application, you Leaks
can see the memory leak notification in the notification bar.
dependencies {
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.10'
}
For specific details, please refer to:
- Android common memory leaks & solutions
Leakcanary
Source code: LeakcanaryLeakcanary
How it works: How LeakCanary worksLeakcanary
Fixing a memory leak: Fixing a memory leak