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

8

11

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?

Fabulous answered 22/6, 2018 at 12:40 Comment(3)
how are you generating it? are you using --prod flag?Indevout
I'm Not using --prod .I'll Check It Again anyway ThanxFabulous
I recently find out some version of keyboard module is too huge. Just check it once.Boy
C
14

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
Cardenas answered 22/6, 2018 at 12:48 Comment(0)
M
6

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

Meerkat answered 1/7, 2019 at 21:6 Comment(0)
B
4

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/

Barringer answered 22/6, 2018 at 15:33 Comment(0)
F
3

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.

Friction answered 2/1, 2020 at 11:45 Comment(0)
A
2

Run cordova clean to cleanup project from build artifacts.

Applicable answered 22/6, 2018 at 17:11 Comment(0)
A
1

Do the following steps

  1. ng build --prod
  2. delete www before building from root folder
  3. ionic cordova build android --prod --release
Abyssal answered 14/8, 2020 at 7:41 Comment(0)
E
0

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

Elwandaelwee answered 23/9, 2020 at 13:36 Comment(0)
R
0

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!!

Rebeccarebecka answered 22/4, 2021 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.