How to wrap a binary xcframework Firebase in an SPM package and use it in a project?

huangapple go评论98阅读模式
英文:

How to wrap a binary xcframework Firebase in an SPM package and use it in a project?

问题

我正在尝试将Firebase作为一个二进制文件添加到我的项目中以减少构建时间,到目前为止,我已经尝试过创建自己的二进制文件并将其添加到项目中,这是成功的,但是,我遇到了一个像Firebase这样的大依赖的问题。

我从Firebase的发布页面下载了Firebase的二进制文件,然后创建了一个新的Swift包,将一些xcframeworks复制到artifacts文件夹中,然后将其添加到Package.swift中,以测试是否可以像这样使用FirebaseAnalytics:

let package = Package(
    name: "FirebaseWrapper",
    products: [
        .library(
            name: "FirebaseWrapper",
            targets: ["FirebaseWrapper"]),
        .library(
            name: "FirebaseAnalytics",
            targets: ["FirebaseAnalytics"]),
    ],
    dependencies: [
    ],
    targets: [
        .target(
            name: "FirebaseWrapper",
            dependencies: [.target(name: "FirebaseAnalytics")]),
        .binaryTarget(name: "FirebaseAnalytics", path: "artifacts/FirebaseAnalytics/FirebaseAnalytics.xcframework"),
        .binaryTarget(name: "FBLPromises", path: "artifacts/FirebaseAnalytics/FBLPromises.xcframework"),
        .binaryTarget(name: "FirebaseAnalyticsSwift", path: "artifacts/FirebaseAnalytics/FirebaseAnalyticsSwift.xcframework"),
        .binaryTarget(name: "FirebaseCore", path: "artifacts/FirebaseAnalytics/FirebaseCore.xcframework"),
        .binaryTarget(name: "FirebaseCoreInternal", path: "artifacts/FirebaseAnalytics/FirebaseCoreInternal.xcframework"),
        .binaryTarget(name: "FirebaseInstallations", path: "artifacts/FirebaseAnalytics/FirebaseInstallations.xcframework"),
        .binaryTarget(name: "GoogleAppMeasurement", path: "artifacts/FirebaseAnalytics/GoogleAppMeasurement.xcframework"),
        .binaryTarget(name: "GoogleAppMeasurementIdentitySupport", path: "artifacts/FirebaseAnalytics/GoogleAppMeasurementIdentitySupport.xcframework"),
        .binaryTarget(name: "GoogleUtilities", path: "artifacts/FirebaseAnalytics/GoogleUtilities.xcframework"),
        .binaryTarget(name: "nanopb", path: "artifacts/FirebaseAnalytics/nanopb.xcframework")
    ]
)

然后,我将FirebaseWrapper添加到“链接二进制文件和库”的阶段,然后尝试构建,但Xcode给了我一堆错误,例如

Undefined symbol: _APMAnalyticsConfiguration
Undefined symbol: _APMAppMeasurementOriginFirebase
...

当我展开详细日志时,我看到

ld: warning: Could not find or use auto-linked framework 'GoogleAppMeasurement'
Undefined symbols for architecture arm64:
  "_APMAnalyticsConfiguration", referenced from:
      +[FIRAnalytics startWithConfiguration:options:] in FirebaseAnalytics(FIRAnalytics.o)
  "_APMAppMeasurementOriginFirebase", referenced from:
      +[FIRAnalytics startWithConfiguration:options:] in FirebaseAnalytics(FIRAnalytics.o)
...
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

所以很明显,当我添加这样的复杂二进制文件时,我必须进行一些额外的步骤,或者我在某个地方出错了。

你可以在这里找到包含所有这些包的测试项目,以进行测试:github链接

我想请教如何修复这个问题,以及关于Xcode构建系统的工作原理,框架/库等如何安排的任何建议文章/视频/指南。到目前为止,我已经观看了这些WWDC会议:WWDC_1WWDC_2,并阅读了Apple的文档:文档_1文档_2文档_3,但它们只给了我一些浅显的了解正在发生的事情。

英文:

I'm trying to add Firebase as a binary to my project to reduce build time, so far I've tried making my own binary and adding it to the project and it was successful, however, I've run into a problem with a big dependency like Firebase.
I've downloaded Firebase binaries from their releases page, then created a new Swift Package, copied some of the xcframeworks into artifacts folder, and then added to Package.swift to test if I could use FirebaseAnalytics like this:

let package = Package(
    name: "FirebaseWrapper",
    products: [
        .library(
            name: "FirebaseWrapper",
            targets: ["FirebaseWrapper"]),
        .library(
            name: "FirebaseAnalytics",
            targets: ["FirebaseAnalytics"]),
    ],
    dependencies: [
    ],
    targets: [
        .target(
            name: "FirebaseWrapper",
            dependencies: [.target(name: "FirebaseAnalytics")]),
        .binaryTarget(name: "FirebaseAnalytics", path: "artifacts/FirebaseAnalytics/FirebaseAnalytics.xcframework"),
        .binaryTarget(name: "FBLPromises", path: "artifacts/FirebaseAnalytics/FBLPromises.xcframework"),
        .binaryTarget(name: "FirebaseAnalyticsSwift", path: "artifacts/FirebaseAnalytics/FirebaseAnalyticsSwift.xcframework"),
        .binaryTarget(name: "FirebaseCore", path: "artifacts/FirebaseAnalytics/FirebaseCore.xcframework"),
        .binaryTarget(name: "FirebaseCoreInternal", path: "artifacts/FirebaseAnalytics/FirebaseCoreInternal.xcframework"),
        .binaryTarget(name: "FirebaseInstallations", path: "artifacts/FirebaseAnalytics/FirebaseInstallations.xcframework"),
        .binaryTarget(name: "GoogleAppMeasurement", path: "artifacts/FirebaseAnalytics/GoogleAppMeasurement.xcframework"),
        .binaryTarget(name: "GoogleAppMeasurementIdentitySupport", path: "artifacts/FirebaseAnalytics/GoogleAppMeasurementIdentitySupport.xcframework"),
        .binaryTarget(name: "GoogleUtilities", path: "artifacts/FirebaseAnalytics/GoogleUtilities.xcframework"),
        .binaryTarget(name: "nanopb", path: "artifacts/FirebaseAnalytics/nanopb.xcframework")
    ]
)

Then I've added this FirebaseWrapper to "Link Binary With Libraries" stage as usual and tried to build, but Xcode gave me a bunch of errors like

Undefined symbol: _APMAnalyticsConfiguration
Undefined symbol: _APMAppMeasurementOriginFirebase
...

When I expand the detailed log, I see

ld: warning: Could not find or use auto-linked framework 'GoogleAppMeasurement'
Undefined symbols for architecture arm64:
  "_APMAnalyticsConfiguration", referenced from:
      +[FIRAnalytics startWithConfiguration:options:] in FirebaseAnalytics(FIRAnalytics.o)
  "_APMAppMeasurementOriginFirebase", referenced from:
      +[FIRAnalytics startWithConfiguration:options:] in FirebaseAnalytics(FIRAnalytics.o)
...
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

So it's obvious that I have to do some additional steps when I add some complex binary like this, or I messed up somewhere.

You can find the test project with all these packages to play with here: github link.

I'd like to ask for advice on how can I fix that, as well as any recommendations for articles/videos/guides on how xcode build system works, how are frameworks/libraries/etc arranged, and so on. So far I've watched these WWDC sessions: WWDC_1, WWDC_2, and read the docs from Apple: doc_1, doc_2, doc_3, but they gave me only somewhat shallow understanding of what's going on.

答案1

得分: 0

抱歉,我只能为您提供英文翻译,以下是要翻译的内容:

"It was my bad, as it turned out I just had to add all the dependencies of FirebaseAnalytics to the dependencies of the main target of my FirebaseWrapper:

Before:

targets: [
.target(
name: "FirebaseWrapper",
dependencies: [
.target(name: "FirebaseAnalytics")
]),
...


After:

targets: [
.target(
name: "FirebaseWrapper",
dependencies: [
.target(name: "FirebaseAnalytics"),
.target(name: "FBLPromises"),
.target(name: "FirebaseAnalyticsSwift"),
.target(name: "FirebaseCore"),
.target(name: "FirebaseCoreInternal"),
.target(name: "FirebaseInstallations"),
.target(name: "GoogleAppMeasurement"),
.target(name: "GoogleAppMeasurementIdentitySupport"),
.target(name: "GoogleUtilities"),
.target(name: "nanopb"),
]),
...
"
Now it builds and runs successfully.

英文:

It was my bad, as it turned out I just had to add all the dependencies of FirebaseAnalytics to the dependencies of the main target of my FirebaseWrapper:

Before:

targets: [
    .target(
        name: "FirebaseWrapper",
        dependencies: [
            .target(name: "FirebaseAnalytics")
        ]),
...
_____________________________________________________________

After:

targets: [
    .target(
        name: "FirebaseWrapper",
        dependencies: [
            .target(name: "FirebaseAnalytics"),
            .target(name: "FBLPromises"),
            .target(name: "FirebaseAnalyticsSwift"),
            .target(name: "FirebaseCore"),
            .target(name: "FirebaseCoreInternal"),
            .target(name: "FirebaseInstallations"),
            .target(name: "GoogleAppMeasurement"),
            .target(name: "GoogleAppMeasurementIdentitySupport"),
            .target(name: "GoogleUtilities"),
            .target(name: "nanopb"),
        ]),
...

Now it builds and runs successfully.

huangapple
  • 本文由 发表于 2023年8月4日 00:50:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定