'How do I decrease the size of the Ionic Android APK file?

I created the app using Ionic and the Android APK file size is too big.

Before the API integration The File Size was 4.8MB. When I integrated the API I modified just 10 Pages now the APK file size is 71 MB.

Here are the Cordova Plugins I have used:

  • cordova-plugin-console
  • cordova-plugin-datepicker
  • cordova-plugin-device
  • cordova-plugin-network-information
  • cordova-plugin-splashscreen
  • cordova-plugin-statusbar
  • cordova-plugin-whitelist
  • cordova-plugin-x-socialsharing
  • cordova-sqlite-storage
  • es6-promise-plugin
  • ionic-plugin-keyboard

I unzipped the APK file to find out what going on. I looked at the build Folder in assets/www/ folder which alone was 357MB and it contains JavaScript and MAP files and a 4.5MB CSS file.

How do I decrease the size of the Ionic Android APK file?



Solution 1:[1]

You should use the --prod and --release flags when building the APK file for production. It reduces the size by compressing the files.

ionic build android --prod --release

Solution 2:[2]

For people running into that issue with newer versions of Ionic:

In Ionic 4.3 there seems to be a bug in the build scripts. If you run a debug build, and then a release/production build, the www folder is not cleared and you will have all the sourcemap (.js.map) files in the www folder and thus in the final apk file.

See https://github.com/ionic-team/ionic-cli/issues/3954

Solution 3:[3]

Please use below command:
ionic cordova build android --minifycss --optimizejs --minifyjs --release [note:- "ionic cordova build android --prod --release" command have email pattern matching issue in a form, always give invalid email error hence not used that command]
Use below link for more details:
https://ionicframework.com/docs/cli/cordova/build/

Solution 4:[4]

Looks like a bug (not sure) but when live reload is active, the tree shaking feature causes the build to emit several chunk files into the www folder and i think the more you reload, the more files you'd have in there.

So you'd see files in the form

0.7f0f403f3c9f5914fbce.js
0.7f0f403f3c9f5914fbce.js.map
1.0db885b5a44ebd4ca57e.js
1.0db885b5a44ebd4ca57e.js.map
...and the number goes 0, 1, 2, ... n

You simply need to delete the www folder to discard the stale chunk files and rebuild your app.

The apk size should be justifiable after that.

Solution 5:[5]

Run cordova clean to cleanup project from build artifacts.

Solution 6:[6]

Do the following steps

  1. ng build --prod
  2. delete www before building from root folder
  3. ionic cordova build android --prod --release

Solution 7:[7]

check sourceMap in angular.json if true put it in false

Solution 8:[8]

I think I am a little late to answer but I here are some of my observations:

Ways to reduce production APK.

  1. Make sure you have created a production build before creating APK Use this command in order to create prod build

    ng build --prod
    
  2. Now use the below command to open android studio for further steps

    ionic capacitor run android --prod
    //--prod is optional if you have already performed step 1
    
  3. You can set minifyEnabled to minify the code which will also reduce the size of the APK drastically.

  4. There is one more thing that I have tried, once you have created a prod build you can delete the unused SVGs from the android source folder. I believe that is added in the assets and so in the prod APK. (Though the step 4 is I am not sure is safe to perform but it did not harm me in anyway) :)

I have seen the size has been reduced from 4.2 MB to 3.4 MB using step 4 ;)

Those are the steps I have performed to reduce the size of the ionic APKs.

Happy Coding!!