Shahram Shobeiri
Shahram Shobeiri

Reputation: 90

Xamarin.Forms bundle assemblies into native code

I created an Android App with Xamarin.Forms. I used "SKD and User Assemblies" for linking and with some dummy usage of classes (to fool linker) and using the

[Android.Runtime.Preserve(AllMembers = true)] 

annotation I could run my app without problem and reduce apk size from about 50 MB to 19 MB. But 19 MB is still too much! So I used "Bundle assemblies into native code", build and archive is OK and my apk size is about 6 MB now. But when I open the app, It closed immediately without any error!

Upvotes: 2

Views: 1260

Answers (1)

SushiHangover
SushiHangover

Reputation: 74134

Check the contents of your .apk, if you are missing the libmonodroid_bundle_app.so, if so there are two basic possibilities.

  • Your Visual Studio (Mac or Windows) installation is not a valid Enterprise license or is a Community/Profession license and the mkbundle step is not being executed

    🍣 IMHO: This should always be an MSBuild error/failure if 
       BundleAssemblies is true in your `.csproj` but you do not 
       have the correct license but it is currently not.
    
  • MSBuild is failing on the Android NDK building of the shared library that contains your CIL assemblies.

This issue is on bugzilla and was fixed in the 7.2.0 release, but I've seen it come back up in some 7.5 / 8.x releases.

The issue with this can be a silent failure and thus not seen by MSBuild and thus an APK is produced with no libmonodroid_bundle_app.so file.

  • Review issue 48678

  • Test outside of the Visual Studio IDE using MSBuild from the cmd-line. Pass the /t:PackageForAndroid to a release configuration and see if you are getting a clean _BuildApkEmbed task:

Clean/working:

    _BuildApkEmbed:
      [mkbundle stderr]
     [cc stderr]
     [LD] X:\Docker\android-sdk-windows\ndk-bundle\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\bin\arm
      -linux-androideabi-ld.exe --shared obj\Release\bundles\armeabi-v7a\temp.o obj\Release\bundles\armeabi-v7a\assemblies.
      o -o obj\Release\bundles\armeabi-v7a\libmonodroid_bundle_app.so -L X:\Docker\android-sdk-windows\ndk-bundle\platfo
    rms\android-14\arch-arm\usr\lib -lc -lm -ldl -llog -lz
      [ld stderr]

Broken/error:

Platform header files for target Arm and API Level XX was not found. Expected path .....

If that is the case, (re)install your Android NDK:

The last failure, not related to your problem, but possibly others.

A complete crash and restart of Visual Studio 2017 during the archive process.

If you are missing files in your Android NDK installation, have an corrupt installation or the wrong version. Also some versions from Google do not have any include directories (?!?!) but do have the arch lib directories and without the header files, the Xamarin.Android MSBuild process hangs VS and causes a crash/restart.

Upvotes: 2

Related Questions