Configure build flavors for cordova

2019-04-08 16:30发布

Hi this question is most likely just a cordova question. I've seen simular questions but never with a satisfactory answer.

What my end goal would be, is a simple way to build multiple Flavors of both an Android, iOS and Windows Phone app.

What my biggest requirements are:

  • Change the namespace. So the widget attributes in config.xml would preferably be overwritten. Also the name and description would help.
  • Generating different icons and splash screens. I'd likely set up directories relevant to the package name I'm using. Putting a high res Splash and Icon in here, using ionic's resources command to generate the correct files.
  • Color scheme changes based on target
  • Optionally, dynamic configuration.

What I've currently done is added some code in my webpack.config (my ionic project uses webpack). To change sass colours, I could easily add some other options here as well.

This is probably the worst way to implement it but I just needed a working prototype

var flavour = "some.namespace.dir";
var ENV = "TEST";

for(var i = 0; i < args.length ; i++) {
    if (args[i] == "--env") {
        if (args[i+1]) {
            ENV = args[i+1];
        }
    }

    if (args[i] == "--flavour") {
        if (args[i+1]) {
            flavour = args[i+1];
        }
    }
}

So this would check if the node command had any --flavour or --env flag to set these properties. after this I'll load the config in a dumb way.

var config =
    JSON.parse(
        require('fs').readFileSync(
            require('path').resolve(
                __dirname,
                'flavours/' + flavour + ".json"),
            'utf8'));

Allright so I have a json object, I can put nearly anything in here, so for doing custom styling we can use the sass loader.

sassLoader: {
    includePaths: [
        'node_modules/ionic-angular',
        'node_modules/ionicons/dist/scss'
    ],
    data: "$custom-primary: " + config.colorPrimary + " ; $custom-accent: " + config.colorAccent + " ;"
},

So this actually lets me add variables to the sass build, great! as for the configuration:

new DefinePlugin({
   'ENV': JSON.stringify(ENV),
   'appConfig': JSON.stringify(config)
})

So I've got these things which I can use to have the logic inside the app be specific to a flavour.

I'd love to hear how dissapointed people are in my implementation and if they have suggestions to improve any of this.

Also as of now I'm thinking I will need a bash script to change my config.xml and the resources. I can either have a template file in which I replace the namespaces, or I just create config.xml files in my "flavour" folders and copy these in place.

Any suggestions or wtf's people can share? As for now I'll probably go with the config in each directory approach, together with the resources. That way I could just hit a bash file to build compile and sign for any flavour.

0条回答
登录 后发表回答