Created
February 1, 2018 08:18
-
-
Save OleksandrKucherenko/f79b0fd93d009ba41ae6e9ff07b6506a to your computer and use it in GitHub Desktop.
Dump Android View hierarchy (very good for unit tests)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package your.name; | |
import android.annotation.TargetApi; | |
import android.app.Activity; | |
import android.app.Application; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.res.Resources; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.support.annotation.*; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.util.Pair; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.FrameLayout; | |
import org.robolectric.Robolectric; | |
import org.robolectric.RuntimeEnvironment; | |
import org.robolectric.android.controller.ActivityController; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.Stack; | |
import rx.functions.Action0; | |
import rx.functions.Action1; | |
import rx.functions.Func0; | |
import rx.functions.Func2; | |
public final class Helper { | |
/** Pint into log activity views hierarchy. */ | |
@NonNull | |
public static String logViewHierarchy(@NonNull final Activity activity) { | |
return logViewHierarchy(activity.findViewById(android.R.id.content)); | |
} | |
/** Print into log view hierarchy. */ | |
@NonNull | |
public static String logViewHierarchy(@NonNull final View root) { | |
final StringBuilder output = new StringBuilder(8192).append("\n"); | |
final Resources r = root.getResources(); | |
final Stack<Pair<String, View>> stack = new Stack<>(); | |
stack.push(Pair.create("", root)); | |
boolean nextLevel = false; | |
while (!stack.empty()) { | |
final Pair<String, View> p = stack.pop(); | |
final View v = p.second; | |
final boolean isLastOnLevel = stack.empty() || !p.first.equals(stack.peek().first); | |
final String graphics = "" + p.first + (isLastOnLevel ? "└── " : "├── "); | |
final String className = v.getClass().getSimpleName(); | |
final String line = graphics + className + " id=" + v.getId() + resolveIdToName(r, v); | |
output.append(line).append("\n"); | |
if (v instanceof ViewGroup) { | |
final ViewGroup vg = (ViewGroup) v; | |
for (int i = vg.getChildCount() - 1; i >= 0; i--) { | |
stack.push(Pair.create(p.first + (isLastOnLevel ? " " : "│ "), vg.getChildAt(i))); | |
} | |
} | |
} | |
final String msg = output.toString(); | |
return msg; | |
} | |
/** @see <a href="https://stackoverflow.com/questions/10137692/how-to-get-resource-name-from-resource-id">Lookup resource name</a> */ | |
@NonNull | |
/* package */ static String resolveIdToName(@Nullable final Resources r, @NonNull final View v) { | |
if (null == r) return ""; | |
try { | |
return " / " + r.getResourceEntryName(v.getId()); | |
} catch (Throwable ignored) { | |
return ""; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! Thanks for the code, it works great! I ported it to run through frida: https://github.com/kiber-io/frida-view-dump