Xamarin-android: After Upgrade from Visual Studio 2019 16.6 to 16.7 APK Size Increases significantly from 24 MB to 64 MB

Created on 6 Aug 2020  路  19Comments  路  Source: xamarin/xamarin-android

Steps to Reproduce

  1. Open Attached Xamarin.Android.zip
  2. Change To Release Mode
  3. Execute Deploy (The generated Xamarin.Android.apk is 64 MB)

Xamarin.Android.zip

Expected Behavior

Size is 24MB

Actual Behavior

Size is 64MB

Version Information

Visual Studio 2019 16.7

Work around

Changing following line in the AndroidManifest.xml
from
<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="29" />
to
<uses-sdk android:minSdkVersion="22" android:targetSdkVersion="29" />
reduces the apk size to 24MB

The reason why the apk with minsdkversion 23 is so much bigger is that that the libs aren't anymore compressed
image

Here the apk with MinSdkVersion="22"

image

With Visual Studio Version 2019 16.6 The size was 24MB even with minSDKVersion:="23"

Currently only x86 is selected as compiled target the app doesn't work is only used as example how the apk size is increased

App+Library Build question

All 19 comments

@inforithmics the difference in the bigger .apk file is this value in the AndroidManifest.xml:

<application android:extractNativeLibs="false"

See: https://developer.android.com/guide/topics/manifest/application-element

I think this value is emitted by default for minSdkVersion="23" and higher. It is something the Android toolchain is doing automatically. Google claims this really cuts down on _install_ size, as the native libraries are loading directly from the .apk file. Otherwise on install, they get extracted to a directory and take up more space.

I could change the manifest to this to get the smaller .apk file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.app52">
    <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="29" />
    <application android:label="App52.Android" android:theme="@style/MainTheme" android:extractNativeLibs="true"></application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
</manifest>

This problem really shows up when you are using AOT, because there are large native libraries. It's up to you as the developer which setting is better for your app.

I'm not sure what Xamarin.Android needs to do here, if anything. If android:extractNativeLibs="false" is _always_ bad when AotAssemblies=true, maybe we could default this value to true when AOT is used?

  • The problem is that the native libraries are really big and the resulting apk would be bigger than 100 MB, so I wouldn't be able to upload it to the Store.
  • By Upgrading for Visual Studio 2019 16.7 I although upgraded the Android Sdks and Tools that's maybe the reason it got changed.

I think in your case, android:extractNativeLibs="true" would need to be specified to stay under 100MB.

Updating to VS 16.7 or updating Android SDKs might have cause the behavior to change for your app. We updated the version of aapt2 that is bundled with Xamarin.Android in 16.7:

https://docs.microsoft.com/xamarin/android/release-notes/11/11.0#aapt2-version-update-to-400

Thanks for the Tipp
setting android:extractNativeLibs="true" worked and the apk is now under 100MB
intrestingly setting the bundle format to aab, the aab file stays under 100MB too (Because in aab the libs are compressed)

I'm going to leave this open for a bit to see if a lot of others hit this issue.

We might need to change _something_, I'm not sure what it would be yet.

We isolated the change that caused this to start happening in 16.7: https://github.com/xamarin/xamarin-android/commit/86737ca159d851f2743c01ffdf66428df07082b5

Specifically updating the manifest-merger from 26.5 to 27.0 caused android:extractNativeLibs="false" to be emitted when minSdkVersion is 23 or higher.

If you are using AndroidX, it enables the new manifest-merger feature:

https://docs.microsoft.com/xamarin/android/release-notes/10/10.2#improved-android-manifest-merging

Thanks, Good to know.
Yes I'm using AndroidX in the app

@jonathanpeppers
I am using AndroidX with a min version higher than 23. Will this increase the size of the apk?
do we need to set android:extractNativeLibs="true

I would like to upgrade to 16.7 visual studio but not sure if this will cause more problems like this and also will the azure pipeline still work as before.

I am deploying to a client in a couple of weeks and I m worried that than I cannot revert to previous version of visual studio.

Any input in the matter will be greatly appreciated

Will this increase the size of the apk? do we need to set android:extractNativeLibs="true

@developer9969 you can read about the setting here:

You might just look at your .apk file and see if this even matters for your app. Google prefers extractNativeLibs="false", because it saves on disk space after your app is installed. But this will cause a larger download size.

_Cross-referencing note for the Xamarin team_

Developer Community item that looks like it might be for the same behavior: https://developercommunity.visualstudio.com/content/problem/1144021/vs-1670-compile-and-archive-issues.html

@brendanzagaeski @dellis1972 there is also a bin/obj bug here...

If you deploy an app with android:extractNativeLibs="true" (most of the time the default when omitted).

Then change it to android:extractNativeLibs="false" and deploy again you get a deploy error:

ADB0010: Mono.AndroidTools.InstallFailedException: Failure [INSTALL_FAILED_INVALID_APK: Failed to extract native libraries, res=-2]

I can fix the problem by doing a Clean. This was probably introduced in https://github.com/xamarin/xamarin-android/commit/1c4727cb89b0d15e3dca43991e306aa7aa9fe9ae -- it doesn't look like compression is taken into account for updating files during the <BuildApk/> MSBuild task.

I will look into this.

@jonathanpeppers thank you for replying I will read into the links and noticed that other people have experienced the same behavior (App growth). I will monitor this issue and hopefully this will be fixed in the next version of Visual studio.

there is also a bin/obj bug here...

@jonathanpeppers, for the eventual fix for that issue, if you like, it looks like https://github.com/xamarin/xamarin-android/issues/4990 is about that part of the behavior and so can be included as a Fixes: line in the commit message for that, for thorough bookkeeping.

Merged a fix for the bin/obj issue: https://github.com/xamarin/xamarin-android/pull/5001

We should discuss about emitting android:extractNativeLibs="true" by default if not specified by Xamarin.Android apps. It seems like Google is changing this value on us, and it is not desired.

@brendanzagaeski hi there is this fix in vs 16.7.2? many thanks

@developer9969, thanks for the question. The fix for the issue is not included in Visual Studio 2019 version 16.7.2.

If everything goes as planned, this fix will be included in future versions of Visual Studio 2019 version 16.8 Preview and Visual Studio 2019 for Mac version 8.8 Preview. I will be adding an "In Preview: " milestone to this issue along with a comment as soon as the fix is published in a Preview version.

Here's an example of how the comment will look: https://github.com/xamarin/xamarin-android/issues/4409#issuecomment-615018927.

Note also that the fix for this issue doesn't do anything other than set android:extractNativeLibs="true" by default when no value is provided by the application in the _PropertiesAndroidManifest.xml_ file. Even when that fix is avaiable, project authors might wish to continue to set android:extractNativeLibs explicitly in _AndroidManifest.xml_ anyway to make it easy to remember which setting their apps are using. That attribute can be set to either "true" to keep the same behavior from Visual Studio 2019 version 16.6 or "false" to accept the tradeoff of a larger APK and download size in exchange for a smaller total install size for the application on device. See also the draft release note for the fix: https://github.com/xamarin/xamarin-android/pull/5021#issuecomment-676527775.

@brendanzagaeski thank you for the very detailed answer, appreciate it . It helps to explain to management

_Release status update_

A new Preview version of Xamarin.Android has now been published that includes the fix for this item. The fix is not yet included in a Release version. I will update this item again when a Release version is available that includes the fix.

Fix included in Xamarin.Android SDK version 11.1.0.3.

Fix included on Windows in Visual Studio 2019 version 16.8 Preview 3. To try the Preview version that includes the fix, check for the latest updates in Visual Studio Preview.

Fix included on macOS in Visual Studio 2019 for Mac version 8.8 Preview 3. To try the Preview version that includes the fix, check for the latest updates on the Preview updater channel.

_Release status update_

A new Release version of Xamarin.Android has now been published that includes the fix for this item.

Fix included in Xamarin.Android SDK version 11.1.0.17.

Fix included on Windows in Visual Studio 2019 version 16.8. To get the new version that includes the fix, check for the latest updates or install the most recent release from https://visualstudio.microsoft.com/downloads/.

Fix included on macOS in Visual Studio 2019 for Mac version 8.8. To get the new version that includes the fix, check for the latest updates on the Stable updater channel.

Was this page helpful?
0 / 5 - 0 ratings