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

后端 未结 4 772
说谎
说谎 2021-02-08 14:30

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

Project/config.xml 

it will be merged into

Pro         


        
相关标签:
4条回答
  • 2021-02-08 15:09

    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.

    0 讨论(0)
  • 2021-02-08 15:18

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

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

    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.

    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
    
    0 讨论(0)
  • 2021-02-08 15:22

    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

    And if you use an older version of Cordova, consider updating as many critical issues were fixed.

    0 讨论(0)
  • 2021-02-08 15:22

    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.

    If you are using the xcode IDE, you can add a pre-action script to the iOS project's build:

    1. Select Product > Scheme > Edit Scheme
    2. Select Build > Pre-actions from the left
    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.
    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.

    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.

    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.

    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.

    0 讨论(0)
提交回复
热议问题