'I cannot run uiautomatorviewer on my macbook M1

Machine
macBook Air M1

OS
macOS Big Sur Version 11.5.2

java -version

java version "1.8.0_301"

Java(TM) SE Runtime Environment (build 1.8.0_301-b09)

Java HotSpot(TM) 64-Bit Server VM (build 25.301-b09, mixed mode)

bash_profile

   export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home
   export PATH=$PATH:$JAVA_HOME/bin
   export PATH=$PATH:$JAVA_HOME/lib
   export MAVEN_HOME=/Users/XXX/Documents/apache-maven-3.8.2
   export M2=$MAVEN_HOME/bin
   export ANDROID_HOME=/Users/XXX/Library/Android/sdk
   export PATH=$PATH:/usr/local/git/bin:/usr/local/bin:
   export PATH=$PATH:$ANDROID_HOME/platform-tools
   export PATH=$PATH:$ANDROID_HOME/tools
   export PATH=$PATH:$ANDROID_HOME/tools/bin
   export PATH=$PATH:$ANDROID_HOME/emulator
   export PATH=$PATH:$ANDROID_HOME/build-tools
   export PATH=$PATH:$ANDROID_HOME/tools/lib/x86_64

On running uiautomatorviewer on terminal i get below exception

java.lang.NullPointerException
at org.eclipse.swt.widgets.Control.internal_new_GC(Unknown Source)
at org.eclipse.swt.graphics.GC.<init>(Unknown Source)
at org.eclipse.swt.graphics.GC.<init>(Unknown Source)
at org.eclipse.swt.widgets.Tree.computeSize(Unknown Source)
at org.eclipse.swt.layout.GridData.computeSize(Unknown Source)
at org.eclipse.swt.layout.GridLayout.layout(Unknown Source)
at org.eclipse.swt.layout.GridLayout.computeSize(Unknown Source)
at org.eclipse.swt.widgets.Composite.computeSize(Unknown Source)
at org.eclipse.swt.custom.SashFormLayout.computeSize(Unknown Source)
at org.eclipse.swt.widgets.Composite.computeSize(Unknown Source)
at org.eclipse.swt.custom.SashFormLayout.computeSize(Unknown Source)
at org.eclipse.swt.widgets.Composite.computeSize(Unknown Source)
at org.eclipse.swt.layout.FillData.computeSize(Unknown Source)
at org.eclipse.swt.layout.FillLayout.computeChildSize(Unknown Source)
at org.eclipse.swt.layout.FillLayout.computeSize(Unknown Source)
at org.eclipse.swt.widgets.Composite.computeSize(Unknown Source)
at org.eclipse.swt.layout.GridData.computeSize(Unknown Source)
at org.eclipse.swt.layout.GridLayout.layout(Unknown Source)
at org.eclipse.swt.layout.GridLayout.layout(Unknown Source)
at org.eclipse.swt.widgets.Composite.updateLayout(Unknown Source)
at org.eclipse.swt.widgets.Composite.updateLayout(Unknown Source)
at org.eclipse.swt.widgets.Composite.layout(Unknown Source)
at org.eclipse.swt.widgets.Display.runSettings(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at org.eclipse.jface.window.Window.runEventLoop(Window.java:825)
at org.eclipse.jface.window.Window.open(Window.java:801)
at com.android.uiautomator.UiAutomatorViewer.main(UiAutomatorViewer.java:78)

Please let me know if anyone was able experience a similar issue and has a solution.

Thanks, Nahid



Solution 1:[1]

I have found a better alternative than uiautomator - appium inspector.

If you have installed appium GUI on your mac follow below steps:

  1. Start appium server
  2. Click the search button to start the appium inspector enter image description here
  3. Provide below your android configurations {
    "platformName": "Android",
    "platformVersion": "8.1", //<>
    "app": "/path/to/.apk/file", "deviceName": "c33143r", //<<get device name using: $ adb devices>>
    "automationName": "UiAutomator2"
    }

Solution 2:[2]

I had the below configurations and Java version is 1.8.0_332

export ANDROID_SDK=$HOME/Library/Android/sdk
export PATH=$ANDROID_SDK/emulator:$PATH
export PATH=$ANDROID_SDK/tools:$PATH
export PATH=$ANDROID_SDK/tools/bin:$PATH
export PATH=$ANDROID_SDK/platform-tools:$PATH
export ANDROID_SDK_ROOT=$ANDROID_SDK 
export ANDROID_AVD_HOME=$HOME/.android/and
alias emulator='$ANDROID_SDK/emulator/emulator'

In my case, the below solution worked like a charm

https://github.com/android/android-test/issues/911#issuecomment-849389068

  1. Download the swt.jar, from https://download.eclipse.org/eclipse/downloads/index.html, rename downloaded swt.jar to swt2.jar
  2. copy swt2.jar to Library/Android/sdk/tools/lib/x86_64/ [Note: DO NOT REPLACE/DELETE]
  3. Launch uiautomatorviewer

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nahid
Solution 2 Bhoomika G