I did a thing! I made Android Studio’s UI Automator Viewer run on Java 20! Some of you might be saying, “Duh, doesn’t everyone already do that?”. I don’t know the answer to that question, but based on my internet searches, I think the answer is “no, not everyone already does that”. Here’s the story and my resulting solution.

Alert! If you just want to see the solution, you can just scroll down a bit, but please read the bulleted list below the solution; there’s info in that list that may be valuable to you.

As part of a Proof of Concept (PoC) project with a client, we need to write automated test scripts using Appium to drive a mobile app on an Android emulator. As most of you know, part of writing automated tests is exploring the apps and identifying the elements with which we need to interact. To facilitate the automation of mobile apps, Android Studio provides a tool called UI Automator; it also provides a tool called UI Automator Viewer which allows automators to explore the structure of the app and choose locators for use in the automated tests.

Android Studio itself works on “current” versions of Java, e.g., Java 11 and Java 20. “Out of the box”, however, UI Automator Viewer expects to run on Java 8. Java 8 is now quite old; as of this writing (July 2023), Java 20 is the current version. The client I’m working with doesn’t want to deal with the overhead of multiple Java versions, especially ones that are so old; they do not want anything to do with Java 8.

I figured, surely, I can’t be the only person that wants to run the viewer on a current version of Java. As one does, I consulted the tried-and-true expert: the internet. I looked for ways to run UI Automator Viewer on a more current version of Java. I chose Java 20 because it’s what I had installed.

The internet wasn’t much help. Most of the guidance I found was, “Yeah, that’s not gonna work; just run it on Java 8”; I even found one post of someone saying that they doubted it was even possible. I did find one post that kind of pointed me in the right direction; check out “Solution #2” in this Stack Overflow post. Unfortunately, my Java command line skills are rusty, and I thought to myself, “There must be an easier way” (spoiler alert, I didn’t find an easier way).

So, I consulted the new expert: ChatGPT. After a bit of back and forth to refine the prompt, ChatGPT provided a bat file that contained a suitable command line call for Java 20. The only problem was that it didn’t actually work to launch the viewer. The provided bat file was, however, very helpful in showing me the basic Java command line arguments I needed; the valid arguments have changed appreciably between Java 8 and subsequent Java versions. Now, I needed to work through the runtime errors.

Most of the errors were ClassNotFoundException. At first, I thought maybe name mangling changed between Java 8 and later versions, but when I came up empty on that, I figured out that some additional JAR files needed to be added to the classpath. I’ll spare you the iterative trial-and-error I went through, but you can see the required JARs in the example below. It took me a few hours to do all of this, but I assume that someone with more experience in snooping JAR files and mapping “class not found” errors would have probably completed this with less effort than I had to expend.

Once I fixed the classpath: success! I was able to launch the viewer with no errors. I was even able to open and explore a previously captured session as well as capture new snapshots.

So, without further ado, here’s my version of the script to launch UI Automator Viewer on Java 20:

@echo off

setlocal

set "JAVA_HOME=<path to Java installation>"
set "ANDROID_HOME=<path to Android Studio Installation>"
set "TOOLS_DIR=%ANDROID_HOME%\tools"
set "LIB_DIR=%TOOLS_DIR%\lib"

rem Set the classpath
set "CLASSPATH=%LIB_DIR%\uiautomatorviewer-26.0.0-dev.jar;%ANDROID_HOME%\platforms\android-34\uiautomator.jar;%LIB_DIR%\x86_64\swt.jar;%ANDROID_HOME%\cmdline-tools\latest\lib\common\tools.common.jar;%LIB_DIR%\ddmlib-26.0.0-dev.jar"

rem Launch UiAutomatorViewer using Java 11
"%JAVA_HOME%\bin\java" -Xmx512m -classpath "%CLASSPATH%" "-Dcom.android.uiautomator.bindir=%ANDROID_HOME%\platform-tools" com.android.uiautomator.UiAutomatorViewer %*

endlocal

Some notes about this implementation

  • You need to set JAVA_HOME as appropriate for your setup unless you have already set it elsewhere in your environment.
  • You need to set ANDROID_HOME as appropriate for your setup unless you have already set it elsewhere in your environment.
  • People with more Java experience than me may know elegant ways to implement this.
  • People with more bat file experience than me may know elegant ways to implement this.

I’m writing this blog to have this info “out there” and shareable; please, free to use and share! Hopefully, I can save others the effort I had to expend. As I said, I’m no expert in creating Java command lines or in bat files. I know some of my readers will have improvements and possibly even fixes to my solution (I can’t say that I’ve spent much time testing beyond my own needs). Please, feel free to add those to the comment section!

Like this? Catch me at an upcoming event!