- 今天解决了一个ANR异常问题,心情愉悦,原来ANR分析解决很简单(可能问题本来比较简单吧~~暂做参考)
三个步骤搞定:
步骤一:重现ANR 异常
对着界面一顿操作...直至弹出anr提示
步骤二:拉取anr log文件
adb shell //进入shell命令模式
cp data/anr/traces.txt sdcard/traces.txt //将traces.txt文件copy到sdcard
步骤三:分析ANR日志:
1.打开traces.txt
tips:可以将文件重命名为.log的文件,然后再vscode打开,这样有颜色。
2.直接搜索关键词:DALVIK THREADS,然后查看 tid=1的"main"线程。
如果你用vscode打开该log文件,会看到类似于logcat的一堆:
at android.os.BinderProxy.transactNative(Native method)
at android.os.BinderProxy.transact(Binder.java:615)
at android.bluetooth.IBluetooth$Stub$Proxy.getRemoteName(IBluetooth.java:1351)
at android.bluetooth.BluetoothDevice.getName(BluetoothDevice.java:731)
at akw$5.onLeScan(BluetoothLeDevice.java:263)
at android.bluetooth.BluetoothAdapter$2.onScanResult(BluetoothAdapter.java:2388)
at android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper$1.run(BluetoothLeScanner.java:400)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6244)
at java.lang.reflect.Method.invoke!(Native method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:797)
3.找到属于你的项目的那个java文件,定位到出毛病的那一行。
4.接下来就是去自己的代码中分析了,如果你知道ANR的理论知识,分析解决,应该不是难事。
============================
============割==============
============================
举个栗子:
在我的项目中,蓝牙扫描的同时绘制地图,出现了ANR,在部分手机可复现,于是按照上述步骤一顿操作,得到ANR日志文件如下(关键部分):
at android.bluetooth.IBluetooth$Stub$Proxy.getRemoteName(IBluetooth.java:1351)
at android.bluetooth.BluetoothDevice.getName(BluetoothDevice.java:731)
at akw$5.onLeScan(BluetoothLeDevice.java:263)
at android.bluetooth.BluetoothAdapter$2.onScanResult(BluetoothAdapter.java:2388)
at android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper$1.run(BluetoothLeScanner.java:400)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6244)
at java.lang.reflect.Method.invoke!(Native method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:797)
#================省略其他线程===================
----- end 9155 -----
可以看出,问题出在调用BluetoothDevice.getName()方法上,自己的代码的问题出在(BluetoothLeDevice.java:263)。这个类是自己写的。进去看一下代码:
//class:BluetoothLeDevice.java
mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
mScanTimes++;
String carBleName = getCarBleName();
String carOrgBleName = getCarBleOrgName();
String carBleMac = getCarBleMac();
if (BuildConfig.DEBUG)
Log.i(TAG, "LeScanCallback " + device.getName() + " ?= " + carBleName + " | " + carOrgBleName + " mac:" + device.getAddress() + " ?= " + carBleMac + " " + mScanTimes);
else
Log.i(TAG, "LeScanCallback name:" + device.getName() + " mac:" + device.getAddress() + " ?= " + carBleMac + " " + mScanTimes);
if ((carBleMac != null && device.getAddress() != null
&& !TextUtils.isEmpty(carBleMac.trim())
&& device.getAddress().equals(carBleMac.trim())) || //这一坨就是第263行
(carBleName != null && device.getName() != null
&& !TextUtils.isEmpty(carBleName.trim())
&& device.getName().equals(carBleName.trim())) ||
(carOrgBleName != null && device.getName() != null
&& !TextUtils.isEmpty(carOrgBleName.trim())
&& device.getName().equals(carOrgBleName.trim()))) {
Log.i(TAG, "LeScanCallback found " + device.getName() + " mac:" + device.getAddress());
postHandlerEvent(new BlueToothMatch(device.getName(), device.getAddress()));
}
};
在第263行,扫描蓝牙的时候,进行了一坨判断,判断里进行了好多次的getName方法,然后postHandle....
这就是问题所在了。解决后:
mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
mScanTimes++;
String carBleName = getCarBleName();
String carOrgBleName = getCarBleOrgName();
String carBleMac = getCarBleMac();
String deviceName = device.getName();//在此处获取一次就行了
String deviceAddress = device.getAddress();
if (BuildConfig.DEBUG)
Log.i(TAG, "LeScanCallback " + deviceName + " ?= " + carBleName + " | " + carOrgBleName + " mac:" + device.getAddress() + " ?= " + carBleMac + " " + mScanTimes);
else
Log.i(TAG, "LeScanCallback name:" + deviceName + " mac:" + deviceAddress + " ?= " + carBleMac + " " + mScanTimes);
if ((carBleMac != null && deviceAddress != null && !TextUtils.isEmpty(carBleMac.trim()) && deviceAddress.equals(carBleMac.trim())) ||
(carBleName != null && deviceName != null && !TextUtils.isEmpty(carBleName.trim()) && deviceName.equals(carBleName.trim())) ||
(carOrgBleName != null && deviceName != null && !TextUtils.isEmpty(carOrgBleName.trim()) && deviceName.equals(carOrgBleName.trim()))) {
Log.i(TAG, "LeScanCallback found " + deviceName + " mac:" + deviceAddress);
postHandlerEvent(new BlueToothMatch(deviceName, deviceAddress));
}
};