Search Unity

Unity iOS exported Xcode project is too huge.

Discussion in 'iOS and tvOS' started by ysshetty96, Dec 28, 2020.

  1. ysshetty96

    ysshetty96

    Joined:
    Feb 27, 2019
    Posts:
    101
    Hi everyone,

    The unity version I'm using is 2019.2.14f1.

    I'm working on a Unity project where we are running unity on top of react-native. in our app whenever we want to play the game then we are launching a unity instance and then we will be playing the game in a unity environment. so basically we are using unity as a library.

    In our unity project, we are not keeping any assets (textures, audio clips, or anything), just we will be keeping scripts and some required small shaders(10-15 shaders are there, and that most of them are unity default shaders), all assets and game-related prefabs and everything is loaded from asset bundle which we will be downloading it at run time.

    In our project, we have a lot of game-related c# scripts and some libraries too. but no assets.

    Now the issue is when we export our unity project to Xcode, unity is generating a Xcode project of ~1.9GB in release mode and ~2.24GB in development mode.
    while making IPA we are just concerned with "Classes", "Libraries" and "Data" folders which are created by unity on exporting to Xcode project.

    In release mode, if I export my unity project to Xcode then these are the size of the 3 folders.
    "Classes" folder: ~950MB
    "Libraries" folder: ~810MB
    "Data" folder: ~50MB

    so these 3 folders' size will be around ~1.9GB.

    But my question is why is this too large? I don't have any assets what I have only is scripts but still why it is 1.9GB?
    I have got a task to reduce its size mainly "Classes" and "Libraries" folders size drastically but I'm not finding any way to do this, I'm not understanding what I have to remove or why these much big folders are created by unity just for scripts.
    Our project is continuously growing so the size of these 3 folders will exceed 2GB within 6months.

    And another issue is if I export to Xcode project in debug mode(for debugging unity related issue at run time)
    then unity is increasing these 3 folders including size to 2.24GB because of that while taking the build I'm getting linker error in Xcode (Image attached below) because of the huge bin size.
    upload_2020-12-29_0-50-2.png
    Arm 64 branch is getting out of range.
    Currently, this is happening because of 2.24GB of bin size in debug mode but sooner our bin size will grow above 2.2GB in release mode itself so at that time what I can do to resolve this issue?

    I have tried to get the solutions from the different threads but couldn't get much help so created a new thread, so please someone from unity or anyone who knows in and out of this thoroughly please suggest me a solution to decrease this bin(3 folders) size drastically. I'm sure that because of some issue it is creating these huge folders.
    And also because of this, our IPA is growing bigger than 250MB, so even we have this issue.
    basically, we wanted to decrease the IPA size as well. 90% of the IPA size is covered by unity itself.

    Just for reference, this is my project settings screenshot
    upload_2020-12-29_1-2-9.png

    Note: I have tried enabling "Stripe engine code" and keeping "Managed stripping level" to High and "Script call optimization" to fast but no exception, doing this will reduce bin size by a certain MB's(Not very much), but this was giving other strange issues while running the game so I left that idea.

    Please, someone, reply as soon as possible with a detailed explanation (what is stored in these folders? and why its size is too high even without any assets? and how it is affecting build size and finally how can I reduce these folders size drastically and the IPA size as well).

    Thank you.
     
    Last edited: Dec 29, 2020
  2. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    thats some insane numbers: Classes are for trampoline objc code and "Native" subfolder that has il2cpp code generated from your c# scripts (but this will be compiled in the end, so not representative, but still a lot), and Libraries contain some il2cpp support code and, well, libraries (it will also contain unity library itself which might be pretty big before xcode stripping). So, i guess, you have too many scripts/plugins? Maybe check the contents (mostly for libs)
     
  3. ysshetty96

    ysshetty96

    Joined:
    Feb 27, 2019
    Posts:
    101
    Thank you so much for replying, but I really need your technical assistance to resolve this issue.

    In my project (Including everything plugins etc etc), there are 5484 C# scripts whose total size ~47MB
    upload_2020-12-29_13-27-35.png

    Yes we do have a few plugins but not too heavy ones I guess
    upload_2020-12-29_13-31-46.png
    Total Plugins folder size itself of ~60MB (and also these plugins C# scripts are also included in my first screenshot)

    If it is insane then how can I reduce these folder's size? first of all, I'm not getting any calculation of what is converting into this much huge Xcode project! , our total unity project size itself 755MB (Assets, Library, ProjectSettings folders).

    We are using unity paid licenses so can we get any enterprise solution from unity.
    Or else I'm very glad to get a solution from your end as well.
     
  4. ysshetty96

    ysshetty96

    Joined:
    Feb 27, 2019
    Posts:
    101
    Waiting for your reply:)
     
  5. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    c# code will get converted to cpp and stored in Classes - there is no direct correlation between c# code size and resulted c++ code size (well, apart from - more csharp -> more cpp) Same for plugins folder - you should check contents of Classes/Libraries in generated xcode project, not of the source unity project

    you can try running
    ls -lhS -R
    in both Classes and Libraries, this will give you the files sorted by size with size information
     
  6. ysshetty96

    ysshetty96

    Joined:
    Feb 27, 2019
    Posts:
    101
    Yes, I have checked that. this is the result what I got
    upload_2020-12-30_14-46-12.png
    Inside Classes, the Native folder itself is the issue. whole classes folder complete size is covered by the Native folder itself.

    upload_2020-12-30_15-5-39.png
    And in Libraries "libiPhone-lib.a" and "libil2cpp.a" are consuming 555MB and 88MB respectively. libiPhone-lib.a is the Unity runtime library. It's needed to make an iOS game built with Unity work but why is its size is too huge? is there any way to reduce its size?

    @Alexey So now I have these stats of Classes and Libraries folders and also now I know the size of each object, so how can I use this data to reduce the "Classes" and "Libraries" folder size? what to remove and what not to?
    Is there any way to reduce this or there is no way except removing those objects and their dependencies from the project? (This will be a problem as if we remove any libraries it will affect app functionality:mad:)
     
    Last edited: Dec 30, 2020
  7. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    516
    These are static libraries. Any part of them not referenced in your code will be stripped. They are copied from the Engine for build purpose, but you can skip the copy process by enabling "Symlink Unity libraries" in Build Settings window.

    Personally, I don't think having nearly 1GB of IL2CPP source is unreasonable if you project is big. But if your goal is a smaller binary, you do have to remove unused C# libraries and code. Also, the source size does not necessarily reflect the binary size. I have 600MB of IL2CPP code and the final binary is only ~80MB per arch.

    I also recommend you taking a look at how much assets your Unity project yields. All included assets can be found in Data folder of your Xcode project and are copied into your app bundle.

    Also, how are you measuring the IPA size? Is the size reported by App Store Connect after you uploaded to it? If you haven't, I also recommend reading the "Create the App Size Report" section of the linked documentation and measure the app size that way.
    https://developer.apple.com/documentation/xcode/reducing_your_app_s_size
     
  8. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    516
    Also, it's always more efficient to contact the dedicated support channel if you have an Enterprise license or Pro license than posting here.
     
  9. ysshetty96

    ysshetty96

    Joined:
    Feb 27, 2019
    Posts:
    101
    Our build taking process is kind of different, as we are running Unity on top of React native we are not directly exporting our unity project to Xcode and running it there. we just export our unity project to Xcode project and in that, we just copy Libraries, Classes, and data folders into react-native Xcode project so if I enable symlink it won't help I guess, and also for the production IPA we(unity team) send our bin (Classes, Libraries Data folder) to React team so they will use these 3 folders to make the final IPA in their machine, the symlink will only work if we are exporting and running in the same machine right?

    Because of the huge unity bin size, we are not able to create IPA, Xcode shows us an error related to Arm 64 branch out of range (image attached in my first message). this issue can happen because of other reasons also but in our case, it is because of huge bin size if the Classes, Data, Libraries folder including size increases more then 2GB then we do get this error while building IPA in Xcode.

    As I mentioned it in my earlier post we don't have any assets in our project everything that we have scripts, and all the game-related asset will be downloaded at run time as an asset bundle.

    Yes, I'm mentioning the APP size which is shown by the app store connect, currently our build size is 224MB. but for a project where there are no game-related assets except scripts then how it can consume 224MB?

    How to find the unused libraries? in unity we are not using any Unused plugins or libraries, and also if it is unused then unity itself stripe those codes while exporting them to the Xcode project right?
     
  10. ysshetty96

    ysshetty96

    Joined:
    Feb 27, 2019
    Posts:
    101
    Yes, we have a license, But do you know where we can connect with them for technical assistance? (Any link?).
     
  11. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    516
    It's definitely not a common use case. I don't think I have specific knowledge regarding this specific area. Perhaps you should try building the Unity app by itself and see if it can build properly and then examine the binary size.

    I don't know if you have considered it, but it does sound like that your use case could be addressed by "Unity as a Library" feature added in Unity 2019.3. Since 2019.3, Unity will build the binary as an iOS framework and embed it into a shell app. So it saves you from moving the source code, and you don't have to worry about if your coworkers could build from source.
     
    Last edited: Dec 31, 2020
  12. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    516
    I don't have those licenses, so I don't know how. You should confirm your license type (Plus, Pro, Enterprise) and ask whoever purchased the license who the point of contact from Unity is.
     
  13. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    516
    That is true that any C# code without any direct references will be stripped. I'm just giving a general advice. If your Unity project contains a lot of features, then I think having a lot of generated sources are unavoidable and you do have to start strip down your code if you do decide to control the size.

    By the way, I just saw in your earlier post that your Data folder is 50MB, and that folder was the "assets" I was referring to.
     
  14. ysshetty96

    ysshetty96

    Joined:
    Feb 27, 2019
    Posts:
    101
    Yes, I'm planning for it, if I just take unity build then I will have an idea of how much unity contributing to the IPA, thanks for the advice.
     
  15. ysshetty96

    ysshetty96

    Joined:
    Feb 27, 2019
    Posts:
    101
    ahh yes. before reading your message I was looking at that folder itself:), yes in unity we do have a single scene where a very small amount of assets are used so from unity we are contributing around 23MB of assets (textures, shaders, animation, etc) which I got to know from unity build report.
    Apart from that unity itself is keeping a "global-metadata.dat" object inside the Data folder which is of around 18MB in our case but I'm sure what it does!.
     
  16. ysshetty96

    ysshetty96

    Joined:
    Feb 27, 2019
    Posts:
    101
    Yes, kind of a similar implementation, in our app apart from games we do have other features as well, so all other features are done in react native and for running games, we are launching unity on top of react-native. officially unity launched this in 2019.3, but even in 2019.2 there was a provision.
     
  17. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    well, Neonlyte is absolutely correct on his assessment: as i have said already the size itself of generated c#->cpp code is not indicative of the final app size, but in your case i would guess the sheer amount of things breaks it. I would suggest trying to tweak compiler options to have -Os optimization (smallest) and/or strip symbols, or run on release, as it is not unreasonable for debug config to generate more code and add symbols resulting in way bigger app.
     
  18. ysshetty96

    ysshetty96

    Joined:
    Feb 27, 2019
    Posts:
    101
    Yes, I got your point and even I'm not worried much about IPA size, as from the unity end we are not contributing much everything is getting stripped down to around 60MB.
    But my main concern is the UNITY bin size, it is very huge and because of that, I'm getting errors in Xcode while building an app that the Arm64 branch is out of range (Image attached in my first message).
    I'm taking the release build itself but still, it is generating 1.9 GB of unity bin.
    If our bin size becomes greater than 2GB then we get that Arm 64 branch out of range error(In a development build obviously we will reach bin size greater than 2GB).

    What to do for this Xcode error because of Unity bin size, if we get that error we won't be able to create the build itself!.
    And also sometimes we may have to take a development build to profile the game for various issues such as memory etc.

    Technically this "Arm64 Branch Out Of Range" issue should not come even if we have a 5GB unity bin right? because however, in the end, Xcode will stripe everything whichever is not required, so then why this error, I have searched in different forums many people got this issue but no one has a proper solution.

    So please do suggest to me what I can do for this "Arm64 Branch Out Of Range" error?

    And one more doubt what I have is when we take an iOS build, in our laptop build size it shows as 116.7MB but when we upload it to AppStore connect then there it shows 224.5MB, how is this possible? any reasons for this?
     
    Last edited: Jan 4, 2021
  19. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    516
    I may be wrong, but when you build in Xcode for running did you select your device as a target? If so, it will only build the single arch slice that the connected device needs. When uploading to App Store, both armv7 and arm64 slices are built. In App Store Connect, the universal binary size contains both armv7 and arm64. A while ago, though, App Store Connect would also report download sizes for each supported device, when I looked at that page a few days ago those info has disappeared.
     
  20. ysshetty96

    ysshetty96

    Joined:
    Feb 27, 2019
    Posts:
    101
    No no, I'm not doing the build and run in Xcode, I'm taking the archive itself. as you said if I select the target then it will only build for that target architecture. but in our case it is universal.

    upload_2021-1-5_12-20-12.png

    "Build Active Architecture Only" is set to true in debug build but it is set to false in Release, so is that affecting anything for the build size.
    in my above message, the build 116.7MB that I mentioned was taken in Debug mode.
     
    Last edited: Jan 5, 2021
  21. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    516
    For debug mode, "Build Active Architecture Only" is set true, so the build destination target is affecting what arch slice is built. The only "universal" destination target in Xcode is "Any iOS Device / Generic iOS Device". Did you select that when building your Debug mode binary?

    upload_2021-1-5_1-10-12.png
     
  22. ysshetty96

    ysshetty96

    Joined:
    Feb 27, 2019
    Posts:
    101
    Yes, I'm selecting the generic iOS device. (Im using Xcode v11.6).
    upload_2021-1-5_12-44-48.png
     
  23. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    516
    BTW for you main issue, I don't think there is anything else I can do to help you as your use case is unique and you have not moved to using UaaL.

    I took a look at the source file of ld that spitted the error. https://opensource.apple.com/source/ld64/ld64-253.6/src/ld/OutputFile.cpp.auto.html

    The problem seems to be that the compiled main.o binary contains too much code, that in function _main, the branch instruction has to jump to an address (in this case the entry point of function __Unwind_resume) more that 128MB away -- there are too many things in between. Thus the number of source file does correlate to the appearance of the issue. This 128MB limit seems to be arbitrary but I did not find a good source to show why it is set that way.

    The 128MB limit is documented in the ARM specification, under "Unconditional branch instructions": https://developer.arm.com/architect...ch64-instruction-set-architecture/single-page
    So the solution to this problem is to reduce the amount of code you are compiling into a single binary, either by removing code or bundling code into different binaries (like a shared library, which is exactly what UaaL does).

    What this solution translates to Unity is that you have to start removing and stripping every bit of C# source files and libraries that you can, so that your IL2CPP code amount reduces. You did say that all of your libraries are useful, but there does not seem to be another way, so you have to make the judgement.

    If you have not done so, you should also review the build settings of the Unity Xcode project and try to match every one of them in your destination Xcode project. Some of those settings may be essential to reducing the binary size.

    But like I mentioned before, UaaL is available should you upgrade Unity to 2019.3 or later, which builds your Unity code as a shared library separately from the app you are integrating into. If you can build your Unity app standalone successfully, you can build it using UaaL.
     
    Last edited: Jan 5, 2021
  24. ysshetty96

    ysshetty96

    Joined:
    Feb 27, 2019
    Posts:
    101
    Thank you so much for the info, yeah we may have to look into that UaaL.
    Apart from UaaL implementation for the shared library is there any other possible way to "bundle code into different binaries"?
    Or if any way to increase the 128MB restriction to something like 500MB?
    Because suddenly porting to Uaal implementation is not possible in our case!, but still I will take this suggestion for sure and I will add this in our future roadmap.
     
  25. ysshetty96

    ysshetty96

    Joined:
    Feb 27, 2019
    Posts:
    101
    Just for checking, I have created a new empty project in Unity 2019.2.14f1
    upload_2021-1-5_23-45-12.png

    Nothing is there in this project I have deleted all inbuilt packages and everything and removed the packages entry from the dependency manifest as well. so now my empty project size is just 23MB.

    But when I exported it to the Xcode project it created a bin(Classes + Data + Libraries folders) of 760MB, isn't it insane?
    I do understand that while making IPA Xcode will stripe down the unnecessary code but in an empty project what unnecessary things will be there? 23MB unity project is exporting a 760MB Xcode project how is this possible?

    And also as you can see in my attached image I have enabled all the code stripping settings in my player setting, everything is set to peak value!

    ARM64 branch out of range issue in Xcode has become a huge problem for us, this is happening because of the huge bin size. out of our Main project 1.9GB bin around 800MB consumed by this unnecessary thing itself which is even getting generated for an empty project!

    @Alexey @Neonlyte Is this intended? I don't think it is intended for an empty project!.

    I'm not understanding, what setting I have to play with so that I can reduce this bin size for an empty project!
    More than the app size our main concern is for bin size, now we aren't able to take build with unity development bin which is already crossing the 2GB mark, and it is making it very hard to debug memory and everything for iOS.
     
    Last edited: Jan 6, 2021
  26. PavelLU

    PavelLU

    Unity Technologies

    Joined:
    Feb 23, 2017
    Posts:
    107
    can you please try disable "Enable Bitcode" option (for main app target and its dependencies) and check where it goes
     
  27. ysshetty96

    ysshetty96

    Joined:
    Feb 27, 2019
    Posts:
    101
    Hey, Thank you for your reply.
    Our build size is not an issue, it is under control, in my previous message I mentioned that our app size 224.5MB, but this is not the app size, it is the space that is required to install our app on mobile(App store shows us the installation size (Which is after uncompressing the IPA) not the downloadable app size(Which is compressed) I recently got to know about this). So IPA size is not a problem now.

    And also "Enable Bitcode" is disabled in our Xcode build settings.

    Only the issue is huge bin size what our unity is exporting. because of its huge size, we are getting the "Arm64 Branch Out Of Range" error which I have mentioned in my earlier messages.
    I understand that regardless of the huge bin size the final IPA will be very small, but still, if the bin size increases more then 2GB then we won't be able to create the IPA itself because of the error, Now itself the bin which is taken in development mode in unity doesn't work because its size exceeds 2.2GB, so currently we are exporting unity project in release mode. but with the release mode bin, we won't be able to do memory profiling and all.