IL2CPP Build Error in Unity 6 for Android project (System.InvalidCastException)

I’m on Unity 6000.0.32f1, and receive this error when attempting to build a project using IL2CPP. Any ideas what could be the issue?

C:\Program Files\Unity\Hub\Editor\6000.0.32f1\Editor\Data\il2cpp\build\deploy\il2cpp.exe @Library\Bee\artifacts\rsp\5001542772473845456.rsp
Error: IL2CPP error (no further information about what managed code was being converted is available)
System.AggregateException: One or more errors occurred. (Specified cast is not valid.)
 ---> System.InvalidCastException: Specified cast is not valid.
   at System.Runtime.TypeCast.CheckCastClass(MethodTable*, Object) + 0x31
   at Unity.IL2CPP.DataModel.BuildLogic.Populaters.DefinitionPopulater.PopulateCustomAttrProvider(CecilSourcedAssemblyData, ICustomAttributeProvider) + 0x1a2
   at Unity.IL2CPP.DataModel.BuildLogic.Populaters.DefinitionPopulater.PopulateTypeDef(TypeContext, UnderConstructionMember`2) + 0x94
   at Unity.IL2CPP.DataModel.BuildLogic.DataModelBuilder.<PopulateCecilSourcedDefinitions>b__14_1(UnderConstructionMember`2 typeDef) + 0x143
   at System.Threading.Tasks.Parallel.<>c__DisplayClass44_0`2.<PartitionerForEachWorker>b__1(IEnumerator& partitionState, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) + 0x286
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception) + 0x13
   at System.Threading.Tasks.Parallel.<>c__DisplayClass44_0`2.<PartitionerForEachWorker>b__1(IEnumerator& partitionState, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) + 0x646
   at System.Threading.Tasks.TaskReplicator.Replica.Execute() + 0x3a
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.TaskReplicator.Run[TState](TaskReplicator.ReplicatableUserAction`1, ParallelOptions, Boolean) + 0x15d
   at System.Threading.Tasks.Parallel.PartitionerForEachWorker[TSource, TLocal](Partitioner`1, ParallelOptions, Action`1, Action`2, Action`3, Func`4, Func`5, Func`1, Action`1) + 0x23f
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
   at System.Threading.Tasks.Parallel.ThrowSingleCancellationExceptionOrOtherException(ICollection, CancellationToken, Exception) + 0x31
   at System.Threading.Tasks.Parallel.PartitionerForEachWorker[TSource, TLocal](Partitioner`1, ParallelOptions, Action`1, Action`2, Action`3, Func`4, Func`5, Func`1, Action`1) + 0x3e0
   at System.Threading.Tasks.Parallel.ForEachWorker[TSource, TLocal](IEnumerable`1, ParallelOptions, Action`1, Action`2, Action`3, Func`4, Func`5, Func`1, Action`1) + 0x17b
   at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1, Action`1) + 0x68
   at Unity.IL2CPP.DataModel.BuildLogic.Utils.ParallelHelpers.ForEach[TSource](IEnumerable`1, Action`1, Boolean) + 0xc4
   at Unity.IL2CPP.DataModel.BuildLogic.DataModelBuilder.PopulateCecilSourcedDefinitions(ReadOnlyCollection`1) + 0x2e6
   at Unity.IL2CPP.DataModel.BuildLogic.DataModelBuilder.Build() + 0x23c
   at Unity.IL2CPP.AssemblyConversion.Phases.InitializePhase.Run(AssemblyConversionContext) + 0x319
   at Unity.IL2CPP.AssemblyConversion.Classic.ClassicConverter.Run(AssemblyConversionContext) + 0x11
   at Unity.IL2CPP.AssemblyConversion.AssemblyConverter.ConvertAssemblies(AssemblyConversionContext, ConversionMode) + 0x24
   at Unity.IL2CPP.AssemblyConversion.AssemblyConverter.ConvertAssemblies(TinyProfiler2, AssemblyConversionInputData, AssemblyConversionParameters, AssemblyConversionInputDataForTopLevelAccess) + 0x17f

UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

Basic questions: did you trace back your changes until you didn’t get this error anymore?

What could work to find more details:
Increase the verbosity of Unity’s logging:

  • In Unity Editor, go to Preferences > External Tools and enable additional logging.
  • In the Build Settings, set the Script Debugging option to true.
  • Rebuild the project to get more detailed information about the error.

@peterklaverlive Thanks for the help.

I spent over a day trying to figure out what part of my code was causing the build failure, with no luck. It’s a new project that I’ve been developing for a few months, and only just now started trying to do builds outside of Unity.

In Preferences > External Tools, I do not see any options to enable additional logging.

I enabled Script Debugging and rebuilt, I got the same error with no additional information.

I managed to fix this by changing Managed Stripping Level from Minimal to Low in Player Settings

1 Like