Android Debug Monitor hierarchy view not showing

Refer to Profile Your Layout with Hierarchy Viewer for hierarchy viewer usage.

Today I use hierarchy viewer with my app, and I get the very similar issue on both silulator and real device, with hierarch viewer Tree View window black.

Note: For the newer version of android studio, you can open hierarchy viewer with Tools > Android > Android Device Monitor > Window > Open Perspective... > Hierarchy View

enter image description here

Below I’ll show you how to solve the problem on silulator and real device.

Info: device debug mode is on, I don’t test if it has influence on the below result.

a). check if the device have open the view server

bogon:~ roofe$ adb shell service call window 3
Result: Parcel(00000000 00000000   '........')

Note: if you get

cannot find command adb

adb is usually in the path of sdk, mine is at /Users/roofe/Library/Android/sdk/platform-tools/adb. Then you can add it to path or make a alias for it.

I put alias adb='/Users/roofe/Library/Android/sdk/platform-tools/adb' in the .bash_profile under the user path, then source ./.bash_profile. Here adb command should works.

output info:

View Server off: Result: Parcel(00000000 00000000 '........')

View Server on: Result: Parcel(00000000 00000001 '........')

If the view server is on, you hierarchy viewer should works well. And you needn’t to read below.

Unfortunately, if your view server is off, go on.
Both view server of my simulator and read device are off.

b). Start View server

bogon:~ roofe$ adb shell service call window 1 i32 4939
Result: Parcel(00000000 00000001   '........')

If the result is Result: Parcel(00000000 00000001 '........'), the view server has been started. While if the result is Result: Parcel(00000000 00000000 '........'), the view server cannot be started.

You also can shutdown the view server with

adb shell service call window 2 i32 4939

Here execute the command two times to shutdown it, maybe there is a execution time here.

bogon:~ roofe$ adb shell service call window 2 i32 4939
Result: Parcel(00000000 00000001   '........')
bogon:~ roofe$ adb shell service call window 2 i32 4939
Result: Parcel(00000000 00000000   '........')

Most of the real device cannot start the view server. For security, Android on real device have restrict on this. You can find the relative code in file WindowManageService.java source code of Andorid

if (!checkCallingPermission(Manifest.permission.DUMP, "startViewServer")) {
    return false;
} 

So if you’re a genius cracker, you can root your device to remove this restrict. I’ve not make it. If your try successfull, show me it. I’ll be very appreciated on your smart work.

Here my simulator works. While real device not. If your view server is still off, go on.

c). Using the ddm protocol, one method from the doc of Android studio doc Profile Your Layout with Hierarchy Viewer

If you’re using the Android Emulator, you can skip this section.
Otherwise, you need to set up your device as follows.

Note: Your device must be running Android 4.1 or higher.

Enable Developer Options on your device.

Set the environment variable ANDROID_HVPROTO=ddm on your development machine.

This variable tells Hierarchy Viewer to connect to the device using the ddm protocol, which is the same as the DDMS protocol. The
caveat is that there can be only one process on the host that connects
to the device, so you must kill any other DDMS session to run
Hierarchy Viewer.

Add export ANDROID_HVPROTO=ddm to your .bash_profile then source ./.bash_profile.

Remove the installed app, and restart Android Studio, then install your app and you can hierarchy view it now.

Note: I set the ddm without restart Android Studio a, I cannot hierarchy view it. One day later, I realise it, and do some test. I founds after set the ppm, it’s better to restart the android studio. I guess when android studio start it will read that config, can cache it.

I didn’t test it without uninstall the app.

d). set up a view server

Please don’t fell frustrated here. To start a view server is very easy.

Tool: ViewServer on github

add this to your build.gradle

allprojects {
    repositories {
        jcenter()
        maven {url "https://jitpack.io"}
    }
}
dependencies {  
    ...
    compile 'com.github.romainguy:ViewServer:017c01cd512cac3ec054d9eee05fc48c5a9d2de'  
}  

Add INTERNET permission in the manifest file

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

Then regiester the viewserver to the activity you want to inspect

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);
    }
}

By now, I can run the Hierarchy view on my real device.

enter image description here

Leave a Comment