[英]How to manually add a .xcframework to a Flutter iOS Plugin?
I'm trying to create a Flutter Plugin to use a native library.我正在尝试创建一个 Flutter 插件来使用本机库。 This library I'm trying to use is stored in a private repository and can be used with Swift Dependency Manager.
我尝试使用的这个库存储在一个私有存储库中,可以与 Swift Dependency Manager 一起使用。
This is causing me a headache, cause I can't add a private repository dependency in my plugin (I couldn't find a way to do this in.podspec file), so what I've done:这让我很头疼,因为我无法在我的插件中添加私有存储库依赖项(我无法在 .podspec 文件中找到执行此操作的方法),所以我做了什么:
MyDependency.xcframework
folder to MyPlugin/ios
folderMyDependency.xcframework
文件夹复制到MyPlugin/ios
文件夹s.preserve_paths = 'MyDependency.xcframework'
s.xcconfig = { 'OTHER_LDFLAGS' => '-framework MyDependency' }
s.vendored_frameworks = 'MyDependency.xcframework'
Doing this I'm able to use MyDependency inside plugin's sources.这样做我可以在插件的源代码中使用 MyDependency。
My current problem is: This is only working in Simulator .我当前的问题是:这仅适用于 Simulator 。
Before doing this, the project was running without any problem in real devices.在这样做之前,该项目在真实设备上运行没有任何问题。
This is the error message I'm receiving every time I tried to run in a real device:这是我每次尝试在真实设备上运行时收到的错误消息:
Also, I've made a test using the dependency directly from Swift Dependency Manager and is working fine.此外,我已经使用直接来自 Swift 依赖管理器的依赖项进行了测试,并且工作正常。 I think the problem is the way I'm adding the framework to my plugin.
我认为问题在于我将框架添加到我的插件的方式。
After doing some research, I've found some links giving me an ideia about the real problem...在做了一些研究之后,我发现了一些链接让我对真正的问题有了一个想法......
To solve this, I've added a simple script to my main project's build process.为了解决这个问题,我在主项目的构建过程中添加了一个简单的脚本。
This script adds the code signing to inner .framework
files.此脚本将代码签名添加到内部
.framework
文件中。
cd "${CODESIGNING_FOLDER_PATH}/Frameworks/"
# flatten nested frameworks by copying to APP.app/Frameworks
for framework in *; do
if [ -d "$framework" ]; then
if [ -d "${framework}/Frameworks" ]; then
echo "Moving embedded frameworks from ${framework} to ${PRODUCT_NAME}.app/Frameworks"
cp -R "${framework}/Frameworks/" .
rm -rf "${framework}/Frameworks"
fi
fi
done
# remove any leftover nested frameworks (i.e. 'PackageName_359AFEED79E48935_PackageProduct.framework')
for framework in *; do
if [ -d "$framework" ]; then
if [ -d "${framework}/Frameworks" ]; then
echo "Removing embedded frameworks from ${framework} to ${PRODUCT_NAME}.app/Frameworks"
rm -rf "${framework}/Frameworks"
fi
fi
done
# codesign for Debugging on device
if [ "${CONFIGURATION}" == "Debug" ] & [ "${SDKROOT}" != *Simulator* ] ; then
echo "Code signing frameworks..."
find "${CODESIGNING_FOLDER_PATH}/Frameworks" -maxdepth 1 -name '*.framework' -print0 | while read -d $'\0' framework
do
# only sign frameworks without a signature
if ! codesign -v "${framework}"; then
codesign --force --sign "${EXPANDED_CODE_SIGN_IDENTITY}" --preserve-metadata=identifier,entitlements --timestamp=none "${framework}"
echo "Added missing signature to '${framework}'"
fi
done
fi
I got mine working by adding the following lines to the xxx.podspec
:我通过将以下几行添加到
xxx.podspec
来xxx.podspec
:
s.preserve_paths = 'xxxxxx.xcframework/**/*'
s.xcconfig = { 'OTHER_LDFLAGS' => '-framework xxxxxx' }
s.vendored_frameworks = 'xxxxxx.xcframework'
Notice the /**/*
at the end of s.preserve_paths
注意
s.preserve_paths
末尾的/**/*
Here is the full xxx.podspec
file:这是完整的
xxx.podspec
文件:
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint xxx.podspec` to validate before publishing.
#
Pod::Spec.new do |s|
s.name = 'xxx'
s.version = '1.0.0'
s.summary = 'xxx'
s.description = <<-DESC
xxx is a flutter plugin
DESC
s.homepage = 'https://example.com'
s.license = { :file => '../LICENSE', :type => 'BSD' }
s.author = { 'xxx' => 'email@example.com' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'Flutter'
s.platform = :ios, '10.0'
# Flutter.framework does not contain a i386 slice.
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' }
s.swift_version = '5.0'
s.preserve_paths = 'xxxxxx.xcframework/**/*'
s.xcconfig = { 'OTHER_LDFLAGS' => '-framework xxxxxx' }
s.vendored_frameworks = 'xxxxxx.xcframework'
end
Make sure to copy the xxxxxx.xcframework
directory to .../xxx/ios/xxxxxx.xcframework
(where your plugin xxx.podspec
is located)确保将
xxxxxx.xcframework
目录复制到.../xxx/ios/xxxxxx.xcframework
(您的插件xxx.podspec
所在的位置)
xxxxxx.xcframework
to Pods
scheme to be able to import itxxxxxx.xcframework
添加到Pods
方案以便能够导入它The Frameworks
directory should be below the Pods
scheme when running the flutter command to create a plugin ie.运行 flutter 命令创建插件 ie 时,
Frameworks
目录应该在Pods
方案下面。
flutter create --org com.xxx --template=plugin --platforms=android,ios -a java -i swift xxx
Source: Developing packages & plugins来源: 开发包和插件
(If not, add it) (如果没有,请添加)
Pods
scheme and right-click on Frameworks
Pods
方案并右键单击Frameworks
Add Files to "Pods"...
Add Files to "Pods"...
ios
directory where the xxxxxx.xcframework
is locatedxxxxxx.xcframework
所在的ios
目录根目录Copy items if needed
Copy items if needed
取消选中Copy items if needed
Create folder references
Create folder references
Pods-Runner
Pods-Runner
xxx
Add
Add
xxxxxx.xcframework
into your projectxxxxxx.xcframework
嵌入到您的项目中(this should allow you to run/publish to real devices) (这应该允许您运行/发布到真实设备)
Pods
schemePods
方案TARGETS
select Pods-Runner
TARGETS
选择Pods-Runner
General
tabGeneral
选项卡xxxxx.xcframework
is present underneath Frameworks and Libraries
(if not, add it by clicking on the + )xxxxx.xcframework
存在于Frameworks and Libraries
下面(如果没有,请点击+ 添加)Embed
to Embed & Sign
Embed
Embed & Sign
更改为Embed & Sign
xxx
xxxxx.xcframework
is present underneath Frameworks and Libraries
xxxxx.xcframework
存在于Frameworks and Libraries
下面Embed
to Embed & Sign
Embed
Embed & Sign
更改为Embed & Sign
Pods
scheme, the next steps should already be fine, but just check itPods
方案下面的两个目标,接下来的步骤应该已经没问题了,但只需检查一下Build Phases
Build Phases
Link Binary With Libraries
Link Binary With Libraries
xxxxx.xcframework
is present, if not add it by clicking the + and that the Status
is set to Required
xxxxx.xcframework
存在,如果没有通过单击+添加它并且Status
设置为Required
Now you should be able to import the framework and reference it in your code and should also publish perfectly fine.现在您应该能够导入框架并在您的代码中引用它,并且也应该可以完美地发布。
Hope this helps, and good luck.希望这会有所帮助,祝你好运。 I hope this doesn't change again soon
我希望这不会很快再次改变
fingers crossed手指交叉
If non of the above fixes work and you still get errors like "framework xxx not found", verify if the xxx.xcframework
and xxx.framework
folders inside have the same name.如果上述修复均无效,并且您仍然收到“找不到框架 xxx”之类的错误,请验证其中的
xxx.xcframework
和xxx.framework
文件夹是否同名。
I spent a few days failing to add .xcframework
to Flutter plugin, and it turned out there was a mismatch in naming.我花了几天时间未能将
.xcframework
添加到 Flutter 插件,结果发现命名不匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.