Xamarin - apk App Size

前端 未结 3 1144
小蘑菇
小蘑菇 2020-12-08 11:32

So, after all the hard-work developing the app using Xamarin.Forms, when I tried to create a release build today, I was shocked to see the app size is ~25MB - ~31MB (after S

相关标签:
3条回答
  • 2020-12-08 11:49

    Xamarin Android APK files have a larger size than usual because they include the Xamarin libraries, that translate C# code to Java code, to run in Android OS.

    Using the following options, you can reduce the APK size:

    • Configuration: Active (Release).
    • Platform: Active (Any CPU). These are necessary to build your APK for the Google Play Store.
    • Use Shared Runtime: false. If you set it to true, the APK will use Mono Runtime to execute. The Mono Runtime is installed automatically when debugging through USB, but not in the Release APK. If Mono Runtime is not installed in the device and this option is set to true in the Release APK, the app will crash.
    • Generate one package (.apk) per selected ABI: false. Create your APK for as many platforms as possible, for compatibility reasons.
    • Enable Multi-Dex: true, but you can set it to false if your app is not very complex (that is, has less than 65536 variables or methods, see here).
    • Enable developer instrumentation (debugging and profiling): false for Release APK.
    • Linking: SDK and User Assemblies. This will make the Xamarin compiler to remove all unused classes from SDK and your code, reducing the APK size.
    • Supported architectures: Select all, for compatibility reasons.

    Here is a print screen example:

    0 讨论(0)
  • 2020-12-08 11:54

    Here's the stats of my Xamarin.Forms app size from Google Play Console. You can see how much it went down as soon as I took the subject seriously and moved away from default Xamarin settings:

    In addition to the measures that are typically found when googling the subject (linking, keeping eye on own resources, APK per ABI etc.) here're a few of my own findings and quirks:

    • Enabling Managed TLS Provider shoves of ~1MB - by default you have 'Native TLS 1.2' option selected in Project Options -> Android Options -> Advanced button -> SSL/TLS implementation drop down. Managed TLS is version 1.1 only, might be an issue from someone
    • Upgrade to Xamarin.Forms 4.1 gives ~1MB reduction (comparing to 3.x and 4.0) - they removed some of unused yet referenced Android Compatability Libraries
    • Enabling aapt2 packager gives ~1MB reduction for App Compat libs resources only (those referenced and included by Xamarin.Forms). Might give more if you have many of your own resources. You can have aapt2 further striping down resources by using the following extra args in you Android's .csproj file:

      <PropertyGroup>
         <AndroidUseAapt2>true</AndroidUseAapt2>
         <AndroidAapt2LinkExtraArgs>--no-version-vectors -c en-rUS</AndroidAapt2LinkExtraArgs>
      </PropertyGroup>
      
    • Turning on D8 and R8 gives another ~1MB for a small Xamarin.Forms app (Project Options -> Android Options or .csproj)

      <PropertyGroup>
          <AndroidDexTool>d8</AndroidDexTool>
          <AndroidLinkTool>r8</AndroidLinkTool>
      </PropertyGroup>
      
    • SkiaSharp 1.60.3 gives ~2MB economy per architecture, compared to more recent versions (e.g. 1.68.0). It's worthwhile looking out for dependencies and their versions bloating your APK.

    • Disabling AoT (if you had it previously enabled, it's off by default) can easily cut your app size in half. Turning on Ahead-of-Time complication could have been a deliberate step to improve your app's start-up performance. There's a clear trade-off, either you go with AoT and have huge app size OR you don't have it and keep size minimal with slow app launches. One of the points to consider when deciding on the trade-off... Most ASO articles (as well as Google Play Console) mention how important it is to have app size small to increase installs conversion. Clearly when browsing Google Play it's app download size that is displayed, not app start time.

    • Adjusting AoT to minimize it's footprint, add the following in .csproj for your Release configuration:

      <PropertyGroup>
          <AotAssemblies>true</AotAssemblies>
          <AndroidAotAdditionalArguments>no-write-symbols,nodebug</AndroidAotAdditionalArguments>
      </PropertyGroup>
      
    • Doing Partial AoT. By default AoT goes through all assemblies resolved by the project and adds 30+ .so files in the /lib directory of your APK. There're numerous app compat libs, BCL and system libs. One might want to have control over which assemblies to put through AoT and which ones to ignore in order find a sweet spot between performance and app size. Well, there's no such thing available as standard feature (at least I didn't find one). You can play with Xamarin build process by making changes to Xamarin.Android.Common.targets - you can find some insights into this process here. I found my golden mean by AoT'ing only three libs (1 my own XF shared UI lib and 2 XF libs):

      <_ResolvedUserAssemblies2 Include="Tmp/android/assets/Xamarin.Forms.Core.dll;Tmp/android/assets/Saplin.CPDT.UICore.dll;Tmp/android/assets/Xamarin.Forms.Platform.Android.dll;" />

    P.S.: Enabling Linking (SDK and User Assemblies)- 90% chance that you'll find your app broken, if you extensively use XAML and bindings. Add your own libs to linker exceptions - XF class library that hosts your UI, any class libs you created and that might use reflection.

    P.P.S: with my minification efforts, partial AoT and paying with async/deffered Xamarin.Forms UI loading (here and here) I reached a significant app size reduction (see pic. above) with significant app launch time improvements (measured on Nokia N1 through ADB):

    • No AOT, sync UI (ms): 3613, 3586, 3576
    • Partial AOT, async UI (ms): 2202, 2255, 2258
    0 讨论(0)
  • 2020-12-08 12:01

    I understand your problem when you create a mobile app, you always wanted to create small size apk file which can easily be downloaded and used by users. For this try to reduce the code duplication in your project and reduce all the image size. Use following steps, it will help you to reduce your apk size. Right Click on droid properties. Open Android Options -> Click on packaging then check generate one package (.apk) per selected ABI. Check Multi-Dex and proguard options as well. Now move to Linker option, select SDK assemblies Only under Linking dropdown. Now move to Advance tab- Check all the option under Supported architecture. Save all the setting and keep you project in release mode clean and build your project then you can create reduced size of apk file. Use following link for more info.

    http://motzcod.es/post/112072508362/how-to-keep-your-android-app-size-down

    0 讨论(0)
提交回复
热议问题