16

In a custom Cordova plugin, how can I config a specific .framework file in plugin.xml such that it will be added to the "Embedded Binaries" section in Xcode? If that's not currently possible directly in plugin.xml, I'm open to alternative suggestions.

2
  • why has to be on embedded binaries and not in linked frameworks and libraries? can you tell which framework are you trying to use? Apr 19, 2016 at 6:28
  • It's a custom framework, don't have it's source, not a public framework, it was given to our company by a 3rd party, it has to be embedded and not linked otherwise I get a runtime exception upon launch "Image not found".
    – Alon Amir
    Apr 19, 2016 at 8:35

5 Answers 5

34

I've implemented a workaround until it's supported by Cordova's plugin.xml, hopefully, in the future, once an embed property in such entries will have the same effect: <framework embed="true" src="..." />, for now, this property does not help, hence the following workaround.

The following solution worked using Cordova version 5.3.3.

First, make sure to add the framework entry to plugin.xml:

<framework src="pointToYour/File.framework" embed="true" />

embed="true" doesn't work for now, but add it anyway.

We're gonna create a hook, declare that in your plugin.xml:

<hook type="after_platform_add" src="hooks/embedframework/addEmbedded.js" />

Next, there's a specific node module we're gonna need in our hook's code, that module is node-xcode.

Install node-xcode (must be 0.8.7 version or above):

npm i xcode

Finally, the code for the hook itself -

addEmbedded.js file:

'use strict';

const xcode = require('xcode'),
    fs = require('fs'),
    path = require('path');

module.exports = function(context) {
    if(process.length >=5 && process.argv[1].indexOf('cordova') == -1) {
        if(process.argv[4] != 'ios') {
            return; // plugin only meant to work for ios platform.
        }
    }

    function fromDir(startPath,filter, rec, multiple){
        if (!fs.existsSync(startPath)){
            console.log("no dir ", startPath);
            return;
        }

        const files=fs.readdirSync(startPath);
        var resultFiles = []
        for(var i=0;i<files.length;i++){
            var filename=path.join(startPath,files[i]);
            var stat = fs.lstatSync(filename);
            if (stat.isDirectory() && rec){
                fromDir(filename,filter); //recurse
            }

            if (filename.indexOf(filter)>=0) {
                if (multiple) {
                    resultFiles.push(filename);
                } else {
                    return filename;
                }
            }
        }
        if(multiple) {
            return resultFiles;
        }
    }

    function getFileIdAndRemoveFromFrameworks(myProj, fileBasename) {
        var fileId = '';
        const pbxFrameworksBuildPhaseObjFiles = myProj.pbxFrameworksBuildPhaseObj(myProj.getFirstTarget().uuid).files;
        for(var i=0; i<pbxFrameworksBuildPhaseObjFiles.length;i++) {
            var frameworkBuildPhaseFile = pbxFrameworksBuildPhaseObjFiles[i];
            if(frameworkBuildPhaseFile.comment && frameworkBuildPhaseFile.comment.indexOf(fileBasename) != -1) {
                fileId = frameworkBuildPhaseFile.value;
                pbxFrameworksBuildPhaseObjFiles.splice(i,1); // MUST remove from frameworks build phase or else CodeSignOnCopy won't do anything.
                break;
            }
        }
        return fileId;
    }

    function getFileRefFromName(myProj, fName) {
        const fileReferences = myProj.hash.project.objects['PBXFileReference'];
        var fileRef = '';
        for(var ref in fileReferences) {
            if(ref.indexOf('_comment') == -1) {
                var tmpFileRef = fileReferences[ref];
                if(tmpFileRef.name && tmpFileRef.name.indexOf(fName) != -1) {
                    fileRef = ref;
                    break;
                }
            }
        }
        return fileRef;
    }

    const xcodeProjPath = fromDir('platforms/ios','.xcodeproj', false);
    const projectPath = xcodeProjPath + '/project.pbxproj';
    const myProj = xcode.project(projectPath);

    function addRunpathSearchBuildProperty(proj, build) {
       const LD_RUNPATH_SEARCH_PATHS =  proj.getBuildProperty("LD_RUNPATH_SEARCH_PATHS", build);
       if(!LD_RUNPATH_SEARCH_PATHS) {
          proj.addBuildProperty("LD_RUNPATH_SEARCH_PATHS", "\"$(inherited) @executable_path/Frameworks\"", build);
       } else if(LD_RUNPATH_SEARCH_PATHS.indexOf("@executable_path/Frameworks") == -1) {
          var newValue = LD_RUNPATH_SEARCH_PATHS.substr(0,LD_RUNPATH_SEARCH_PATHS.length-1);
          newValue += ' @executable_path/Frameworks\"';
          proj.updateBuildProperty("LD_RUNPATH_SEARCH_PATHS", newValue, build);
       }
    }

    myProj.parseSync();
    addRunpathSearchBuildProperty(myProj, "Debug");
    addRunpathSearchBuildProperty(myProj, "Release");

    // unquote (remove trailing ")
    var projectName = myProj.getFirstTarget().firstTarget.name.substr(1);
    projectName = projectName.substr(0, projectName.length-1); //Removing the char " at beginning and the end.

    const groupName = 'Embed Frameworks ' + context.opts.plugin.id;
    const pluginPathInPlatformIosDir = projectName + '/Plugins/' + context.opts.plugin.id;

    process.chdir('./platforms/ios');
    const frameworkFilesToEmbed = fromDir(pluginPathInPlatformIosDir ,'.framework', false, true);
    process.chdir('../../');

    if(!frameworkFilesToEmbed.length) return;

    myProj.addBuildPhase(frameworkFilesToEmbed, 'PBXCopyFilesBuildPhase', groupName, myProj.getFirstTarget().uuid, 'frameworks');

    for(var frmFileFullPath of frameworkFilesToEmbed) {
        var justFrameworkFile = path.basename(frmFileFullPath);
        var fileRef = getFileRefFromName(myProj, justFrameworkFile);
        var fileId = getFileIdAndRemoveFromFrameworks(myProj, justFrameworkFile);

        // Adding PBXBuildFile for embedded frameworks
        var file = {
            uuid: fileId,
            basename: justFrameworkFile,
            settings: {
                ATTRIBUTES: ["CodeSignOnCopy", "RemoveHeadersOnCopy"]
            },

            fileRef:fileRef,
            group:groupName
        };
        myProj.addToPbxBuildFileSection(file);


        // Adding to Frameworks as well (separate PBXBuildFile)
        var newFrameworkFileEntry = {
            uuid: myProj.generateUuid(),
            basename: justFrameworkFile,

            fileRef:fileRef,
            group: "Frameworks"
        };
        myProj.addToPbxBuildFileSection(newFrameworkFileEntry);
        myProj.addToPbxFrameworksBuildPhase(newFrameworkFileEntry);
    }

    fs.writeFileSync(projectPath, myProj.writeSync());
    console.log('Embedded Frameworks In ' + context.opts.plugin.id);
};

What this hook actually does:

  1. Creates a "Build Phase" named after your plugin id, configured to "Copy Files", destination of that copy is "Frameworks".
  2. Finds and adds your .framework files to the above Build Phase, in turn, embedding it.
  3. Sets an Xcode build property named LD_RUNPATH_SEARCH_PATHS to also look for embedded frameworks in "@executable_path/Frameworks" (That's were the embedded framework is going to be copied to after the "Copy Files"->"Frameworks" Build Phase
  4. Configures the ATTRIBUTES key by setting "CodeSignOnCopy" and "RemoveHeadersOnCopy" for your .framework files.
  5. Removes your .framework files from the FrameworksBuildPhase and re-adds them to the FrameworksBuildPhase as new separated PBXBuildFiles (Same PBXFileReference), it has to be done in order for the "CodeSignOnCopy" to mean anything, without removing it, if you open the project with Xcode, you will not find a checkmark in the build phase that says it will sign it.

Updated 1: hook code, modifications:

  1. The hook automatically finds your .framework files, no need to edit the hook.
  2. Added an important modification, setting ATTRIBUTES "CodeSignOnCopy" and "RemoveHeadersOnCopy" for your .framework files.
  3. Improved the hook to allow it to work in such a case where multiple plugins use this hook.

Update 2

  1. Since my pull request has been accepted, there's no longer a need to install my own fork.
  2. Improved hook code.

Update 3 (19/09/2016)

Modified hook script according to Max Whaler's suggestion, as I experienced the same issue over Xcode 8.

Final Note

Once you upload your app to the AppStore, if validation fails due to unsupported architectures (i386, etc...), try the following Cordova plugin (only hook, no native code): zcordova-plugin-archtrim

8
  • This - npm i xcode - worked to install the xcode module.
    – arjunattam
    Nov 1, 2016 at 4:12
  • 1
    Thank you for your answer! But i have another question; to make it work I have to add the plugin first and after that the iOS platform. The framework is copied to embedded only in these steps. Is there anything I can do so the plugin copies the framework to embedded, if the iOS platform is already installed ?
    – Macaret
    Nov 4, 2016 at 11:26
  • @Macarat if you're not using the CLI workflow then you don't need any of this, you simply open Xcode, and add your framework file to the embedded binaries section, nothing cordova related.
    – Alon Amir
    Nov 4, 2016 at 15:28
  • @AlonAmir thanks for the great solution! This technique is being used in the cordova-plugin-braintree plugin and while it appears the frameworks are being embedded, when building, it's telling me the headers cannot be found. Any suggestions? Thank you!
    – jdixon04
    Dec 4, 2016 at 23:10
  • @jdixon04 Thanks, good to know ! regarding your question, make sure you're using the #import statement with < and > (not ") when importing headers from the framework.
    – Alon Amir
    Dec 4, 2016 at 23:15
11

For adding libraries to "Embedded Binaries" section in Xcode (Starting from cordova-ios 4.4.0 and cordova 7.0.0), put this in your plugin.xml:

<framework src="src/ios/XXX.framework"   embed="true" custom="true" />

For adding libraries to "Linked Frameworks and Libraries" section in Xcode, put this in your plugin.xml:

<source-file src="src/ios/XXX.framework" target-dir="lib" framework="true" />

Both of them can exist at the same time. For example:

<!-- iOS Sample -->
<platform name="ios">
    ....
    <source-file src="src/ios/XXX.m"/>
    <source-file src="src/ios/XXX.framework" target-dir="lib" framework="true" />
    <framework src="src/ios/XXX.framework"   embed="true" custom="true" /> 
    ....  
</platform>


<!-- Android Sample for your reference -->
<platform name="android">
    ....
    <source-file src="src/android/XXX.java"/>
    <framework src="src/android/build.gradle" custom="true" type="gradleReference" />
    <resource-file src="src/android/SDK/libs/XXX.aar" target="libs/XXX.aar" />
    ....  
</platform>
2
  • 2
    After i did what you say, the framework has been added both in "Embed binaries" and "Linked Frameworks & Libraries" sections. But in the Linked Frameworks & Libraries section, the framework seems to look like a disabled one. Even in the Framework group(under project navigator of xcode), the framework seems to be in red(not linked state). Kindly help me in fixing this issue. Thanks much.
    – GJDK
    May 14, 2018 at 12:01
  • @GJDK Have you tried taking " embed="true" " off in <framework> tag?
    – Joanne
    May 31, 2018 at 3:41
7

To get my plugin to build with a project on XCode 8.0 and cordova-ios 4.2, I had to run the hook in the after_build phase. Also, make sure that the node environment is using the latest verison of xcode-node (^0.8.9) or you will get bugs in the copy files phase.

<framework src="lib/myCustom.framework" custom="true" embed="true" /> <hook type="after_build" src="hooks/add_embedded.js" />

The plugin.xml needs custom="true" for Cordova to copy the framework file, which ended up conflicting with the changes made to the .pbxproj when this hook ran in after_platform add or even after_prepare.

4

embed="true" is supported as of cordova-ios 4.4.0 and cordova 7.0.0, which was released today. https://cordova.apache.org/docs/en/latest/plugin_ref/spec.html#framework https://issues.apache.org/jira/browse/CB-11233

3
  • Not accurate for custom iOS frameworks for embedded binaries section, see continued discussion of issues.apache.org/jira/browse/CB-11233 even on last month (April/2017). Notice it was labeled "cordova-7.0.0".
    – Alon Amir
    May 4, 2017 at 11:42
  • Heh right, I was thinking of adding, "but this has yet to work for me." ;-) May 4, 2017 at 20:23
  • Undeleted now that 7.0.0 is released. May 4, 2017 at 23:17
1

@Alon Amir, thanks for sharing, it works beautifully! Although, my app ran perfectly in Debug but not in Release mode. I figured out that the LD_RUNPATH_SEARCH_PATHS was only added to Debug mode as proj.getBuildProperty without a build parameter takes the first result. I modified your code a bit so that it works in Debug as well as in Release mode:

function addRunpathSearchBuildProperty(proj, build) {
   const LD_RUNPATH_SEARCH_PATHS =  proj.getBuildProperty("LD_RUNPATH_SEARCH_PATHS", build);
   if(!LD_RUNPATH_SEARCH_PATHS) {
      proj.addBuildProperty("LD_RUNPATH_SEARCH_PATHS", "\"$(inherited) @executable_path/Frameworks\"", build);
   } else if(LD_RUNPATH_SEARCH_PATHS.indexOf("@executable_path/Frameworks") == -1) {
      var newValue = LD_RUNPATH_SEARCH_PATHS.substr(0,LD_RUNPATH_SEARCH_PATHS.length-1);
      newValue += ' @executable_path/Frameworks\"';
      proj.updateBuildProperty("LD_RUNPATH_SEARCH_PATHS", newValue, build);
   }
}

myProj.parseSync();
addRunpathSearchBuildProperty(myProj, "Debug");
addRunpathSearchBuildProperty(myProj, "Release");
2
  • Thanks! glad to know it helped you. although if you use updateBuildProperty or addBuildProperty without the 3rd parameter (as in my hook), it should've been applied to all build settings, here's the condition from node-xcode lib (build is the 3rd parameter) if ( (build && config.name === build) || (!build) ). If your project fails to build in "Release", try make sure that either "bitcode" is supported by the framework you embed, or disable "bitcode" in your own cordova project (there's a plugin + hook for it).
    – Alon Amir
    May 20, 2016 at 19:02
  • The problem isn't in either updateBuildProperty or addBuildProperty, but with getBuildProperty: it first comes to the Debug entry, where LD_RUNPATH_SEARCHPATHS was already set for some reason, so "target" was set to that value. After that, it reaches the Release entry. As there's no LD_RUNPATH_SEARCHPATHS entry, "target" will not be changed! For consequense, addBuildProperty will not be called afterwards. I hope that I was able to clarify my case a bit (and did that correctly). The bitcode flag doesn't seem to be an issue though as it runs perfectly once I set the search path correctly.
    – Max Wahler
    May 22, 2016 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.