问题描述
我正在使用cordova-plugin-facebook4插件来建立一个离子/ cordova项目,用于Facebook身份验证访问。在 config.xml
中,这看起来像: < plugin name = cordova-plugin-facebook4spec =〜1.7.1>
< variable name =APP_IDvalue =1234567890123456/>
< variable name =APP_NAMEvalue =My_Appy_App/>
< / plugin>
这个工作没问题,但是 APP_ID
正在使用的是开发应用程序,我们有一个单独的Facebook应用程序为其他环境,如QA。
有什么办法参数化这些变量在 config.xml
并将它们替换为构建步骤的一部分?如下所示:
< plugin name =foospec =〜0.0.0>
< variable name =barvalue =$ {env.APP_ID}/>
< / plugin>
...然后运行 APP_ID = baz ion build android
或类似的东西。
我没有在cordova文档中看到任何可以让你这样做的东西。
正如你在帖子中提到的,在cordova文档中没有关于此的很多文档。在这个分析上花了一些时间,这就是我的结论:
现在提供了一些最小的帮助,可以在 config.xml中
。这可以通过偏好变量来实现,如。但是这种方法的问题在于它的工作取决于插件是如何编码的。
我用facebook插件尝试了这种方法,但它没有起作用: (我试过如下:
< preference name =MY_CUSTOM_STRINGdefault =12345678901234567/>
< plugin name =cordova-plugin-facebook4spec =〜1.7.1>
< variable name =APP_ID> $ MY_CUSTOM_STRING< / variable>
<变量名=APP_NAMEvalue =My_Appy_App/>
< / plugin>
试图使用相同的方法来google地图插件,它的工作:)我尝试如下:
< preference name =MY_CUSTOM_STRING default =12345678901234567/>
< plugin name =cordova-plugin-googlemapsspec =〜1.3.9>
< variable name =API_KEY_FOR_ANDROID> $ MY_CUSTOM_STRING< / variable>
< / plugin>
所以我可以得出结论,参数化方法取决于核心的插件代码。
在Facebook插件的情况下,如果要参数化 APP_ID
变量,那么我猜钩子是进行的方式。即使是一个简单的Windows批处理文件替换字符串匹配也应该很好,它可以在预构建操作上调用来实现你所需要的。希望它有帮助。
更新:
我同意布兰登的评论。
在我有限的时间内,我能够拿出解决这个问题的cordova钩子。这可能是一个粗俗的方法,它也可以被改进,但是现在这种方法工作正常。在我的中发布了一个示例应用程序的钩子,README文件具有完整的信息。希望有帮助让我发贴。
I am building an ionic/cordova project using the cordova-plugin-facebook4 plugin for Facebook authentication access. In config.xml
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"/>
</plugin>
This works okay, but the APP_ID
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 config.xml
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}"/>
</plugin>
... 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"/>
</plugin>
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>
</plugin>
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.
UPDATE:
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.
这篇关于使用环境变量/参数化config.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!