2
votes

What´s the best workflow to deal with to environments (DEV and PROD) in one ionic project with an Android app regarding the google-services.json which needs to be in filesystem in folder /android/app.

For firebase environments there is a great way with firebase use... but what´s the best way for google-services.json. I´v two different files, one for firebase DEV and one for firebase PRODenter code here

1

1 Answers

0
votes

I did it only for Android as per below and it was working for me:-

  1. Create the following files and directories in your project (these are the source files where configurations for both Firebase projects are hardcoded. config.xml needs to be replaced because of the different package name):

  2. leveraging ionic hooks, create the file build-before.js which contains the following script:

    module.exports = function(ctx) {  
    console.log(ctx);
    var fs = require("fs");
    var xmlConfig = "config.xml"
     if (ctx.build.configuration == 'production') {
     var srcfile = "src/production/google-services.json";
     var destfile = "google-services.json";
     var srcConfigfile = "src/production/config.xml";
     var destConfigfile = "config.xml";
     console.log("copying " + srcfile + " to " + destfile);
     fs.createReadStream(srcConfigfile).
     pipe(fs.createWriteStream(destConfigfile));
    
     fs.createReadStream(srcfile).pipe(fs.createWriteStream(destfile));
    
    
     } else {
       console.log("TARGET environment variable is not set.  Using default values.");
    
       var srcfile = "src/development/google-services.json";
       var destfile = "google-services.json";
       var srcConfigfile = "src/development/config.xml";
       var destConfigfile = "config.xml";
       console.log("copying " + srcfile + " to " + destfile);
       fs.createReadStream(srcConfigfile).
       pipe(fs.createWriteStream(destConfigfile));
    
       fs.createReadStream(srcfile).
       pipe(fs.createWriteStream(destfile));
       }
     }
    

3.In file ionic.config.json add the following:

   "hooks": {
   "build:before": "./scripts/build-before.js"
  }

4.Check with the following commands:

$ ionic cordova build android --prod

$ ionic cordova build android

$ ionic serve --prod

$ ionic serve