I have been working some time with ionic 1 and I recently started a new project using ionic 2. It is awesome so far, but there is a problem I already had with ionic 1 which I hadn’t resolved as I would like to, and this time I thought I would be able to resolve with the App Scripts system but I really don’t see how to do it.
The basic problem I have is that I need to have a configuration file which changes depending on the environment, this is something I was used to do automatically with gradle on Android and it was always super useful. Let me give you an example so that is easy to understand, imagine I have the following configuration file named “config.json”:
When my application starts I read this file (similar to language files for translation) and I will use this different configuration values at runtime, in this case the url I will use to call my API. The straightforward solution is to update this file everytime I make a new build, but this is of course not a good idea. Now what I would like to do, is have different files to use in different environments:
android.json <--- Debug android file
ios.json <--- Debug ios file
android-release.json <--- Release android file
local.json <--- Config used for ionic serve
What I would like to do is before starting any build, replace the “config.json” file with one of these files. The solution I found in ionic 1 was to implement cordova hook scripts to do this in before_prepare, the problem with that is that this wouldn’t be called from “ionic serve” command, so I would have to replicate the code in a gulp task as well.
Now in order to replicate this in ionic 2 I was hoping to have a better way of doing this, simply having a script executed before every build where I can check environment (platform, debug, etc.) and place the correct file, but so far I haven’t found a good solution.
PS: here’s the documentation of the gradle feature I was mentioning: https://developer.android.com/studio/build/build-variants.html