Thursday, August 16, 2012

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:

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) {
         // Set content view, etc.

     public void onDestroy() {

     public void onResume() {

To use this view server, your application must require the INTERNET 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:
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.


  1. It does not work at all in Android4.1.2.:)

  2. The thing just doesnt work (4.2.2). i tried copy ViewServer class to my project and also refer to it as library. it doesnt work neither way.