Builld fails when trying to build app bundles instead of .apk

Hi! I’m trying to build on a cloud build configuration that has worked before, but for some reason fails now. The cause of the fail seems to be the “Build app bundles (.aab) instead of an APK” since the build doesn’t fail if I uncheck it. It also fails on commits that have been able to get built before. We’re using latest 2021.3

The imortant logs in question (I think) is

2024-05-14T12:21:34Z - Unity] Android PostProcess task "Calling IPostGenerateGradleAndroidProject callbacks" took 40.7147 ms
[2024-05-14T12:21:34Z - Unity] Android PostProcess task "Performance Reporting Symbol Upload" took 0.2088 ms
[2024-05-14T12:21:34Z - Unity] DisplayProgressbar: Building Gradle project
[2024-05-14T12:23:22Z - Unity] Android PostProcess task "Building Gradle project" took 108306.8395 ms
[2024-05-14T12:23:22Z - Unity] DisplayProgressbar: Processing Mapping File
[2024-05-14T12:23:22Z - Unity] Android PostProcess task "Processing Mapping File" took 6.4744 ms
[2024-05-14T12:23:22Z - Unity] DisplayProgressbar: Moving output package(s)
[warning] [2024-05-14T12:23:22Z - Unity] EXCEPTION: UnityException: Moving final Android package(s) failed
[2024-05-14T12:23:22Z - Unity] Can't remove non-empty directory 'BUILD_PATH/p/unity_project/temp20240514-2653-t4ubw4/Android - Production.aab'.
[warning] [2024-05-14T12:23:22Z - Unity] UnityException: Moving final Android package(s) failed
[2024-05-14T12:23:22Z - Unity] Can't remove non-empty directory 'BUILD_PATH/p/unity_project/temp20240514-2653-t4ubw4/Android - Production.aab'.
[warning] [2024-05-14T12:23:22Z - Unity] Rethrow as BuildFailedException: Exception of type 'UnityEditor.Build.BuildFailedException' was thrown.
[2024-05-14T12:23:22Z - Unity]   at UnityEditor.Android.PostProcessor.CancelPostProcess.AbortBuild (System.String title, System.String message, System.Exception ex) [0x00020] in <c2ac05f9fb8d4a3784f58cfb37e9c9a1>:0
[2024-05-14T12:23:22Z - Unity]   at UnityEditor.Android.PostProcessor.CancelPostProcess.AbortBuild (System.String title, System.String message) [0x00014] in <c2ac05f9fb8d4a3784f58cfb37e9c9a1>:0
[2024-05-14T12:23:22Z - Unity]   at UnityEditor.Android.PostProcessor.Tasks.MoveFinalPackage.Clean (System.String path) [0x0002e] in <c2ac05f9fb8d4a3784f58cfb37e9c9a1>:0
[2024-05-14T12:23:22Z - Unity]   at UnityEditor.Android.PostProcessor.Tasks.MoveFinalPackage.CleanAndroidAppBundle () [0x0000e] in <c2ac05f9fb8d4a3784f58cfb37e9c9a1>:0
[2024-05-14T12:23:22Z - Unity]   at UnityEditor.Android.PostProcessor.Tasks.MoveFinalPackage.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context) [0x0014e] in <c2ac05f9fb8d4a3784f58cfb37e9c9a1>:0
[2024-05-14T12:23:22Z - Unity]   at UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context) [0x00078] in <c2ac05f9fb8d4a3784f58cfb37e9c9a1>:0
[2024-05-14T12:23:22Z - Unity]   at UnityEditor.Android.PostProcessAndroidPlayer.PostProcess (UnityEditor.Modules.BuildPostProcessArgs args, AndroidPlayerBuildProgram.Data.AndroidPlayerBuildProgramOutput buildProgramOutput) [0x001a5] in <c2ac05f9fb8d4a3784f58cfb37e9c9a1>:0
[2024-05-14T12:23:22Z - Unity]   at UnityEditor.Android.AndroidBuildPostprocessor.PostProcess (UnityEditor.Modules.BuildPostProcessArgs args, UnityEditor.BuildProperties& outProperties) [0x00024] in <c2ac05f9fb8d4a3784f58cfb37e9c9a1>:0
[2024-05-14T12:23:22Z - Unity]   at UnityEditor.PostprocessBuildPlayer.Postprocess (UnityEditor.BuildTargetGroup targetGroup, UnityEditor.BuildTarget target, System.Int32 subtarget, System.String installPath, System.String companyName, System.String productName, System.Int32 width, System.Int32 height, UnityEditor.BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.Build.Reporting.BuildReport report) [0x000e5] in <fe1491035895425882642e3ab9c18f22>:0
[2024-05-14T12:23:22Z - Unity] UnityEditor.BuildPipeline:BuildPlayerInternalNoCheck(String[], String, String, BuildTargetGroup, BuildTarget, Int32, BuildOptions, String[], Boolean)
[2024-05-14T12:23:22Z - Unity] UnityEditor.BuildPipeline:BuildPlayerInternal(String[], String, String, BuildTargetGroup, BuildTarget, Int32, BuildOptions, String[])
[2024-05-14T12:23:22Z - Unity] UnityEditor.BuildPipeline:BuildPlayer(String[], String, String, BuildTargetGroup, BuildTarget, Int32, BuildOptions, String[])
[2024-05-14T12:23:22Z - Unity] UnityEditor.BuildPipeline:BuildPlayer(BuildPlayerOptions)
[2024-05-14T12:23:22Z - Unity] UnityEditor.CloudBuild.Builder:BuildPlayerDefault(BuildPlayerOptions)
[2024-05-14T12:23:22Z - Unity] UnityEditor.CloudBuild.Builder:BuildPlayer(BuildPlayerOptions)
[2024-05-14T12:23:22Z - Unity] UnityEditor.CloudBuild.Builder:Build()
[2024-05-14T12:23:22Z - Unity]
[2024-05-14T12:23:22Z - Unity] Unloading 1308 Unused Serialized files (Serialized files now loaded: 0)
[2024-05-14T12:23:22Z - Unity] Unloading 5491 unused Assets / (14.8 MB). Loaded Objects now: 5561.
[2024-05-14T12:23:22Z - Unity] Memory consumption went from 308.1 MB to 293.3 MB.
[2024-05-14T12:23:22Z - Unity] Total: 20.999100 ms (FindLiveObjects: 1.326200 ms CreateObjectMapping: 0.559100 ms MarkObjects: 9.566200 ms  DeleteObjects: 9.546100 ms)
[2024-05-14T12:23:22Z - Unity]
[2024-05-14T12:23:22Z - Unity] Build Finished, Result: Failure.

It sounds like there might be a regression in the latest version of Unity 2021.3. If you are set to use Latest then it will automatically start using whatever version is released. I’d suggest looking back at the commits that passed previously and building using the version of Unity that worked at that point in time.

If they are different, then you’ll want to open up a bug within the Editor. If they are the same version then I’d suggest checking your dependency versions to make sure they are all pinned or if they changed between the passing and failing builds.

Writing an update here if someone have the same problem. After a lot of cloud building I think I narrowed down the problem to the settinng PlayerSettings.Android.buildApkPerCpuArchitecture that was set to true in the Pre-Export method. It’s a bit strange since that property should be ignored when building an app bundle, at least it says so for the editor, but i looks like that did the trick!

2 Likes

I’m running into this issue in Unity Cloud Build Automation with Unity 6000.0.23f1. @JussiKremant did you ever find a workaround? My idea is to create an ENV var, set it in my AAB’s build configuration, and create a PreExport script that undoes the buildApkPerCpuArchitecture setting, i.e:

#if UNITY_ANDROID && ANDROID_AAB_BUILD
  PlayerSettings.Android.buildApkPerCpuArchitecture = false;
#endif 

Although I’m not sure if Unity Cloud Build’s pre-export script will execute before or after mine.

Edit:

My solution did fix the issue. Unity Cloud Build IS in fact setting buildApkPerCpuArchitecture to true even when Build app bundles (.aab) instead of an APK is checked. Here’s some debug logging from my PreExport script to prove as much:

[2024-10-30T22:40:06Z - Unity] CloudBuildHelper.PreExport: Google Play AAB Settings:
[2024-10-30T22:40:06Z - Unity]   buildApkPerCpuArchitecture: True
[2024-10-30T22:40:06Z - Unity]   buildAppBundle:             True

For anyone else who could benefit from this, here’s my workaround for this:

My Pre-Export Script and Unity Cloud Build Automation Configuration

PreExport script, saved as CloudBuildHelper.cs in a(ny) folder named Editor

#if UNITY_CLOUD_BUILD

using UnityEditor;
using UnityEngine;

public class CloudBuildHelper : MonoBehaviour {
  public static void PreExport(UnityEngine.CloudBuild.BuildManifestObject manifest) {
    #if UNITY_ANDROID && ANDROID_AAB_BUILD
      PlayerSettings.Android.buildApkPerCpuArchitecture = false;
    #endif
  }
}

#endif

In my Cloud Build Android Build Target’s Configuration, I have set the Pre-Export Method to CloudBuildHelper.PreExport and added a #define for ANDROID_AAB_BUILD

Hope this helps

@Benjamin-Gooding is this a known regression for Unity Cloud Build Automation when using Unity 6?

1 Like

From an Editor side of things not that I’m aware of. Are you using Build Profiles by any chance? If so, you could use our experimental cloud build support for profiles. I believe that using a build profile should avoid us setting any player settings ourselves and instead completely rely on the build profile’s configuration.

I can also create a bug in our system that will make sure that buildApkPerCpuArchitecture is not enabled when buildAppBundle is true.

I’m not using Build Profiles yet, but I like the concept and UI in the editor. If you need beta-testers I’d be happy to try it out. But if it’s an all-or-nothing feature in Unity Cloud I’ll have to pass for now.

I think that could help avoid issues for other folks. Thank goodness for this post otherwise I wouldn’t ever have figured it out. Let me know if I can help provide more details or test cases.