Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

IL2CPP Android build failing with Incremental Player Build failed

Discussion in 'Editor & General Support' started by joaoborlido17465, Mar 8, 2022.

  1. mike-voorhees

    mike-voorhees

    Unity Technologies

    Joined:
    Aug 9, 2016
    Posts:
    45
    Your project is hitting the NullReferenceException for the same underlying conditions as the last user. However, yours is caused by a different set of circumstances.

    The tl;dr is, I'm guessing your project has a System.Memory.dll included that is incompatible with the class library that we use. I don't have your full project, nor what version of Unity you are using, so I'm taking some guesses here.

    * There was a bug fix that landed in 2021.2.12f1 that added the mechanism to prevent a System.Memory.dll in your project from being included in a build. If you are on an older version of 2021.x, try updating.

    * If you want a workaround, I'm guessing that if you remove System.Memory.dll from the project, or exclude from the standalone build, that should fix the issue. Having said that, I have not been able to recreate a project that allows a System.Memory.dll in the project to be used during a player build. I'm using Unity 2021.3.9f1.

    If you are using a Unity version 2021.2.12f1 or newer, please submit a new bug report with a full Unity project. We may have a bug in our mechanism to prevent a System.Memory.dll in the project from being used. Try to mention System.Memory.dll in addition to the NullReferenceException. And post the incident link here. That way we can make sure it doesn't get flagged as a duplicate again.

    If you want the nitty gritty details, here's a further explanation of what's going on.

    Going into UnityLinker (i.e. Managed Code Stripping) here is an example of the problematic IL. This pattern shows up in a few places in the assemblies you sent in, but they are all the same issue.

    In Grpc.Core.Api.dll there are methods such as

    .method public hidebysig newslot virtual
    instance class [System.Memory]System.Buffers.IBufferWriter`1<uint8>
    GetBufferWriter() cil managed


    The important thing to note is that System.Buffers.IBufferWriter`1 claims to live in System.Memory.
    And then in Assembly-CSharp.dll there is IL such as


    IL_001e: callvirt instance class [mscorlib]System.Buffers.IBufferWriter`1<uint8> [Grpc.Core.Api]Grpc.Core.SerializationContext::GetBufferWriter()


    Note that here System.Buffers.IBufferWriter`1 lives in mscorlib.

    This situation is okay IF System.Memory.dll contains a TypeForwarder for System.Buffers.IBufferWriter`1. However, in your project, it does not. The System.Memory in your project contains implementation classes.

    What's going to happen during code stripping is that the linker will see the call to Grpc.Core.SerializationContext::GetBufferWriter(). It will resolve that method. Then it will mark the dependencies of the method definition. The method definition would be the first IL snippet from Grpc.Core.Api.dll above. This results in the System.Buffers.IBufferWriter`1 being marked in System.Memory.dll. When something is "marked" during code stripping that means that it will survive code stripping.

    Recall that Assembly-CSharp.dll needs `[mscorlib]System.Buffers.IBufferWriter`1<uint8>`. The linker does not mark the types used by MethodReferences. This means that the System.Buffers.IBufferWriter`1 in mscorlib is stripped.

    And now we have the conditions necessary for il2cpp.exe to crash with the NullReferenceException.

    System.Memory is a bit of a special case. If you went to https://www.nuget.org/packages/System.Memory downloaded the package, extracted it, and copied "system.memory.4.5.5\lib\net461\System.Memory.dll" into your project you would get an implementation assembly. That is, a variation of System.Memory that contains actual implementations of types rather than type forwarders. If you copied "system.memory.4.5.5\lib\netstandard2.0\System.Memory.dll" into your project you would also get an implementation assembly.

    Here is where System.Memory is a bit of a special case. In the mono class libraries, the implementation classes that appear in System.Memory are defined in mscorlib.dll. mono has a facade assembly called System.Memory.dll containing type forwarders for the types in System.Memory.dll. We distribute this facade assembly with Unity and it is referenced automatically. And as of 2021.2.12f1 it shouldn't be possible trump this reference.

    -Mike
     
    Petr777 and Qbit86 like this.
  2. Liam2349

    Liam2349

    Joined:
    Mar 10, 2016
    Posts:
    78
    mike-voorhees, thank you for mentioning System.Memory.

    I had the same error with IL2CPP, using Unity 2022.1.16f1, and recalled that earlier in my project I had imported some assemblies relating to SignalRCore, System.Memory included.

    I ended up deleting all of the assemblies relating to this, and then the build succeeded.
     
    mike-voorhees likes this.
  3. hehui568350701

    hehui568350701

    Joined:
    May 17, 2023
    Posts:
    5
    I had the same error with IL2CPP, using Unity 2022.3.2f1c1.

    Error Log:


    C:\Program Files\Unity\Hub\Editor\2022.3.2f1c1\Editor\Data\il2cpp\build\deploy\il2cpp.exe @Library\Bee\artifacts\rsp\14036664738710574387.rsp
    Error: IL2CPP error for method 'System.Double GEOVIS.Utils.Math.Matrix4x4d::get_Item(System.Int32)' in assembly 'F:\GIS\GeovisEarth20230223_Unity\Earth\InnerWorld20230223\Library\Bee\artifacts\Android\ManagedStripped\Assembly-CSharp.dll'
    System.NullReferenceException: Object reference not set to an instance of an object.
    at Unity.IL2CPP.DataModel.Awesome.CFG.ControlFlowGraphBuilder.ConnectBlock(InstructionBlock block)
    at Unity.IL2CPP.DataModel.Awesome.CFG.ControlFlowGraphBuilder.ConnectBlocks()
    at Unity.IL2CPP.DataModel.Awesome.CFG.ControlFlowGraphBuilder.BuildBasicBlocks()
    at Unity.IL2CPP.MethodBodyWriter..ctor(MethodWriteContext context, IGeneratedMethodCodeWriter writer, IRuntimeMetadataAccess metadataAccess)
    at Unity.IL2CPP.MethodWriter.WriteMethodBody(MethodWriteContext context, IGeneratedMethodCodeWriter methodBodyWriter, IRuntimeMetadataAccess metadataAccess)
    at Unity.IL2CPP.CodeWriters.CodeWriterExtensions.WriteMethodWithMetadataInitialization(IGeneratedMethodCodeWriter writer, String methodSignature, Action`2 writeMethodBody, String uniqueIdentifier, MethodReference methodRef, WritingMethodFor writingMethodFor)
    at Unity.IL2CPP.MethodWriter.WriteMethodDefinition(AssemblyWriteContext context, IGeneratedMethodCodeWriter writer, MethodReference method)
    at Unity.IL2CPP.SourceWriter.WriteTypesMethods(SourceWritingContext context, IGeneratedMethodCodeWriter writer, TypeWritingInformation& writingInformation, NPath filePath, Boolean writeMarshalingDefinitions)
    at Unity.IL2CPP.SourceWriters.SourceWriterBase`2.WriteItem(StreamWorkItemData`2 data)
    at Unity.IL2CPP.Contexts.Scheduling.Streams.FileLevelParallelStreamManager`3.WorkerWriteItemsToFile(WorkItemData`2 data)
    at Unity.IL2CPP.Contexts.Scheduling.PhaseWorkScheduler`1.WorkerLoop(Object data)
    Error: IL2CPP error for method 'System.Int32 SpaceGraphicsToolkit.SgtHelper::GetOffset(UnityEngine.TextureFormat,System.Int32)' in assembly 'F:\GIS\GeovisEarth20230223_Unity\Earth\InnerWorld20230223\Library\Bee\artifacts\Android\ManagedStripped\Assembly-CSharp.dll'
    System.NullReferenceException: Object reference not set to an instance of an object.
    at Unity.IL2CPP.DataModel.Awesome.CFG.ControlFlowGraphBuilder.ConnectBlock(InstructionBlock block)
    at Unity.IL2CPP.DataModel.Awesome.CFG.ControlFlowGraphBuilder.ConnectBlocks()
    at Unity.IL2CPP.DataModel.Awesome.CFG.ControlFlowGraphBuilder.BuildBasicBlocks()
    at Unity.IL2CPP.MethodBodyWriter..ctor(MethodWriteContext context, IGeneratedMethodCodeWriter writer, IRuntimeMetadataAccess metadataAccess)
    at Unity.IL2CPP.MethodWriter.WriteMethodBody(MethodWriteContext context, IGeneratedMethodCodeWriter methodBodyWriter, IRuntimeMetadataAccess metadataAccess)
    at Unity.IL2CPP.CodeWriters.CodeWriterExtensions.WriteMethodWithMetadataInitialization(IGeneratedMethodCodeWriter writer, String methodSignature, Action`2 writeMethodBody, String uniqueIdentifier, MethodReference methodRef, WritingMethodFor writingMethodFor)
    at Unity.IL2CPP.MethodWriter.WriteMethodDefinition(AssemblyWriteContext context, IGeneratedMethodCodeWriter writer, MethodReference method)
    at Unity.IL2CPP.SourceWriter.WriteTypesMethods(SourceWritingContext context, IGeneratedMethodCodeWriter writer, TypeWritingInformation& writingInformation, NPath filePath, Boolean writeMarshalingDefinitions)
    at Unity.IL2CPP.SourceWriters.SourceWriterBase`2.WriteItem(StreamWorkItemData`2 data)
    at Unity.IL2CPP.Contexts.Scheduling.Streams.FileLevelParallelStreamManager`3.WorkerWriteItemsToFile(WorkItemData`2 data)
    at Unity.IL2CPP.Contexts.Scheduling.PhaseWorkScheduler`1.WorkerLoop(Object data)
    Error: IL2CPP error for method 'System.Void RuntimeGizmos.TransformGizmo::GetTarget()' in assembly 'F:\GIS\GeovisEarth20230223_Unity\Earth\InnerWorld20230223\Library\Bee\artifacts\Android\ManagedStripped\Assembly-CSharp.dll'
    System.NullReferenceException: Object reference not set to an instance of an object.
    at Unity.IL2CPP.DataModel.Awesome.CFG.ControlFlowGraphBuilder.ConnectBlock(InstructionBlock block)
    at Unity.IL2CPP.DataModel.Awesome.CFG.ControlFlowGraphBuilder.ConnectBlocks()
    at Unity.IL2CPP.DataModel.Awesome.CFG.ControlFlowGraphBuilder.BuildBasicBlocks()
    at Unity.IL2CPP.MethodBodyWriter..ctor(MethodWriteContext context, IGeneratedMethodCodeWriter writer, IRuntimeMetadataAccess metadataAccess)
    at Unity.IL2CPP.MethodWriter.WriteMethodBody(MethodWriteContext context, IGeneratedMethodCodeWriter methodBodyWriter, IRuntimeMetadataAccess metadataAccess)
    at Unity.IL2CPP.CodeWriters.CodeWriterExtensions.WriteMethodWithMetadataInitialization(IGeneratedMethodCodeWriter writer, String methodSignature, Action`2 writeMethodBody, String uniqueIdentifier, MethodReference methodRef, WritingMethodFor writingMethodFor)
    at Unity.IL2CPP.MethodWriter.WriteMethodDefinition(AssemblyWriteContext context, IGeneratedMethodCodeWriter writer, MethodReference method)
    at Unity.IL2CPP.SourceWriter.WriteTypesMethods(SourceWritingContext context, IGeneratedMethodCodeWriter writer, TypeWritingInformation& writingInformation, NPath filePath, Boolean writeMarshalingDefinitions)
    at Unity.IL2CPP.SourceWriters.SourceWriterBase`2.WriteItem(StreamWorkItemData`2 data)
    at Unity.IL2CPP.Contexts.Scheduling.Streams.FileLevelParallelStreamManager`3.WorkerWriteItemsToFile(WorkItemData`2 data)
    at Unity.IL2CPP.Contexts.Scheduling.PhaseWorkScheduler`1.WorkerLoop(Object data) .......