Moons
Moons

Reputation: 623

Why building with IL2CPP backend results in bigger apk size in Unity

I'm using Unity 2018.2.3 with .Net 4.6 Scripting Runtime.
I have two games. When I build with IL2CPP backend, apk size is always bigger than Mono backend.

First Game:
APK size With Mono: 12.9 MB
APK size with IL2CPP: 13.7 MB

Second Game:
APK size With Mono: 39 MB
APK size with IL2CPP: 45 MB

Why is this happening?

Upvotes: 3

Views: 2773

Answers (1)

Josh Peterson
Josh Peterson

Reputation: 2329

This happens for a few reasons.

First, the Android build with Mono ships the managed assemblies in the APK. Those assemblies are just-in-time compiled to machine code on the Android device. IL2CPP, on the other hand, ahead-of-time compiles the managed assemblies into machine code, and ships the machine code in the APK. The IL byte code in those managed assemblies is more compact than machine code for two reasons:

  1. Only the managed code that is actually executed is converted to machine code by the JIT. IL2CPP must convert all of it.
  2. Generics need to all be expanded ahead of time. IL2CPP can share some generic implementations, but not all (specifically not generic implementations with value type arguments). The JIT only generated the generic implementations as they are used.

Second, by default Unity generates machine code for both the ARMv7 and x86 architectures with IL2CPP. Since IL2CPP needs to generate that code ahead of time, all of the issues with larger code mentioned in the first point are duplicated. If you don't need to support x86, for example, you can modify the player settings to avoid building it.

Upvotes: 7

Related Questions