Alo*_*mir 13 xcode ios cordova cordova-plugins
在自定义Cordova插件中,如何在plugin.xml中配置特定的.framework文件,以便将其添加到Xcode的"嵌入式二进制文件"部分?如果目前不能直接在plugin.xml中使用,我可以接受其他建议.
Alo*_*mir 31
我已经实现了一个解决方法,直到它被Cordova的plugin.xml支持,希望将来,一旦embed这些条目中的属性具有相同的效果:<framework embed="true" src="..." />,目前,此属性没有帮助,因此以下解决方法.
以下解决方案使用Cordova 5.3.3版.
<framework src="pointToYour/File.framework" embed="true" />
Run Code Online (Sandbox Code Playgroud)
embed="true" 暂时不起作用,但无论如何都要添加它.
<hook type="after_platform_add" src="hooks/embedframework/addEmbedded.js" />
Run Code Online (Sandbox Code Playgroud)
接下来,我们在钩子代码中需要一个特定的节点模块,该模块是node-xcode.
npm i xcode
Run Code Online (Sandbox Code Playgroud)
最后,钩子本身的代码 -
'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);
};
Run Code Online (Sandbox Code Playgroud)
LD_RUNPATH_SEARCH_PATHS寻找嵌入式框架"@executable_path/Frameworks"(这是嵌入式框架将被复制到"复制文件" - >"框架"构建阶段之后根据Max Whaler的建议修改了钩子脚本,因为我遇到了与Xcode 8相同的问题.
将应用程序上传到AppStore后,如果由于不支持的体系结构(i386等等)验证失败,请尝试以下Cordova插件(仅限钩子,无本机代码):zcordova-plugin-archtrim
小智 7
为了让我的插件在XCode 8.0和cordova-ios 4.2上用一个项目构建,我必须在这个after_build阶段运行钩子.此外,请确保节点环境使用xcode-node的最新版本(^ 0.8.9),否则您将在复制文件阶段遇到错误.
<framework src="lib/myCustom.framework" custom="true" embed="true" />
<hook type="after_build" src="hooks/add_embedded.js" />
plugin.xml需要custom="true"Cordova复制框架文件,当这个钩子在after_platform add或者after_prepare中运行时,最终与.pbxproj所做的更改相冲突.
要将库添加到Xcode中的"嵌入式二进制文件"部分(从cordova-ios 4.4.0和cordova 7.0.0开始),请将其放在plugin.xml中:
<framework src="src/ios/XXX.framework" embed="true" custom="true" />
Run Code Online (Sandbox Code Playgroud)
要将库添加到Xcode中的"Linked Frameworks and Libraries"部分,请将其放在plugin.xml中:
<source-file src="src/ios/XXX.framework" target-dir="lib" framework="true" />
Run Code Online (Sandbox Code Playgroud)
它们都可以同时存在.例如:
<!-- 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9976 次 |
| 最近记录: |