APKAnalyzer failed to get size of APK building for android

I am using Unity 2022.3.16f1 on Windows 11 to build and deploy an app to android (Quest 3). I hit a problem with the build where the apkanalyzer was failing with a fatal error.

System.Exception: apkanalyzer failed to estimate the apk size. Output:
Error: Could not find or load main class

This turned out to be due to apkanalyzer.bat not handling there being a space in the path (Unity was installed in C:\Program Files.

This can be fixed by editing this file:

Editor\Data\PlaybackEngines\AndroidPlayer\SDK\cmdline-tools\6.0\bin\apkanalyzer.bat

Find this line:

@rem Add default JVM options here. You can also use JAVA_OPTS and APKANALYZER_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=-Dcom.android.sdklib.toolsdir=%~dp0\..

and add double quotes around the %~dp0..

set DEFAULT_JVM_OPTS=-Dcom.android.sdklib.toolsdir="%~dp0\.."

Alternatively install Unity in a directory with no space in the name (like C:\Tools) by configuring the relevant option in Unity Hub settings, which is what I did after testing my fix.

Kudos to you! Fixed my issue in no time. Thanks

Cool, worked for me. I love it how Unity 's own tools don’t work with Unity’s -default- install directory, amazing stuff.

Now I just have to get every person on my team who does builds to do this and make sure we remember to fix this step after every Unity update.

Hey Unity, maybe spend 5 seconds to get one of your thousands of employees to fix this?