无法修改自动生成的Config.xml,能否通过Plugin.xml添加Universal Links配置?
Got it, let's tackle this problem. Since you can't modify config.xml directly, we can hardcode the Universal Links configuration into the cordova-universal-links-plugin source code instead. Here's how to do it step by step:
1. Locate the Plugin Directory
First, find the installed plugin folder in your Cordova project. It should be at this path:
plugins/cordova-universal-links-plugin/
2. Modify the Configuration Parser
The plugin reads Universal Links settings from config.xml using a dedicated parser file. We'll override this logic to return our desired configuration directly.
Navigate to lib/configParser.js inside the plugin directory. Open this file, and replace the existing parse function with our hardcoded configuration.
Original (simplified) code:
module.exports.parse = function(xmlContent) { // Existing logic to parse <universal-links> from config.xml // ... return config; };
Modified code:
module.exports.parse = function(xmlContent) { // Hardcode our Universal Links configuration return { hosts: [ { name: "example.com", paths: [ { url: "/some/path" } ] } ] }; };
This forces the plugin to use your specified host and path regardless of what's in config.xml.
3. Regenerate Platform Configurations
Run the Cordova prepare command for your target platforms to let the plugin generate the required native configuration files:
# For iOS cordova prepare ios # For Android cordova prepare android
- On iOS, this will update the
Entitlements-*.plistfile with thecom.apple.developer.associated-domainsentitlement (applinks:example.com) and handle path associations. - On Android, this will create/update the
assetlinks.jsonfile inandroid/app/src/main/assets/.well-known/with the correct domain and app ID mappings (if the plugin supports it).
4. Key Notes
- Plugin Updates: If you ever update the
cordova-universal-links-pluginin the future, your changes will be overwritten. To avoid this, consider forking the plugin repository, applying your changes there, and installing the plugin from your fork instead of the official repo. - Domain Setup: Don't forget to configure the
apple-app-site-association(iOS) andassetlinks.json(Android) files on yourexample.comdomain—these are required for Universal Links to work properly. - Testing: Always rebuild your entire project after making these changes to ensure the native configurations are applied correctly.
内容的提问来源于stack exchange,提问作者Rendy

