hierarchyviewer不能用的解决办法

本文介绍如何在Android应用中使用ViewServer类启用HierarchyViewer工具,以便进行UI调试。通过注册和注销活动来管理视图服务器,并确保应用程序具有INTERNET权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ViewServer is a simple class you can use in your Android application 
to use the HierarchyViewer inspection tool. 

ViewServer requires the Android SDK r12 or higher. 
http://developer.android.com/sdk/index.html 

Please refer to the documentation in ViewServer.java for more info. 

https://github.com/romainguy/ViewServer  


Enable HierarchyViewer on production builds  

For security reasons HierarchyViewer does NOT work on production builds. It works only with userdebug and engineering builds (this includes the emulator.) 

In this case you’ve got the following errors: 
[hierarchyviewer]Unable to get view server protocol version from device 
[hierarchyviewer]Unable to debug device 

But there is a way to enable the use of HierarchyViewer inside your own application. Romain Guy suggests to use ViewServer class: https://github.com/romainguy/ViewServer 

The recommended way to use this API is to register activities when they are created, and to unregister them when they get destroyed: 

public class MyActivity extends Activity { 
    public void onCreate(Bundle savedInstanceState) { 
         super.onCreate(savedInstanceState); 
         // Set content view, etc. 
         ViewServer.get(this).addWindow(this); 
     } 

     public void onDestroy() { 
         super.onDestroy(); 
         ViewServer.get(this).removeWindow(this); 
    } 

     public void onResume() { 
         super.onResume(); 
         ViewServer.get(this).setFocusedWindow(this); 
    } 



To use this view server, your application must require the INTERNET permission: 

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

As a result you will be able to make HierarchyViewer work on any devices: 


 

Sometimes it could be useful to build ViewServer as Library project, as described at:  http://developer.android.com/tools/projects/projects-eclipse.html 
And then just referencing it from your application like this: 


 




And once again: do not forget to add INTERNET permission! Otherwise you will got some NullPointerExceptions with ServerSocket.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值