TraceView工具如何使用

一、TraceView工具如何使用

TraceView有4种启动/关闭分析方式:

(1) 第一种使用方法演示

1、 选择跟踪范围

在想要根据的代码片段之间使用以下两句代码:

Debug.startMethodTracing("love_world_");
Debug.stopMethodTracing();


例如,onCreate与onStart方法之间方法跟踪

public class MainActivity extends Activity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
        Debug.startMethodTracing("Love_World_");  
    }  
  
    @Override  
    protected void onStart() {  
        super.onStart();  
          
        Debug.stopMethodTracing();  
    }  
      
}  

2、添加SD卡访问权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>    


如果不添加,执行项目会出现以下异常

java.lang.RuntimeException:Unable to open trace file '/mnt/sdcard/Love_World_.trace': Permission denied  


如果手机没有SD卡也会出现同样的问题

3、 导出traceview文件

1  首先执行项目,查看trace文件是否生成

进入shell模式

adb shell  


查看是否已经生成这个文件

ls sdcard/Love_World_.trace  

2 导出trace文件

adb pull sdcard/Love_World_.trace

4、 打开trace文件

打开trace文件需要Android提供的traceview.bat工具,工具所在目录:sdk\tools\traceview.bat, 有两种方式执行:

  1. 在命令行中切换到此目录
  2. 将此目录添加到系统环境变量中
//  cmd在calc.trace所在目录执行  
traceview C:\Users\YourName\Desktop\Love_World_.trace  


其中“C:\Users\YourName\Desktop\” 表示trace所在你系统中的目录,此工具需要输入trace文件的绝对路径才行

在新版本的SDK 会有以下提示:

The standalone version of traceview is deprecated.  
Please use Android Device Monitor (tools/monitor) instead.  


所以建议使用tools/monitor 启动后跟Eclipse DDMS界面差不多,然后File -> Open File -> 选择trace文件

5、异常处理

1 异常处理

'C:\Windows\system32\java.exe' 不是内部或外部命令,也不是可运行的程序  
或批处理文件。  
SWT folder '' does not exist.  
Please set ANDROID_SWT to point to the folder containing swt.jar for your platfo  
rm.  


2 异常信息

The standalone version of traceview is deprecated.  
Please use Android Device Monitor (tools/monitor) instead.  
Failed to read the trace filejava.io.IOException: Key section does not have an *  
end marker  
        at com.android.traceview.DmTraceReader.parseKeys(DmTraceReader.java:420)  
        at com.android.traceview.DmTraceReader.generateTrees(DmTraceReader.java:91)  
        at com.android.traceview.DmTraceReader.<init>(DmTraceReader.java:87)  
        at com.android.traceview.MainWindow.main(MainWindow.java:286)  


通常是trace文件有异常,再重新生成并导出试试

3. 没有SD卡会出现异常

Unable to open trace file '/sdcard/Love_World_.trace': Permission denied  
 Caused by: java.lang.RuntimeException: Unable to open trace file '/sdcard/Love_World_.trace': Permiss

--------------------- 本文来自 superxlcr 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/superxlcr/article/details/78219673?utm_source=copy

猜你喜欢

转载自blog.csdn.net/c_z_w/article/details/82901582