In Cordova, how can I specify different package names for ios and android? [duplicate]

别等时光非礼了梦想. 提交于 2019-12-03 05:53:07

This is now built into CLI (finally):

In you your config.xml file-

Example:

<widget
    android-packageName="com.example.android"
    ios-CFBundleIdentifier="com.example.ios">

Source:

https://github.com/apache/cordova-lib/blob/master/cordova-lib/src/configparser/ConfigParser.js#L92

Edit: (Cordova-Lib has since been moved)

https://github.com/apache/cordova-lib/blob/master/cordova-common/src/ConfigParser/ConfigParser.js#L109

A way to automate this is by adding in an after prepare hook. I started out with the example of how to Replace Text Depending on Environment from here: http://devgirl.org/2013/11/12/three-hooks-your-cordovaphonegap-project-needs/.

I've got a project.json in my project that specifies what id I want to use for each platform:

{ 
    "android": 
    {
        "app_id": "<my Android Package name>"
    },
    "ios": 
    {
        "app_id": "<my iOS Bundle Identifier>"
    }
}

Then in the /hooks directory I have an /after_prepare directory with a replace_text.js as follows:

#!/usr/bin/env node

// this plugin replaces arbitrary text in arbitrary files
//

var fs = require("fs");
var path = require("path");

var rootdir = process.argv[2];

function replace_string_in_file(filename, to_replace, replace_with) {
    var data = fs.readFileSync(filename, "utf8");

    var result = data.replace(to_replace, replace_with);
    fs.writeFileSync(filename, result, "utf8");
}

function update_app_id(rootdir, platform, configobj) {
    var appId = configobj[platform].app_id,
        stringToReplace = "<value of the widget id property in the config.xml>";

    if (platform === "android") {

        replace_string_in_file(path.join(rootdir, "platforms/android/AndroidManifest.xml"), stringToReplace, appId);
        replace_string_in_file(path.join(rootdir, "platforms/android/res/xml/config.xml"), stringToReplace, appId);

    } else if (platform === "ios") {

        replace_string_in_file(path.join(rootdir, "platforms/ios/<app name>/<app name>-Info.plist"), stringToReplace, appId);
        replace_string_in_file(path.join(rootdir, "platforms/ios/<app name>/config.xml"), stringToReplace, appId);

    }
}

if (rootdir) {
    var ourconfigfile = path.join(rootdir, "project.json");
    var configobj = JSON.parse(fs.readFileSync(ourconfigfile, "utf8"));

    // Update each platform's specific configuration/properties files
    update_app_id(rootdir, "android", configobj);
    update_app_id(rootdir, "ios", configobj);
}

Please make sure to replace the values indicated with < > brackets with the values that pertain to your app/project.

Note: This has not been tested, but i think it should work.

After creating a new IOS platform in Cordova CLI, Edit the package name in the files below.

app/platforms/ios/<APP_NAME>/config.xml app/platforms/ios/www/config.xml

To avoid an ugly complication, i suggest you try this first in a different Git Branch of your project.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!