AAD-20-Debugging
11/28
Debugging is the process of isolating and removing defects in software code. By understanding the debugging tools in Android Studio, Android developers can create reliable and robust applications.
To prepare for the AAD certification exam, Android developers should:
- Understand the basic debugging techniques available in Android Studio
- Know how to debug and fix issues with an app’s functional behavior and usability
- Be able to use the System Log to output debug information
- Understand how to use breakpoints in Android Studio
- Know how to inspect variables using Android Studio
Resources
- Android Developers -> Debug your app
- Android Dev Fundamentals -> Write and view logs with Logcat
- Codelabs -> Android Studio debugger
- Codelabs -> Add log statements to your app
11/28
android {
buildTypes {
release {
debuggable false
...
}
debug {
debuggable true
...
}
}
}考虑到还有native:c/c++代码,因此AS中debug模式其实:Java/Native/Dual(Java+Native)
- Debugger:调试器
- Attach debugger to Android process:关联到一个进程,这种方式其实非常高效而无需重启app基本的操作:
- 启动调试
- 断点
- 快捷键:step in/out, resume
- watch 变量,以及 Evaluate Expression
- Log相关, Logcat
- 查看崩溃点: adb logcat --buffer=crash
- 分析堆栈 Analyze Stack Trace.
- profile:我还没有怎么关注过这个:Android Profiler/Network Profiler/CPU Profiler/ 属于app或者的优化和问题分析的重要工具。---要尝试使用。我的手机上:一进入断点,暂停几秒就会自动调试停止,但是在emulator上一直没有这个问题。
adb: Android debug bridge
本身包含三部分:
- adb 命令:这是client端(我们开发使用的电脑),我们输入adb 命令
- adbd: 这个是跑在emulator或device上,daemon,但是有的设备把这个禁用了
- A server:也是跑在开发的电脑上,用于连接 电脑和设备,负责两者之间的通信。
明白了adb是由三部分组成的,对于了解很多高级功能很有帮助,如 远程调试。adb是Android熟练和入门的分界线。当然Android还有很多其他工具,但adb是最基本的。
ANR: Application Not Responding
发送ANR后,会有一个trace文件: /data/anr/traces.txt
这个文件会可能被覆盖,On newer OS releases, there are multiple /data/anr/anr_* files.# 直接查看crash 点(连接设备或者仿真器), 相当于读取logcat输出过滤
adb logcat --buffer=crash
adb logcat -b crash
adb logcat AndroidRuntime:E *:S根据这儿的说明
adb logcat ActivityManager:I MyApp:D *:S
这儿有3个过滤器
以下是一个过滤器表达式的示例,该表达式会抑制(不显示)除标记为“ActivityManager”、优先级不低于“信息”的日志消息,以及标记为“MyApp”、优先级不低于“调试”的日志消息以外的所有其他日志消息:
上述表达式中最后一个元素 *:S 将所有标记的优先级设为“静默”,从而确保系统仅显示标记为“ActivityManager”和“MyApp”的日志消息。使用 *:S 是确保日志输出受限于您已明确指定的过滤器的绝佳方式,
*:S 也可写成 logcat -s过滤器优先级: V详细/D调试/I信息/W警告/E错误/F严重错误/S静默
日志的优先级:VERBOSE、DEBUG、INFO、WARNING、ERROR 或 FATALAndroid 日志记录系统是系统进程 logd 维护的一组结构化环形缓冲区。这组可用的缓冲区是固定的,并由系统定义。最相关的缓冲区为:
main(用于存储大多数应用日志)
system(用于存储源自 Android 操作系统的消息)和
crash(用于存储崩溃日志)。
adb logcat -b crash: 这个命令 -b指的是buffer
日志格式
date time PID-TID/package priority/tag: message
12-10 13:02:50.071 1901-4229/com.google.android.gms V/AuthZen: Handling delegate intent.由于每条log只能有一个tag,因此在logcat过滤显示的时候,filter之间是或的关系
一般而言tag就可以区分app,所以我们很少在logcat的时候指定app或者package,但是对于一些framework来说这是必要的。问题:
- 怎么过滤app,logcat com.xx.yy:I 貌似不起作用
ProGuard
ProGuard是Java的产物
ProGuard主要的作用是:
压缩(Shrinking)减小目标的大小
优化(Optimization)执行更快,优化有级别(可能是内存与运行速度的平衡)
混淆(Obfuscation)保护源代码防止反编译(比如网络的model就不能混淆)日志的级别:就属于优化的一部分,因为日志输出会带来性能下降
混淆规则:app/build/outputs/mapping/release下生成一个mapping.txt一般而言,我们在debug的时候不需要ProGuard,proguard-rules.pro 这个文件就是混淆例外规则,各种-keep
android {
buildTypes {
release {
debuggable false
...
proguardFiles getDefaultProguardFile(""), 'proguard-rules.pro' }
debug {
debuggable true
...
}
}
}