简体   繁体   English

如何手动添加 a.xcframework 到 Flutter iOS 插件?

[英]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 文件中找到执行此操作的方法),所以我做了什么:

  1. I've added the plugin to the Example project with Swift Package Manager我已经使用 Swift Package 管理器将插件添加到示例项目中
  2. Manually copied MyDependency.xcframework folder to MyPlugin/ios folder手动将MyDependency.xcframework文件夹复制到MyPlugin/ios文件夹
  3. Referenced it in podspec file, like this:在 podspec 文件中引用它,如下所示:
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.podspecxxx.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所在的位置)


Add 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) (如果没有,请添加)

添加框架 1

  1. Navigate to Pods scheme and right-click on Frameworks导航到Pods方案并右键单击Frameworks
  2. Click Add Files to "Pods"...单击Add Files to "Pods"...

在此处输入图片说明

  1. Navigate to the root of the ios directory where the xxxxxx.xcframework is located导航到xxxxxx.xcframework所在的ios目录根目录
  2. Uncheck Copy items if needed Copy items if needed取消选中Copy items if needed
  3. Select Create folder references选择Create folder references
  4. Check Pods-Runner检查Pods-Runner
  5. Check your plugin ie.检查你的插件,即。 xxx
  6. Click Add点击Add

Embed xxxxxx.xcframework into your projectxxxxxx.xcframework嵌入到您的项目中

(this should allow you to run/publish to real devices) (这应该允许您运行/发布到真实设备)

在此处输入图片说明

  1. Click on the Pods scheme点击Pods方案
  2. In TARGETS select Pods-RunnerTARGETS选择Pods-Runner
  3. Click on the General tab单击General选项卡
  4. Ensure the xxxxx.xcframework is present underneath Frameworks and Libraries (if not, add it by clicking on the + )确保xxxxx.xcframework存在于Frameworks and Libraries下面(如果没有,请点击+ 添加
  5. Change Embed to Embed & Sign Embed Embed & Sign更改为Embed & Sign

在此处输入图片说明

  1. Click on your plugin ie.单击您的插件即。 xxx
  2. Ensure the xxxxx.xcframework is present underneath Frameworks and Libraries确保xxxxx.xcframework存在于Frameworks and Libraries下面
  3. Change Embed to Embed & Sign Embed Embed & Sign更改为Embed & Sign

在此处输入图片说明

  1. Again, for both targets below Pods scheme, the next steps should already be fine, but just check it同样,对于Pods方案下面的两个目标,接下来的步骤应该已经没问题了,但只需检查一下
  2. Click Build Phases单击Build Phases
  3. Click Link Binary With Libraries单击Link Binary With Libraries
  4. Ensure 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.xcframeworkxxx.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM