简体   繁体   English

科尔多瓦:如何设置特定于平台的config.xml文件,科尔多瓦构建后该文件不会被覆盖?

[英]Cordova: How to set Platform-specific config.xml file which is not overwritten after cordova build?

I use Cordova with two platforms ios and android. 我将Cordova与ios和android两个平台一起使用。 When I change something in my 当我改变自己的东西

Project/config.xml 

it will be merged into 它将合并到

Project/platforms/android/res/xml/config.xml  
Project/platforms/ios/Project/config.xml

after doing cordova build . 做完科尔多瓦后。

How can I set platform specific settings which will not be overwritten with cordova build? 如何设置特定于平台的设置,这些设置将不会被cordova build覆盖?

Define your platform specific configuration inside platform elements. 在平台元素内定义平台特定的配置。

<platform name="android">
    <preference name="Fullscreen" value="true" />
    ...
</platform>

See bottom of: http://cordova.apache.org/docs/en/4.0.0/config_ref_index.md.html 请参阅以下内容的底部: http : //cordova.apache.org/docs/en/4.0.0/config_ref_index.md.html

And if you use an older version of Cordova, consider updating as many critical issues were fixed. 并且,如果您使用的是旧版本的Cordova,请考虑进行更新,因为已修复了许多关键问题。

You need a cordova hook. 您需要一个科尔多瓦钩子。 I strongly recommend you see this link: Cordova Hooks 我强烈建议您查看此链接: Cordova挂钩

More specifically: 进一步来说:

  1. In the hooks/ top-level folder of your app, create a subfolder named "after_prepare". 在应用程序的hooks /顶级文件夹中,创建一个名为“ after_prepare”的子文件夹。
  2. In hooks/after_prepare , create an executable script that your platform knows how to run (eg on Linux: create a BASH script; on Windows: create a BAT file; on any platform: create a Node.js script). 在hooks / after_prepare中,创建您的平台知道如何运行的可执行脚本(例如,在Linux上:创建BASH脚本;在Windows上:创建BAT文件;在任何平台上:创建Node.js脚本)。 Let's say you want a bash script: 假设您想要一个bash脚本:

create an "after_prepare" cordova hook script file 创建一个“ after_prepare” cordova钩子脚本文件

mkdir -p hooks/after_prepare
touch hooks/after_prepare/change_my_configxml.sh
chmod 755 hooks/after_prepare/change_my_configxml.sh
edit hooks/after_prepare_change_my_configxml.sh

hooks/after_prepare/change_my_configxml.sh 钩子/ after_prepare / change_my_configxml.sh

#!/bin/bash

echo ${CORDOVA_VERSION};
echo ${CORDOVA_PLATFORMS};
echo ${CORDOVA_PLUGINS};
echo ${CORDOVA_HOOK};
echo ${CORDOVA_CMDLINE};
# == put commands to make your config xml changes here ==
# == replace this statement with your own ==
sed "s/index\.html/http:\/\/my.example.com\?platform=ios\&amp;cordova=3.4.0/" platforms/ios/www/config.xml > config.tmp && mv -f -v config.tmp platforms/ios/www/config.xml

Note that cordova hooks were originally placed in .cordova/hooks/... , but recently they have moved to a top-level hooks/ folder. 请注意,cordova挂钩最初放置在.cordova / hooks / ...中,但是最近它们已移至顶级hooks /文件夹。

EDIT: 编辑:

Here is a fully working script to create and run two slightly different cordova apps. 这是一个完全有效的脚本,用于创建和运行两个略有不同的cordova应用程序。 Interestingly, the attribute in the iOS platform folder's config.xml seems to get ignored, so this script updates the top-level config.xml when dealing with iOS. 有趣的是,iOS平台文件夹的config.xml中的属性似乎被忽略了,因此此脚本在处理iOS时会更新顶级config.xml。

createMultiPlatformCordovaApp.sh createMultiPlatformCordovaApp.sh

#!/bin/bash
# ==== Create cordova apps with different URLs for android and iOS
cordova create appFolder com.example.myApp "My App"

cd appFolder/

cordova platform add ios
cordova platform add android

# ==== make an after_prepare hook script
mkdir -p hooks/after_prepare 
touch hooks/after_prepare/change_my_configxml.sh
chmod 755 hooks/after_prepare/change_my_configxml.sh
# ==== start of hook script content ====
echo '#!/bin/bash

    echo "CORDOVA_VERSION: ${CORDOVA_VERSION}";
    echo "CORDOVA_PLATFORMS: ${CORDOVA_PLATFORMS}";
    echo "CORDOVA_PLUGINS: ${CORDOVA_PLUGINS}";
    echo "CORDOVA_HOOK: ${CORDOVA_HOOK}";
    echo "CORDOVA_CMDLINE: ${CORDOVA_CMDLINE}";
    # == put commands to make your config xml changes here ==
    # == replace these statement with your own ==

    if [ ! -f platforms/android/res/xml/config.xml ] 
    then 
      cp -v -f config.xml platforms/android/res/xml/config.xml; 
    fi
    if [[ ${CORDOVA_PLATFORMS} == android ]]; then
        sed -E "s/<content src=\"[^\"]+\"/<content src=\"http:\/\/www.google.com\/search\?q=Google%20Nexus%205\&amp;hl=xx-bork\"/" platforms/android/res/xml/config.xml > config.tmp && mv -f -v config.tmp platforms/android/res/xml/config.xml
    fi
    if [[ ${CORDOVA_PLATFORMS} == ios ]]; then
        sed -E "s/<content src=\"[^\"]+\"/<content src=\"http:\/\/www.google.com\/search\?q=iPad%20Retina\&amp;hl=xx-bork\"/"  config.xml > config.tmp && mv -f -v config.tmp config.xml
    fi
' > hooks/after_prepare/change_my_configxml.sh
# ==== end of hook script content ====

cordova prepare android
cordova build android
cordova emulate android

cordova prepare ios
cordova build ios
cordova emulate ios

Since I have not yet found a better way, I suggest saving the config.xml files as you want them in some other location, and writing a script which does a cordova prepare , then copies the config.xml file to where it should go before compiling, and then have it run cordova compile . 由于尚未找到更好的方法,建议您将config.xml文件保存在其他位置,并编写一个脚本进行cordova prepare ,然后将config.xml文件复制到应该放置的位置编译,然后让它运行cordova compile cordova buld just does a cordova prepare followed by a cordova compile , so a script that does this copying between these two steps is equivalent to a cordova build , but one which preserves your custom files. cordova buld只是先进行cordova prepare然后进行cordova compile ,所以在这两个步骤之间进行此复制的脚本等效于cordova build ,但是会保留您的自定义文件。

If you are using the xcode IDE, you can add a pre-action script to the iOS project's build: 如果您使用的是xcode IDE,则可以向iOS项目的构建中添加预操作脚本:

  1. Select Product > Scheme > Edit Scheme 选择产品>方案>编辑方案
  2. Select Build > Pre-actions from the left 从左侧选择Build> Pre-actions
  3. Click + and select "New Run Script Action" 单击+,然后选择“新建运行脚本操作”

And for the script you can enter: 对于脚本,您可以输入:

  1. cordova prepare
  2. the cp commands to copy your saved config.xml and anything else you want to copy. cp命令复制您保存的config.xml以及您要复制的其他任何内容。
  3. anything else you'd like to do. 您想做的其他事情。

After that, doing a build from inside xcode will refresh your common code files, preserve your edits to files, and do a build. 之后,从xcode内部进行构建将刷新您的通用代码文件,保留对文件的编辑并进行构建。

This is clearly a workaround, but it does have the virtue that you can copy different versions of config.xml and of any other files into different platform directories. 显然,这是一种解决方法,但是它确实具有可以将config.xml和其他文件的不同版本复制到不同平台目录的优点。

One caveat of this is that you must be careful whenever you do something with the Cordova tools which make changes to config.xml or any other file you are preserving through a prepare that you would want to keep. 需要注意的一个问题是,每当使用Cordova工具进行某些操作时都必须小心,该工具通过您要保留的准备对config.xml或要保存的任何其他文件进行更改。 In particular, you must remember to update your custom config.xml file whenever you add or remove a plugin. 特别是,无论添加或删除插件,您都必须记住要更新自定义config.xml文件。

I hope the Cordova tool developers either add support for this kind of reasonable desire, or improve the documentation if it actually is already supported, because this. 我希望Cordova工具开发人员可以增加对这种合理愿望的支持,或者如果实际上已经支持它,则可以改进文档,因为如此。

Is it not more simple to add the platform folder into your repository, edit the files within the platform folder? 是不是更简单添加的platform文件夹复制到你的资料库,编辑中的文件platform文件夹? In you regular build you only call cordova compile instead of cordova build , in order to prevent from overriding your platform files. 在常规构建中,您仅调用cordova compile而不是cordova build ,以防止覆盖platform文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM