Explosion Pills Explosion Pills - 3 months ago 17
Android Question

Using environment variables / parameterizing config.xml

I am building an ionic/cordova project using the cordova-plugin-facebook4 plugin for Facebook authentication access. In

this looks like:

<plugin name="cordova-plugin-facebook4" spec="~1.7.1">
<variable name="APP_ID" value="1234567890123456"/>
<variable name="APP_NAME" value="My_Appy_App"/>

This works okay, but the
we are using is for the dev app and we have a separate facebook app for other environments such as QA.

Is there any way to parameterize these variables in
and have them be replaced as part of a build step? Something like:

<plugin name="foo" spec="~0.0.0">
<variable name="bar" value="${env.APP_ID}"/>

... and then run
APP_ID=baz ionic build android
or something like that.

I don't see anything in the cordova documentation that allows you to do this.


As you mentioned in the post, there is not much documentation about this in the official cordova documentation. After spending some time on this analysis, this is what I concluded:

There is some minimal help available ready-made to parameterize plugin variables available in config.xml. This can be achieved through preference variables as mentioned in the official cordova link. But the problem with this approach is that its working depends on how on the plugin is coded.

I tried this approach with the facebook plugin, but it didn't work :( I tried as below:

<preference name="MY_CUSTOM_STRING" default="12345678901234567" />
    <plugin name="cordova-plugin-facebook4" spec="~1.7.1">
        <variable name="APP_ID">$MY_CUSTOM_STRING</variable>
        <variable name="APP_NAME" value="My_Appy_App"/>

Tried out the same approach for google maps plugin and it worked :) I tried as below:

<preference name="MY_CUSTOM_STRING" default="12345678901234567" />
<plugin name="cordova-plugin-googlemaps" spec="~1.3.9">
    <variable name="API_KEY_FOR_ANDROID">$MY_CUSTOM_STRING</variable>

So all I could conclude is that the parameterizing approach is dependent on the core plugin code.

In the case of the facebook plugin, if you want to parameterize the APP_ID variable, then I guess hooks are the way to proceed. Even a simple windows batch file to replace a string match should be fine and it can be invoked on pre build action to achieve what you require. Hope it helps.


I do agree with Brandon's comments.

With the limited time i had, I was able to come up with the cordova hook that resolves this issue. It may be a crude way and it can be refined too but for now this approach works fine. Have posted the hook as a sample app in my github page and the README file has complete info about it. Hope it helps. Keep me posted.