Build Android with --release and --prod make an unstable apk

Hello All,

I am facing some problems with the release version of the apk, these problems happen only with the release version while the debug version works perfectly.

  • Cordova plugins not loaded always when you close and open the app different times.
  • Ionic Deploy not injected and it’s always undefined.

Steps:

  • Create a new ionic2 app with side menu
  • Add a cordova plugin, ed OneSignal
  • Add ionic deploy
  • Init/inject both the plugins (and use them :wink: :slight_smile:)
  • Build and install on a simulator/device

Build:

$ ionic build android --prod --release

$ zipalign -v -p 4 android-release-unsigned.apk android-release-unsigned-al.apk

$ apksigner sign -ks my-release-key.jks --out myapp.apk android-release-unsigned-al.apk

I also opened an issue, but they said to ask here. Any help? Thanks.

So after some testing I figured it out that all the following will make a working app

ionic build android --release
ionic build android --prod
ionic build android --prod --aot

The following command will make an unstable app with the problem described above

ionic build android --release --prod

Hi anthem182,

first of all thank you for your solution. I am facing the same problem :persevere:

When you run the command

ionic build android --prod

what is your debug print saying? … look at this:
image

a dev build is started and NOT a production build… do you have the same output or is your app building in production?
I think the production build is causing the messed up release-apk.

Hi @dom_es,

Not a problem, this seems not to be a quite common problem some people might have some might not, I have also opened a bug on the ionic app script repo, hoping that someone will take care of it!

This issue is related with the building process and each ionic app script version has its own problem.

First it’s better if you update the app script to version 1.0.1 or to the latest, both have the same error but at least you will have a smallest binary.

npm install @ionic/app-scripts@latest --save-dev

This is what I have done:

npm run ionic:build

ionic build android --release

Sign the apk after that.

After some testing I figured it out that the flags --prod and --aot make the bundle unstable.

Let me know if you were able to fix it.

HI there ,
I am trying to create a prod build but getting a error

[13:54:57] ionic-app-script task: “build”
[13:54:57] TypeError: Cannot read property ‘codeGen’ of undefined
TypeError: Cannot read property ‘codeGen’ of undefined
at Object.doCodegen (E:\projects\DIB_FULLSVN\SDLC\Source Code\branches\1.0\C
lient\node_modules@ionic\app-scripts\dist\aot\codegen.js:6:50)
at E:\projects\DIB_FULLSVN\SDLC\Source Code\branches\1.0\Client\node_modules
@ionic\app-scripts\dist\aot\aot-compiler.js:42:30

earlier i was able to create build using ionic build android --prod , but now its showing error.
Please help

Hello @7728,

which version of ionic script are you using? are you getting the same error using a debug script?

Hello guys.
@7728, @anthem182. I’m trying build my app for production but get same error ionic-app-script’s "Cannot read property ‘codeGen’ of undefined
TypeError: Cannot read property ‘codeGen’ of undefined
at Object.doCodegen "
I built for debug version and it worked perfectly. I’m using ionic-app-scripts 1.3.5.
Can anyone help me?

HI @vinh2709 ,

I faced the same issue. I did not find any solution for that. So what i did I created a new project and used my “src” folder in that, after that you have to change app.module.ts file and some tweaks in the code to make it running on newer version, as the newer version will be using angular 4.

Once you will do that you will be able to create the prod apk with in 2 to 3 mins (It was taking 15 mins earlier for me).

Or you can directly update the “package.json” file with the updated version of angular cli and other modules.

1 Like

Also be sure that when building for production the code doesn’t have any wrong call or reference to variable and functions. Sometimes it compiles in debug but fails in prod.

1 Like

Thank you all so much for your reply. I’m going to upgrade ionic now. Hope it can working well.

It is dev vs. release build. --prod only causes the build to be minified etc. If you want a production build, add --release to your command.

The problem is adding prod and release together, the app doesn’t work on android, in case you use ionic deploy or other cordova libs.