IL2CPP Build Error: UnityEditor.dll assembly is referenced by user code, but this is not allowed

Trying to build with IL2CPP am getting:

UnityEditor.dll assembly is referenced by user code, but this is not allowed.
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
Build completed with a result of 'Failed' in 22 seconds (22138 ms)
UnityEditor.dll assembly is referenced by user code, but this is not allowed.
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

Additionally:

ProfileValueReference: GetValue called with empty id.
UnityEditor.AddressableAssets.GUI.AddressableAssetsSettingsGroupEditor:BuildAddressablesWithResult (UnityEditor.AddressableAssets.GUI.AddressableAssetsSettingsGroupEditor/BuildMenuContext)
AddressablesPlayerBuildProcessor:DefaultBuild (UnityEditor.AddressableAssets.Settings.AddressableAssetSettings) (at ./Library/PackageCache/com.unity.addressables/Editor/Build/AddressablesPlayerBuildProcessor.cs:143)
AddressablesPlayerBuildProcessor:PrepareForPlayerbuild (UnityEditor.AddressableAssets.Settings.AddressableAssetSettings,UnityEditor.Build.BuildPlayerContext,bool) (at ./Library/PackageCache/com.unity.addressables/Editor/Build/AddressablesPlayerBuildProcessor.cs:100)
AddressablesPlayerBuildProcessor:PrepareForBuild (UnityEditor.Build.BuildPlayerContext) (at ./Library/PackageCache/com.unity.addressables/Editor/Build/AddressablesPlayerBuildProcessor.cs:79)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
Json build layout written to Library/com.unity.addressables/buildlayout.json
UnityEditor.AddressableAssets.GUI.AddressableAssetsSettingsGroupEditor:BuildAddressablesWithResult (UnityEditor.AddressableAssets.GUI.AddressableAssetsSettingsGroupEditor/BuildMenuContext)
AddressablesPlayerBuildProcessor:DefaultBuild (UnityEditor.AddressableAssets.Settings.AddressableAssetSettings) (at ./Library/PackageCache/com.unity.addressables/Editor/Build/AddressablesPlayerBuildProcessor.cs:143)
AddressablesPlayerBuildProcessor:PrepareForPlayerbuild (UnityEditor.AddressableAssets.Settings.AddressableAssetSettings,UnityEditor.Build.BuildPlayerContext,bool) (at ./Library/PackageCache/com.unity.addressables/Editor/Build/AddressablesPlayerBuildProcessor.cs:100)
AddressablesPlayerBuildProcessor:PrepareForBuild (UnityEditor.Build.BuildPlayerContext) (at ./Library/PackageCache/com.unity.addressables/Editor/Build/AddressablesPlayerBuildProcessor.cs:79)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
Addressable content successfully built (duration : 0:00:13,972)
UnityEditor.AddressableAssets.GUI.AddressableAssetsSettingsGroupEditor:BuildAddressablesWithResult (UnityEditor.AddressableAssets.GUI.AddressableAssetsSettingsGroupEditor/BuildMenuContext)
AddressablesPlayerBuildProcessor:DefaultBuild (UnityEditor.AddressableAssets.Settings.AddressableAssetSettings) (at ./Library/PackageCache/com.unity.addressables/Editor/Build/AddressablesPlayerBuildProcessor.cs:143)
AddressablesPlayerBuildProcessor:PrepareForPlayerbuild (UnityEditor.AddressableAssets.Settings.AddressableAssetSettings,UnityEditor.Build.BuildPlayerContext,bool) (at ./Library/PackageCache/com.unity.addressables/Editor/Build/AddressablesPlayerBuildProcessor.cs:100)
AddressablesPlayerBuildProcessor:PrepareForBuild (UnityEditor.Build.BuildPlayerContext) (at ./Library/PackageCache/com.unity.addressables/Editor/Build/AddressablesPlayerBuildProcessor.cs:79)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

No script of mine has using UnityEditor;
Already tried deleting Library and doing a Clean Build.

Unity Editor 6000.0.32f1
Addressables 2.2.2

Any solutions?

I get the same error in 6000.0.33f1, no Addressables

This is an older project with custom build pipeline that we’re trying to upgrade from Unity2021. There it worked all fine, with all the uses of UnityEditor space either blocked by #if UNITY_EDITOR definitions or in Editor folders.

It needn’t have the using. It could also be inlined like: UnityEditor.AssetDatabase.CreateAsset(..)

Did you try a Mono build? It would be strange if that worked but not IL2CPP. In that case I would scan for IL2CPP conditionals, these may be much rarer than UNITY_EDITOR.

Check for any and all uses of UnityEditor in your code and if that reveals nothing respectively all uses are safeguarded, you may also have to scan package and assets code.

Note that merely having using UnityEditor; in a script will not trigger the issue. I have my IDE set up to always include this line. But it can sometimes be hard to spot something like using UnityEditor.SceneManagement; (which will trigger build failures all by itself, and I think this goes for any sub-namespace under UnityEditor) because it can easily mask as using UnityEngine.SceneManagement;

Worth noting: if you don’t use an Assembly Definition in the “Editor” folder or any of its parents, then this will work. However, if someone were to add a new Asmdef under “This” in the following path Assets/This/That/Some/Folder/Editor then all of a sudden the scripts in the Editor folder will be compiled under the new Asmdef, which would make them runtime scripts unless you add an editor-only Asmdef to the Editor folder.

This has been a traditional failure point for those introducing or new to Asmdefs. :wink:

I fixed the issue, at least for now, by removing some unused plugins and assets and the build passed successfully. Probably one or more had these problems but didn’t manage to pinpoint a specific plugin.

EDIT:
I believe that the asset “Instant Screenshot” by Saad Khawaja is causing the build error and disabling it for “Editor” allows for a normal build