Search Unity

Asset Bundles & il2cpp code stripping.

Discussion in 'Editor & General Support' started by pjohalloran, Nov 11, 2015.

  1. pjohalloran

    pjohalloran

    Joined:
    Jan 11, 2013
    Posts:
    19
    Hi,

    My project has a bunch of asset bundles. I want to ensure that every UnityEngine component they reference are not stripped in the build process for iOS. Therefore I need to add all the components they reference to the link.xml file.

    I want to be able to do this as a post asset bundle build step by analyzing the asset bundle manifest files and generating a link.xml from them which i think should be possible.

    In the Unity 5 asset bundle manifests, the following example data is listed in 1 of my projects examples:
    Code (CSharp):
    1. ClassTypes:
    2. - Class: 1
    3.   Script: {instanceID: 0}
    4. - Class: 21
    5.   Script: {instanceID: 0}
    6. - Class: 28
    7.   Script: {instanceID: 0}
    8. - Class: 48
    9.   Script: {instanceID: 0}
    10. - Class: 114
    11.   Script: {fileID: 1980459831, guid: f5f67c52d1564df4a8936ccd202a3bd8, type: 3}
    12. - Class: 114
    13.   Script: {fileID: 1301386320, guid: f5f67c52d1564df4a8936ccd202a3bd8, type: 3}
    14. - Class: 114
    15.   Script: {fileID: 708705254, guid: f5f67c52d1564df4a8936ccd202a3bd8, type: 3}
    16. - Class: 115
    17.   Script: {instanceID: 0}
    18. - Class: 128
    19.   Script: {instanceID: 0}
    20. - Class: 222
    21.   Script: {instanceID: 0}
    22. - Class: 223
    23.   Script: {instanceID: 0}
    24. - Class: 224
    25.   Script: {instanceID: 0}
    There is also the YAML Class ID page in the Unity docs, http://docs.unity3d.com/Manual/ClassIDReference.html

    How do I automate this and generate the resulting "link.xml" file?
    e.g.
    Code (CSharp):
    1.     <linker>
    2.         <assembly fullname="UnityEngine">
    3.             <type fullname="UnityEngine.MeshFilter" preserve="all"/>
    4.             <type fullname="UnityEngine.MeshRenderer" preserve="all"/>
    5.             <type fullname="UnityEngine.Animator" preserve="all"/>
    6.             <type fullname="UnityEngine.Light" preserve="all"/>
    7.             <type fullname="UnityEngine.FlareLayer" preserve="all"/>
    8.             <type fullname="UnityEngine.AudioListener" preserve="all"/>
    9.             <type fullname="UnityEngine.Mesh" preserve="all"/>
    10.             <type fullname="UnityEngine.Avatar" preserve="all"/>
    11.             <type fullname="UnityEngine.UI.Canvas" preserve="all"/>
    12.             <type fullname="UnityEngine.UI.CanvasRenderer" preserve="all"/>
    13.         </assembly>
    14.     </linker>
    15.  
    Is there some Editor helper function that will convert class id to the Namespace and classname so i can ensure it will not be stripped?

    Thanks,
    PJ
     
  2. MarimoAli

    MarimoAli

    Joined:
    Jan 16, 2015
    Posts:
    18
    Hello PJ,

    from what I have understood after reading the stripping part on this topic ( http://forum.unity3d.com/threads/il2cpp-build-size-improvements.322079/ ), is that it should generally not be used when building for iOS, as il2cpp does it by itself. I know this isn't a direct answer for your problem, but I just thought I should mention it.

    -Ali
     
  3. pjohalloran

    pjohalloran

    Joined:
    Jan 11, 2013
    Posts:
    19
    Hi MarimoAli,
    I think its still necessary to use link.xml to prevent stripping certain files.

    Post 15 from JohnPeterson confirms in that post i believe
     
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    5,676
    @pjohalloran

    I don't believe there is a good way to do this now in an Editor script. One option you could try is to use a static link.xml file that preserves everything in UnityEngine.dll. If you don't have strict size requirements, or you end up using most of UnityEngine.dll anyway, this might work:

    Code (CSharp):
    1.  
    2. <linker>
    3.         <assembly fullname="UnityEngine" preserve="all"/>
    4. </linker>
    5.  
     
  5. pjohalloran

    pjohalloran

    Joined:
    Jan 11, 2013
    Posts:
    19
    Hello Josh,
    The reason this came up for me is that i am looking into Unitys AssetBundle example system in https://www.assetstore.unity3d.com/en/#!/content/45836

    It has a scene that is minimal and loads asset bundles in. UnityEngine is mostly stripped out on iOS it seems since the scene is empty.

    I tried using your idea for link.xml but im getting errors when loading asset bundles
    Code (CSharp):
    1.  
    2. <linker>
    3.         <assembly fullname="UnityEngine" preserve="all"/>
    4.         <assembly fullname="UnityEngine.UI" preserve="all"/>
    5. </linker>
    6.  
    Which is CanvasRenderer which i thought " <assembly fullname="UnityEngine.UI" preserve="all"/>" would have prevented stripping for?

    I would really like if the build AssetBundle process had some way of generating this script to avoid such issues. Can we get it in at all? Or can you guys provide a way to map a class id from the asset bundle manifests to a class name? That way it would give devs the possibility to do it ourselves?
     
    Last edited: Nov 11, 2015
  6. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    5,676
    @pjohalloran

    It sounds like there is some additional bug in the stripping system here. In general though, I think it would be better if the asset bundle system could just handle this quietly, preserving all of the proper types. Is it possible for you to submit a bug report with a project that reproduces this issue?
     
  7. pjohalloran

    pjohalloran

    Joined:
    Jan 11, 2013
    Posts:
    19
    Yes, i think there is something wrong with the stripping system. (note It works if i use this though in my link.xml:

    Do you mean that UnityEngine and UnityEngine.UI should be preserved in il2cpp code stripping process? I agree I guess that this should be handled quietly by asset bundle system. I'm not sure how it could though? Or what effect that would have on the final app size?

    Yes, I can submit a bug report with the minimal project I have.
     
  8. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    5,676
    @pjohalloran

    No, they should not necessarily be preserved. However, the error about being unable to produce a class with a given ID indicates that something is probably wrong on the Unity side, I think.
     
  9. pjohalloran

    pjohalloran

    Joined:
    Jan 11, 2013
    Posts:
    19
    Ok, If I submit a bug report, is there some way I could tag you on it Josh?
     
  10. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    5,676
    @pjohalloran

    Sure, just reply here with the bug report number, and I'll have a look at it.
     
  11. pjohalloran

    pjohalloran

    Joined:
    Jan 11, 2013
    Posts:
    19
    Thanks Josh. Case number is :
    743844
     
  12. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    5,676
    @pjohalloran

    Thanks, we will investigate this bug report.
     
  13. pjohalloran

    pjohalloran

    Joined:
    Jan 11, 2013
    Posts:
    19
    Thank you, if you need me to provide you more info on the sample project, reply here and i will get back to you asap.
     
  14. pjohalloran

    pjohalloran

    Joined:
    Jan 11, 2013
    Posts:
    19
  15. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,565
    Another tip:

    Code (csharp):
    1.  
    2. namespace Preservation
    3. {
    4.    public class PreserveAttribute : Attribute
    5.    {
    6.        public PreserveAttribute() {}
    7.    }
    8. }
    9.  
    Put this in your project and apply this attribute to everything that you don't want stripped. The IL2CPP stripping engine should ignore it when it finds the attribute and not strip it.

    Pu
     
    pjohalloran likes this.
  16. Miguel-Ferreira

    Miguel-Ferreira

    Joined:
    May 8, 2015
    Posts:
    90
    Hi,

    I'm having a very similar problem (http://forum.unity3d.com/threads/asset-bundle-and-code-stripping.380619/)

    In what version was this fixed? How does it behave now? Is the link.xml automatically changed when BuildPipeline.BuildAssetBundles is called?

    I was looking for a solution that didn't force me to manually change every single script I've ever used or everytime I create one or download from the asset store
     
    Last edited: Jan 19, 2016
  17. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    5,676
    @Miguel Ferreira

    I've added a more in-depth answer on the other thread. TL;DR I'm working on fixing exactly this issue now!
     
  18. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    20,304
    We also found an interesting one: if you make a project without any assets containing MeshColliders, then in code you do a GameObject.CreatePrimitive( PrimitiveType.Plane);, it fails to add the MeshCollider class instance to the created plane.

    It also emits a message (iOS only) to that effect, when stripping is on.

    This seems improbable since there must be a snippet somewhere in Unity that says .AddComponent<MeshCollider>() to the newly-created primitive, right??

    Should I file a bug?
     
  19. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    5,676
    @Kurt Dekker

    Yes, this is worth filing a bug report. Thanks!
     
  20. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    20,304
    Well, after going back and forth with it last week in our huge project, proving it on and off with either code-referenced MeshColliders, MeshColliders in scene, or MeshColliders in an asset in the Resources folder, I am now today unable to reproduce the bug in a tiny project. How strange! I will go re-verify it in our large project, which takes a while to build, and I will keep this small project and try it again in case I'm missing some important step that triggers the problem.
     
  21. blahpers

    blahpers

    Joined:
    Jul 1, 2013
    Posts:
    3
    Greetings,

    This looks related to something I'm seeing in 5.3.3p2. The behavior I'm seeing is that if I have a project containing asset bundles containing objects with collider components and no such colliders anywhere in the project itself, the client throw an exception when attempting to instantiate those objects. I can get around this, but just for clarification: Should Unity be handling this situation already, or is it expected behavior that I would need to either use link.xml or a dummy prefab/scene to ensure that these components are included?

    Thanks in advance!
     
  22. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    5,676
    @blahpers

    This is the expected behavior in 5.3 and 5.4. Your best approach to work around it is to include dummy usages of the engine code type you need in a scene in the build. In 5.5 we will provide a new BuildPlayer API that will allow you to tell the build system about which asset bundles could be used at runtime, and Unity will handle it correctly.
     
unityunity