I have 3 different environments where depending on the environment in use, the application id and name (as well as other variables like cloud URLs) are different.
What I want, is to implement a Cordova hook that automatically makes the modifications to the config.xml file without having to do this manually.
I have managed to do this for the
<resource-file src="src/certificates/${CERTIFICATE_PATH}" target="src/certificates/${CERTIFICATE_PATH}" />
as well as for other variables.
The environment files:
environment.common.ts
environment.a.ts
environment.b.ts
environment.c.ts
environment.ts
I have this before_platform_add hook:
const fs = require('fs');
const path = require('path');
const compile = require('es6-template-strings/compile');
const resolveToString = require('es6-template-strings/resolve-to-string');
const helpers = require('./helpers');
module.exports = function (ctx) {
const FILES = {
SRC: "config.tpl.xml",
DEST: "config.xml"
};
const ROOT_DIR = ctx.opts.projectRoot;
const srcFileFull = path.join(ROOT_DIR, FILES.SRC);
const destFileFull = path.join(ROOT_DIR, FILES.DEST);
const templateData = fs.readFileSync(srcFileFull, 'utf8');
let environment = helpers.getEnvironment(ctx); // I am getting the environment (a, b, or c) in JSON format via parsing the ts file with regex
let config = helpers.getHookConfig(environment); // This is a JSON object with keys config.APPLICAITON_ID, config.APPLICATION_NAME, config.CERTIFICATE_PATH, ...
var compiled = compile(templateData);
var content = resolveToString(compiled, config);
fs.writeFileSync(destFileFull, content);
};
How it works (inspired by this solution):
config.tpl.xml
<?xml version='1.0' encoding='utf-8'?>
<widget id="${APPLICATION_ID}" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>${APPLICATION_NAME}</name>
...
config.xml after the hook:
<?xml version='1.0' encoding='utf-8'?>
<widget id="the_ID" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>the_Name/name>
...
The name and ID are successfully updating.
THE PROBLEM
Let's suppose that it is the first time I am building the application.
I do it like this:
cordova platform remove android
cordova platform add android --variable var1=true
The hook prints correct environment variables depending on --variable var1=true
and after the hook, the terminal prints:
Using cordova-fetch for cordova-android@^9.0.0
Adding android project...
Creating Cordova project for the Android platform:
Path: platforms/android
**Package: var1**
Name: a_name
Activity: MainActivity
Android target: android-29
After that, if I do
cordova platform remove android
cordova platform add android --variable var2=true
The hook prints correct environment variables depending on --variable var2=true
and after the hook, the terminal prints:
Using cordova-fetch for cordova-android@^9.0.0
Adding android project...
Creating Cordova project for the Android platform:
Path: platforms/android
**Package: var1** //STACKS IN VAR1
Name: a_name
Activity: MainActivity
Android target: android-29
So, although the hook does its job and changes the app_id and app_name at config.xml, the cordova still caught the previous app_id and 'thinks' that the previous package needs to be used. So the config.xml listens to the new app id but the cordova to the previous one.
ERROR THROWN:
No Java files found that extend CordovaActivity.
Any ideas on whether the cordova caches the package name or anything would be much appreciated.