Unity-Technologies / BuildReportInspector

Editor script which implements an inspector for the BuildReport class
321 stars 52 forks source link

[Android] Build Report Inspector generates "apkanalyzer failed to estimate the apk size. Output: Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8" exception on Build when using Java Environment Variable #29

Open SkowronskiAndrew opened 1 year ago

SkowronskiAndrew commented 1 year ago

Transferred from Jira (UUM-19493) because this ticket is a bug in the BuildReportInspector.

How to reproduce:

  1. Go to “Edit the system environment variables” > “Environment Variables…” > “New”
  2. Add “JAVA_TOOL_OPTIONS“ as a Variable name and “-Dfile.encoding=UTF-8“ as a Variable value
  3. Create a new Unity project
  4. Go to “Package Manager“ > “Add package from git URL“ and add “com.unity.build-report-inspector“ URL
  5. Build for Android
  6. After the Build is complete observe the Console

Expected result: There are no exceptions Actual result: “System.Exception: apkanalyzer failed to estimate the apk size. Output: Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8“ exception appears

Reproducible with: 0.2.2-preview, 0.3.0-preview (2020.3.42f1, 2021.3.14f1, 2022.1.23f1, 2022.2.0b16, 2023.1.0a20)

Not reproducible on: iOS, Standalone Player (Windows 11)

Note: After setting Environment Variable a Unity Hub / PC restart may be needed

Full exception message: System.Exception: apkanalyzer failed to estimate the apk size. Output: 18131288 Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 at Unity.BuildReportInspector.Mobile.AndroidUtilities.GetApkDownloadSize (System.String applicationPath) [0x001e5] in Project Path\Library\PackageCache\com.unity.build-report-inspector@0.3.0-preview\Editor\Mobile\AndroidUtilities.cs:253 at Unity.BuildReportInspector.Mobile.AndroidUtilities.GetArchitectureInfo (System.String applicationPath) [0x000f3] in Project Path\Library\PackageCache\com.unity.build-report-inspector@0.3.0-preview\Editor\Mobile\AndroidUtilities.cs:128 at Unity.BuildReportInspector.Mobile.MobileAppendix..ctor (System.String applicationPath) [0x000d8] in Project Path\Library\PackageCache\com.unity.build-report-inspector@0.3.0-preview\Editor\Mobile\MobileAppendix.cs:90 at Unity.BuildReportInspector.Mobile.MobileHelper.GenerateMobileAppendix (System.String applicationPath, System.String guid) [0x0001e] in Project Path\Library\PackageCache\com.unity.build-report-inspector@0.3.0-preview\Editor\Mobile\MobileHelper.cs:77 UnityEngine.Debug:LogError (object) Unity.BuildReportInspector.Mobile.MobileHelper:GenerateMobileAppendix (string,string) (at Library/PackageCache/com.unity.build-report-inspector@0.3.0-preview/Editor/Mobile/MobileHelper.cs:83) Unity.BuildReportInspector.Mobile.MobileHelper:GenerateAndroidAppendix (string,string) (at Library/PackageCache/com.unity.build-report-inspector@0.3.0-preview/Editor/Mobile/MobileHelper.cs:27) Unity.BuildReportInspector.Mobile.PostBuildSetup:BuildPostProcess (UnityEditor.BuildTarget,string) (at Library/PackageCache/com.unity.build-report-inspector@0.3.0-preview/Editor/Mobile/PostBuildSetup.cs:44) UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

Details:

Notes from Julius Miknevicius: Build Report Inspector package tries to parse the whole apkanalyzer output as long https://github.com/Unity-Technologies/BuildReportInspector/blob/master/com.unity.build-report-inspector/Editor/Mobile/AndroidUtilities.cs#L247

however when JAVA_TOOL_OPTIONS environment variable is specified, the output of apkanalyzer is:

Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
20964989 

20964989 is the size of the apk, which is what build report inspector wants.

So it seems the fix would be to improve the parsing code to strip off that possible extra junk in the output (or find a way to invoke the tool so that it doesn't add that unexpected noise in its output)

dtaddis commented 1 year ago

I'm getting this same error since upgrading from Unity 2021 to 2022. I don't have a JAVA_TOOL_OPTIONS environment variable.

It technically causes my (Quest) build to fail, although the apk is produced and can be copied to the device manually.

nick-morhun commented 5 months ago

There is a branch named "seans/fix-jkd-issues" which has some fix.

Xeonzinc commented 1 month ago

There is a branch named "seans/fix-jkd-issues" which has some fix.

that does not seem to solve the issue mentioned above - but a hacky solution just to get it running if you dont care about overall file size:

AndroidUtilities.cs #238 -> #258

          int exitCode = 0;
            long result = 0;
            if (File.Exists(apkAnalyzerPath))
            {
                var apkAnalyzerArgs = $"apk download-size \"{applicationPath}\"";
                apkAnalyzerOutput = Utilities.RunProcessAndGetOutput(apkAnalyzerPath, apkAnalyzerArgs, out exitCode);
            }
            else
            {
                var javaExecutablePath = GetJavaExecutablePath();
                var apkAnalyzerArgs = $"{GetApkAnalyzerJavaArgs()} apk download-size \"{applicationPath}\"";
                apkAnalyzerOutput = "0";// Utilities.RunProcessAndGetOutput(javaExecutablePath, apkAnalyzerArgs, out exitCode);
                result = 0;
            }

            if (exitCode != 0)// || !long.TryParse(apkAnalyzerOutput, out var result))
            {
                throw new Exception($"apkanalyzer failed to estimate the apk size. Output:\n{apkAnalyzerOutput}");
            }

            return result;