2

After archiving my app, my apk size is 71.82MB (estimated app store size). There are not lot of images also in my application too.

Also, i set my linker to Don't link which is the only option that works for me. It is too big for users. When i set linker to Link SDK assemblies only and also link all assemblies, i get a java exit 1 code error.

Are the some steps i could follow to reduce the size of my apk please?

**Where app crashes **

Environment Variables passed to tool:

    PROGUARD_HOME=/Library/Frameworks/Xamarin.Android.framework/Versions/Current/lib/mandroid/proguard
    /usr/bin/java -jar /Library/Frameworks/Xamarin.Android.framework/Versions/Current/lib/mandroid/proguard/lib/proguard.jar -include /Users/emmnock/AndroidStudioProjects/android-sdk-macosx//tools/proguard/proguard-android.txt -include obj/Release/proguard/proguard_xamarin.cfg -include obj/Release/proguard/proguard_project_references.cfg -include obj/Release/proguard/proguard_project_primary.cfg -include proguard.cfg -include obj/Release/XbdMerge/proguard/00d975c90.txt -include obj/Release/XbdMerge/proguard/0757626b0.txt -include obj/Release/XbdMerge/proguard/1db418320.txt -include obj/Release/XbdMerge/proguard/1f4fd9cc0.txt -include obj/Release/XbdMerge/proguard/211c61910.txt -include obj/Release/XbdMerge/proguard/2f4d086a0.txt -include obj/Release/XbdMerge/proguard/581878c90.txt -include obj/Release/XbdMerge/proguard/67b1d0a90.txt -include obj/Release/XbdMerge/proguard/8f1de9cb0.txt -include obj/Release/XbdMerge/proguard/a35ed7280.txt -include obj/Release/XbdMerge/proguard/acdd43380.txt -include obj/Release/XbdMerge/proguard/b76e0d310.txt -include obj/Release/XbdMerge/proguard/c756521f0.txt -include obj/Release/XbdMerge/proguard/d87effcf0.txt -include obj/Release/XbdMerge/proguard/d99a9fda0.txt -include obj/Release/XbdMerge/proguard/d9f2c52f0.txt -include obj/Release/XbdMerge/proguard/dc663e480.txt -include obj/Release/XbdMerge/proguard/e5bf18810.txt -include obj/Release/XbdMerge/proguard/e843ef2a0.txt -include obj/Release/XbdMerge/proguard/fb4df8d50.txt "-injars 'obj/Release/proguard/__proguard_input__.jar':'/Library/Frameworks/Mono.framework/External/xbuild-frameworks/MonoAndroid/v7.1/mono.android.jar':'/Library/Frameworks/Xamarin.Android.framework/Versions/Current/lib/mandroid/android-support-multidex.jar':'obj/Release/__library_projects__/Bolts.AppLinks/library_project_imports/bin/classes.jar':'obj/Release/__library_projects__/Bolts.Tasks/library_project_imports/bolts-tasks.jar':'obj/Release/__library_projects__/EasySplashScreen/library_project_imports/bin/classes.jar':'obj/Release/__library_projects__/ExoPlayer/library_project_imports/bin/classes.jar':'obj/Release/__library_projects__/FormsViewGroup/library_project_imports/formsviewgroup.jar':'obj/Release/__library_projects__/Glide/library_project_imports/glide-3.8.0.jar':'obj/Release/__library_projects__/Refractored.Controls.CircleImageView/library_project_imports/bin/classes.jar':'obj/Release/__library_projects__/Square.OkHttp/library_project_imports/okhttp.jar':'obj/Release/__library_projects__/Square.OkIO/library_project_imports/okio.jar':'obj/Release/__library_projects__/Square.Picasso/library_project_imports/picasso-2.5.2.jar':'obj/Release/__library_projects__/Xamarin.Android.Support.Animated.Vector.Drawable/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Android.Support.Compat/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Android.Support.Core.UI/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Android.Support.Core.Utils/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Android.Support.CustomTabs/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Android.Support.Design/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Android.Support.Fragment/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Android.Support.Media.Compat/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Android.Support.Transition/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Android.Support.Vector.Drawable/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Android.Support.v4/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Android.Support.v7.AppCompat/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Android.Support.v7.CardView/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Android.Support.v7.MediaRouter/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Android.Support.v7.Palette/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Android.Support.v7.RecyclerView/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Facebook/library_project_imports/bin/classes.jar':'obj/Release/__library_projects__/Xamarin.Facebook.AudienceNetwork/library_project_imports/bin/classes.jar':'obj/Release/__library_projects__/Xamarin.Firebase.Analytics/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Firebase.Analytics.Impl/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Firebase.Auth/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Firebase.Common/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Firebase.Database/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Firebase.Database.Connection/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Firebase.Iid/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Firebase.Storage/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.Firebase.Storage.Common/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.GooglePlayServices.Ads/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.GooglePlayServices.Ads.Lite/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.GooglePlayServices.Base/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.GooglePlayServices.Basement/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.GooglePlayServices.Clearcut/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.GooglePlayServices.Gass/library_project_imports/classes.jar':'obj/Release/__library_projects__/Xamarin.GooglePlayServices.Tasks/library_project_imports/classes.jar'" "-libraryjars '/Users/emmnock/AndroidStudioProjects/android-sdk-macosx/platforms/android-25/android.jar'" -outjars "obj/Release/proguard/__proguard_output__.jar" -optimizations !code/allocation/variable 
    proguard.ParseException: Unknown option '' in line 1 of file 'proguard.cfg',
      included from argument number 10
        at proguard.ConfigurationParser.parse(Unknown Source)
        at proguard.ProGuard.main(Unknown Source)
    /Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets(1922,3): error MSB6006: "java" exited with code 1.
  Done executing task "Proguard" -- FAILED.
Done building target "_CompileToDalvikWithDx" in project "FindMeApp.Droid.csproj" -- FAILED.

Done building project "FindMeApp.Droid.csproj" -- FAILED.
Community
  • 1
  • 1
  • Are you compiling with the shared runtime? – EJoshuaS - Stand with Ukraine Jul 17 '17 at 14:44
  • Using Shared Mono Runtime (under the general tab) is unchecked. I am not using it –  Jul 17 '17 at 14:45
  • Also, what do you mean by "archiving your app"? Are you trying to improve its size when deployed to clients or when storing it somewhere? – EJoshuaS - Stand with Ukraine Jul 17 '17 at 14:46
  • 1
    @EJoshuaS Shared runtime is only recommended for debug. Amateur, that's a very big problem that Xamarin is having, the problem is because as you don't link the SDK assemblies all the SDK is included. You need to link the SDK assemblies but if it gives you an error then the linker is removing too much code, if you can find what is removed and should not there are ways to preserve certain libraries. Also, I would try to ask on the Xamarin forums as there you will get more specialized help. – Gusman Jul 17 '17 at 14:46
  • 1
    @Gusman Yes, I'm aware, that's why I asked what he's trying to do with it. Also, I think that this should be a reasonable question for SO - I've often gotten better help here than I have on the Xamarin forums, which seems to have low participation. – EJoshuaS - Stand with Ukraine Jul 17 '17 at 14:47
  • 1
    @EJoshuaS, I am trying to publish on playStore and i have to archive it before i can do that. But now, the size is too big for clients to download if i publish an app of size 71+ –  Jul 17 '17 at 14:51
  • @Gusman, Participation in relation to the Xamarin Forum is poor to me. But all the same, i might be lucky to get some help –  Jul 17 '17 at 14:53
  • 1
    Amateur if you ask for help with libraries, forms and things like these, yes, it's poor, but usually for things as basic as linkage the Xamarin team tends to answer the questions. – Gusman Jul 17 '17 at 14:55
  • @Gusman is there a way to find the libraries creating the problems? I might want to try and ignore those libraries in my options –  Jul 17 '17 at 16:17
  • @Amateur look at the logcat, it usually gives a ton of info when xamarin crashes. – Gusman Jul 17 '17 at 16:33
  • I added the part where the app crashes.. Looking at it with lesser knowledge in diagnosing, could it be from the proguard? –  Jul 17 '17 at 16:41
  • Possible duplicate of [Xamarin proguard.ParseException: Unknown option '' in line 1 of file 'Properties/proguard.cfg'](https://stackoverflow.com/questions/38743557/xamarin-proguard-parseexception-unknown-option-in-line-1-of-file-properties) – Jon Douglas Jul 17 '17 at 18:18

2 Answers2

2

Let's talk about this. There's a few things that end up making your application very large.

1. Mono Linker

Depending on your Linker Behavior set, you can be risking quite a bit of space by not employing a more aggressive linking behavior. That is, if you set your Linker Behavior to None, you are saying "Please include everything inside my application even if it's not used". This can add quite a bit of excess classes/constructors/methods/etc that can bloat an assembly file. This is a managed linker meaning that it will only link your managed code (C#)

See my blog on this:

http://www.jon-douglas.com/2017/04/13/linker-bitdiffer/

2. CPU Architectures

You might be including too many architectures in your APK than you actually need. With each additional ABI, you are adding quite a bit of bloat to your application.

https://developer.xamarin.com/guides/android/application_fundamentals/cpu_architectures/#How_to_Specify_Supported_Architectures

Also see my answer on this here: Which Xamarin ABIs should we support

3. Resources / Raw Files

Large resources and raw files included in your application. Chances are when you're developing your app, you might bring some resources into it. However you might not actually be paying attention to how big some of those resources are. One trick to figure out the main cause of bloat in an APK is to use a tool like WinDirStat(https://windirstat.info/) or Disk Inventory X(http://www.derlien.com/) to investigate why your .apk is so large.

4. Proguard

Finally Proguard can be used as a means to shrink your APK by removing unused classes, fields, methods, and attributes. Think of Proguard as a Native Linker which will optimize your bytecode and remove unused code instructions.

See the documentation I wrote for this here:

http://www.jon-douglas.com/2016/11/22/xamarin-android-proguard/

and

https://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/proguard/#Overview

RE: Why Proguard is failing

You can see my post on this here:

Xamarin proguard.ParseException: Unknown option '' in line 1 of file 'Properties/proguard.cfg'

Jon Douglas
  • 13,006
  • 4
  • 38
  • 51
1

Well... that the downside of using Xamarin. It bundles mono runtime and other stuff intro your .apk.

But you have some options:

1) Fix your linking. Yes, that takes time and adds unwanted complexity. But it is what it is, you can't re-write all code back to native.

2) There is an option - "bundle assemblies into native code", from my practice it allows reducing .apk size in half. But it requires enterprise license.

3) Build application only for one instruction set, you can do that under "Android Build -> Advanced" tab. If you want your app to be able to run on all devices, left armeabi. From my experience, you can choose armeabi-v7a, it gives much better performance, and almost all devices nowadays supports it. enter image description here Also, be sure to remove tick from

enter image description here

Even if that does not give any results, while app is under 100mb, you can publish it as it is.

Taier
  • 2,109
  • 12
  • 22
  • i am facing the same issue although i have almost all your options done but my apk is still large (66.56MB).. Too big for a version 1 app for me :) –  Jul 17 '17 at 15:22
  • 1
    @Switz then most logical solution will be fixing linking. After all, its it main purpose, stripping unused code from included libraries. And since Xamarin uses .Net libraries, there are LOTS of code to strip :) You can read more about Xamarin linking, and how to exclude libraries from it on: https://developer.xamarin.com/guides/android/advanced_topics/linking/ – Taier Jul 17 '17 at 15:31
  • But then my linkage doesn't work like i stated in the question. Probably it is removing codes that are important in the build –  Jul 17 '17 at 15:44
  • @Amateur yes, that the common issue. When one library crashes after linking, but that library prevents 99% of others to be stripped. To fix that, you can force compiler not to link that particular library, but link all others. You can read how to do that in section linkskip: https://developer.xamarin.com/guides/android/advanced_topics/linking/#linkskip – Taier Jul 17 '17 at 15:52
  • Where do i add the linkskip? In which file in my project please? –  Jul 17 '17 at 15:54
  • @Amateur at first you need to find out which library is responsible for the crash. After that, you need to open your android.csproj by textedit and find your release target, it will be in property group line Add to that property group LibraryThatCrash and that's it :) – Taier Jul 17 '17 at 16:01
  • Looking in my Diagnostics, i don't find any library crashing but i will still search –  Jul 17 '17 at 16:03