Cordova:如何设置特定于平台的配置。在cordova构建之后没有覆盖的xml文件?

时间:2022-03-31 07:30:02

I use Cordova with two platforms ios and android. When I change something in my

我使用Cordova有两个平台ios和android。当我改变我的

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构建覆盖的平台特定设置?

4 个解决方案

#1


10  

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,请考虑更新,因为许多关键问题都是固定的。

#2


5  

You need a cordova hook. I strongly recommend you see this link: Cordova Hooks

你需要一个科尔多瓦钩。我强烈建议你看看这个联系:科尔多瓦·胡克斯

More specifically:

更具体地说:

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

create an "after_prepare" cordova hook script file

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

#!/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/钩子/…,但最近它们已经转移到*的钩子/文件夹。

EDIT:

Here is a fully working script to create and run two slightly different cordova apps. 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.

这里有一个完整的脚本,用于创建和运行两个稍微不同的cordova应用程序。有趣的是,iOS平台文件夹配置中的属性。xml似乎被忽略了,因此这个脚本更新了*配置。使用iOS时使用xml。

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

#3


0  

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. 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.

由于我还没有找到更好的方法,我建议保存配置。将xml文件放在其他位置,并编写一个脚本,由cordova准备,然后复制配置。xml文件在编译之前应该放在什么位置,然后让它运行cordova编译。cordova只是做了一些准备工作,接着是一个cordova编译,所以在这两个步骤之间进行复制的脚本相当于一个cordova编译,但是它保留了您的自定义文件。

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. 选择产品>方案>编辑方案
  3. Select Build > Pre-actions from the left
  4. 从左边选择Build >预操作
  5. Click + and select "New Run Script Action"
  6. 点击+,选择“新运行脚本动作”

And for the script you can enter:

对于脚本,你可以输入:

  1. cordova prepare
  2. 科尔多瓦准备
  3. the cp commands to copy your saved config.xml and anything else you want to copy.
  4. cp命令复制您保存的配置。xml和任何你想复制的东西。
  5. anything else you'd like to do.
  6. 你还有什么事要做吗?

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.

这显然是一种解决方案,但它的优点是可以复制不同版本的配置。将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. In particular, you must remember to update your custom config.xml file whenever you add or remove a plugin.

需要注意的一点是,当您使用Cordova工具对配置进行更改时,必须非常小心。通过准备保存的xml或任何其他文件。特别是,您必须记住更新您的定制配置。无论何时添加或删除插件,都要使用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工具开发人员要么为这种合理的需求添加支持,要么改进文档(如果它已经得到支持),因为这一点。

#4


0  

Is it not more simple to add the platform folder into your repository, edit the files within the platform folder? In you regular build you only call cordova compile instead of cordova build, in order to prevent from overriding your platform files.

将平台文件夹添加到存储库中、编辑平台文件夹中的文件不是更简单吗?在您的常规构建中,您只需调用cordova编译,而不是cordova构建,以防止覆盖您的平台文件。

#1


10  

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,请考虑更新,因为许多关键问题都是固定的。

#2


5  

You need a cordova hook. I strongly recommend you see this link: Cordova Hooks

你需要一个科尔多瓦钩。我强烈建议你看看这个联系:科尔多瓦·胡克斯

More specifically:

更具体地说:

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

create an "after_prepare" cordova hook script file

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

#!/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/钩子/…,但最近它们已经转移到*的钩子/文件夹。

EDIT:

Here is a fully working script to create and run two slightly different cordova apps. 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.

这里有一个完整的脚本,用于创建和运行两个稍微不同的cordova应用程序。有趣的是,iOS平台文件夹配置中的属性。xml似乎被忽略了,因此这个脚本更新了*配置。使用iOS时使用xml。

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

#3


0  

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. 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.

由于我还没有找到更好的方法,我建议保存配置。将xml文件放在其他位置,并编写一个脚本,由cordova准备,然后复制配置。xml文件在编译之前应该放在什么位置,然后让它运行cordova编译。cordova只是做了一些准备工作,接着是一个cordova编译,所以在这两个步骤之间进行复制的脚本相当于一个cordova编译,但是它保留了您的自定义文件。

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. 选择产品>方案>编辑方案
  3. Select Build > Pre-actions from the left
  4. 从左边选择Build >预操作
  5. Click + and select "New Run Script Action"
  6. 点击+,选择“新运行脚本动作”

And for the script you can enter:

对于脚本,你可以输入:

  1. cordova prepare
  2. 科尔多瓦准备
  3. the cp commands to copy your saved config.xml and anything else you want to copy.
  4. cp命令复制您保存的配置。xml和任何你想复制的东西。
  5. anything else you'd like to do.
  6. 你还有什么事要做吗?

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.

这显然是一种解决方案,但它的优点是可以复制不同版本的配置。将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. In particular, you must remember to update your custom config.xml file whenever you add or remove a plugin.

需要注意的一点是,当您使用Cordova工具对配置进行更改时,必须非常小心。通过准备保存的xml或任何其他文件。特别是,您必须记住更新您的定制配置。无论何时添加或删除插件,都要使用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工具开发人员要么为这种合理的需求添加支持,要么改进文档(如果它已经得到支持),因为这一点。

#4


0  

Is it not more simple to add the platform folder into your repository, edit the files within the platform folder? In you regular build you only call cordova compile instead of cordova build, in order to prevent from overriding your platform files.

将平台文件夹添加到存储库中、编辑平台文件夹中的文件不是更简单吗?在您的常规构建中,您只需调用cordova编译,而不是cordova构建,以防止覆盖您的平台文件。