Cordova: Correct way to add plugin configuration and resources?

Viewed 5770

Cordova project. Firebase plugin (custom plugin). I am trying to figure out what the correct way to specify custom resource details that a plugin will use when the platform or plugin is added.

For instance, API_KEY, APP_ID and google-service.json for android, and GoogleServices-Info.plist for iOS

I have API_KEY and APP_ID nailed I think in plugin.xml

<platform name="android">
  <preference name="API_KEY"/>
  <preference name="APP_ID"/>

  <config-file parent="/resources" target="res/values/strings.xml">
    <string name="google_app_id">$APP_ID</string>
    <string name="google_api_key">$API_KEY</string>
  </config-file>
</platform>

So when the plugin is added I do cordova plugin add myfirebaseplugin --variable APP_ID=blah --variable API_KEY=blah

But what I have not worked out yet is the correct way to install google-service.json or Google-Service.Info.plist

I have tried having --variable IOS_FIREBASE_CONFIG="<path>" and using <source-file src="$IOS_FIREBASE_CONFIG" target-dir="$PACKAGE_NAME/Resources"/> in plugin.xml but it seems source-file wont expand $IOS_FIREBASE_CONFIG.

".../plugins/firebase/$IOS_FIREBASE_CONFIG" not found!

Can the plugin handle the setup the config based on variables I pass in? Or is this (copying the config files) something config.xml would handle during platform add?

I have looked at a few firebase plugins and how they deal with it and it seems they cop out and tell you to manually copy in the config files into the platform folders, which doesn't feel right.

As a last resort I can probably write a hook for platform add.

1 Answers

If you are using Cordova CLI 7 you can use resource-file tag in the config.xml

you don't have to do anything, just document it so the user put the file on the root of the project and adds this in the config.xml

For cordova-android 6.x.x and older

<platform name="android">
  <resource-file src="google-services.json" target="google-services.json" />
</platform>

For cordova-android 7.x.x

<platform name="android">
  <resource-file src="google-services.json" target="app/google-services.json" />
</platform>

For iOS

<platform name="ios">
  <resource-file src="GoogleService-Info.plist" />
</platform>
Related