Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwiftUI Previews Not Working When Using Analytics via SPM #6374

Closed
StarLard opened this issue Aug 31, 2020 · 13 comments
Closed

SwiftUI Previews Not Working When Using Analytics via SPM #6374

StarLard opened this issue Aug 31, 2020 · 13 comments
Labels
api: analytics beta-software Related to using beta iOS or Xcode versions. Swift Package Manager SwiftUI Issues directly related to Apple's SwiftUI framework

Comments

@StarLard
Copy link

StarLard commented Aug 31, 2020

Step 0: Are you in the right place?

  • For issues or feature requests related to the code in this repository
    file a Github issue.
    • If this is a feature request please use the Feature Request template.
  • For general technical questions, post a question on StackOverflow
    with the firebase tag.
  • For general (non-iOS) Firebase discussion, use the firebase-talk
    google group.
  • For backend issues, console issues, and other non-SDK help that does not fall under one
    of the above categories, reach out to
    Firebase Support.
  • Once you've read this section and determined that your issue is appropriate for
    this repository, please delete this section.

[REQUIRED] Step 1: Describe your environment

  • Xcode version: Xcode 12 Beta 6
  • Firebase SDK version: 6.31-spm-beta
  • Firebase Component: Analytics
  • Component version: 6.31-spm-beta
  • Installation method: Swift Package Manager

[REQUIRED] Step 2: Describe the problem

Steps to reproduce:

What happened? How can we make the problem occur?

  1. Install Firebase Analytics via SPM through a framework which lists it as a dependency.
  2. Add -ObjC flag as per instructions.
  3. Attempt to view a SwiftUI view in preview.
  4. Preview fails to load with error:
Undefined symbols for architecture arm64:
  "_OBJC_CLASS_$_FIRAnalytics", referenced from:
      objc-class-ref in <My framework which depends on Analytics>.o
     (maybe you meant: _OBJC_CLASS_$_FIRAnalyticsConfiguration)
ld: symbol(s) not found for architecture arm64

Building app succeeds, but attempting to run the app will produce the following error:

Could not install at this time.
Failed to load Info.plist from bundle at path /Users/<username>/Library/Developer/CoreSimulator/Devices/B2616B0F-8BB0-4FBC-9A56-D3BD8EEFA502/data/Library/Caches/com.apple.mobile.installd.staging/temp.KrgZu7/extracted/<app name>.app/Frameworks/GoogleAppMeasurement.framework; Extra info about Info.plist: Couldn't stat /Users/<username>/Library/Developer/CoreSimulator/Devices/B2616B0F-8BB0-4FBC-9A56-D3BD8EEFA502/data/Library/Caches/com.apple.mobile.installd.staging/temp.KrgZu7/extracted/<app name>.app/Frameworks/GoogleAppMeasurement.framework/Info.plist: No such file or directory

Adding arm64 to the 'Excluded Architectures' setting as suggested here
will change the previews error to the error produced while attempting to run.

If you have a downloadable sample project that reproduces the bug you're reporting, you will
likely receive a faster response on your issue.

Relevant Code:

Swift package declaration:

let package = Package(
    name: "SomePackage",
    platforms: [ .iOS(.v14), .macOS(.v11), .tvOS(.v14) ],
    products: [
        .library(
            name: "SomePackage",
            targets: ["SomePackage"]),
    ],
    dependencies: [
        .package(name: "Firebase", url: "https://github.com/firebase/firebase-ios-sdk.git", .branch("6.31-spm-beta"))
    ],
    targets: [
        .target(
            name: "SomePackage",
            dependencies: [
                .product(name: "FirebaseAnalytics", package: "Firebase", condition: .when(platforms: [.iOS])),
                .product(name: "FirebaseAuth", package: "Firebase"),
                .product(name: "FirebaseCore", package: "Firebase"),
                .product(name: "FirebaseFirestore", package: "Firebase"),
                .product(name: "FirebaseFirestoreSwift", package: "Firebase"),
            ],
        )
        .testTarget(
            name: "SomePackageTests",
            dependencies: ["SomePackage"]
        ),
    ]
)
@google-oss-bot
Copy link

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@StarLard
Copy link
Author

Possibly related issue? #6360 (comment)

@StarLard
Copy link
Author

Tried removing FirebaseAnalytics dependency from my package, but still getting the 'GoogleAppMeasurement can't find plist' error when loading preview even after removing analytics.

@paulb777
Copy link
Member

@StarLard Thanks for the report. This looks very similar to #6360 which we haven't been able to reproduce. Are you able to share a reproducible example?

For the most recent comment, it seems that the Xcode project has lingering references to GoogleAppMeasurement. Can you search and remove any of them?

@paulb777 paulb777 added the beta-software Related to using beta iOS or Xcode versions. label Aug 31, 2020
@ryanwilson ryanwilson added the SwiftUI Issues directly related to Apple's SwiftUI framework label Aug 31, 2020
@ryanwilson
Copy link
Member

Thanks for the report, I was able to reproduce this one! It works in the simulator but previews are broken, meaning it's different from #6360. Will try with the other package managers as well, but for now here's what I did:

  1. Create a blank SwiftUI only iOS project using Xcode 12 b6.
  2. Add FirebaseAnalyitcs as a SwiftPM dependency.
  3. In ContentView.swift, add import Firebase and replace the contents of body with:
var body: some View {
    Text("Hello, world!")
      .padding()
      // Added this modifier.
      .onAppear {
        Analytics.logEvent("test-event-custom", parameters: nil)
      }
  }
  1. Try to regenerate the preview. Build fails:
Undefined symbols for architecture arm64:
  "_OBJC_CLASS_$_FIRAnalytics", referenced from:
      objc-class-ref in ContentView.o

Note: there are multiple warnings above and below that gives us a clue at what's going on, it looks like SwiftPM is looking for the arm64 architecture inside the generated xcframework for the simulator instead of the expected x86_64 slice. At the beginning of the build logs:

'<ObfuscatedLocalProjectPath>/SourcePackages/artifacts/Firebase/FirebaseAnalytics.xcframework' is missing architecture(s) required by this target (arm64), but may still be link-compatible.

Then at the error:

ld: warning: ignoring file <ObfuscatedLocalProjectPath>/Build/Intermediates.noindex/Previews/TestingSwiftPM/Products/Debug-iphonesimulator/FirebaseAnalytics.framework/FirebaseAnalytics, missing required architecture arm64 in file <ObfuscatedLocalProjectPath>/Build/Intermediates.noindex/Previews/TestingSwiftPM/Products/Debug-iphonesimulator/FirebaseAnalytics.framework/FirebaseAnalytics (2 slices)

Looking at the .xcframework contents in the screenshot and Info.plist below, it's looking like the simulator is trying to run an arm64 slice instead of an x86_64 slice for some reason. Perhaps this has to do with changes related to Apple Silicon, or maybe it's just a bug. We'll have to dig into this further.

Info.plist contents:

<key>AvailableLibraries</key>
	<array>
		<dict>
			<key>LibraryIdentifier</key>
			<string>ios-i386_x86_64-simulator</string>
			<key>LibraryPath</key>
			<string>FirebaseAnalytics.framework</string>
			<key>SupportedArchitectures</key>
			<array>
				<string>i386</string>
				<string>x86_64</string>
			</array>
			<key>SupportedPlatform</key>
			<string>ios</string>
			<key>SupportedPlatformVariant</key>
			<string>simulator</string>
		</dict>
		<dict>
			<key>LibraryIdentifier</key>
			<string>ios-arm64_armv7</string>
			<key>LibraryPath</key>
			<string>FirebaseAnalytics.framework</string>
			<key>SupportedArchitectures</key>
			<array>
				<string>arm64</string>
				<string>armv7</string>
			</array>
			<key>SupportedPlatform</key>
			<string>ios</string>
		</dict>
	</array>

Contents of XCFramework directory

@ryanwilson
Copy link
Member

As noted in #6375 this happens in Zip file as well, so it must be related to the framework itself and how Xcode tries to run it.

@paulb777
Copy link
Member

A workaround is adding arm64 to the Excluded Architectures Build Setting. Thanks to https://developer.apple.com/forums/thread/656509?answerId=627644022#627644022.

Screen Shot 2020-08-31 at 9 10 22 AM

@StarLard
Copy link
Author

@paulb777 To be clear, adding arm64 is a workaround for the _OBJC_CLASS_$_FIRAnalytics error right? This workaround does not resolve the GoogleAppMeasurement issue for me.

@StarLard
Copy link
Author

it seems that the Xcode project has lingering references to GoogleAppMeasurement. Can you search and remove any of them?

Yup, this was the case. I had to completely remove and reinstall Firebase from my project to get it to remove. Clearing package caches did not work.

@kylebrowning
Copy link

Adding arm64 to excluded did not work for me. I still receive
image

@paulb777
Copy link
Member

@StarLard @kylebrowning I believe the issue you're discussing is the same as #6360 and should now be resolved with an update. See that issue for details.

I'll close this issue now since the missing symbol issue has a workaround and seems to be a reported Xcode issue.

If you're seeing otherwise, please comment or open a new issue and we'll continue the investigation.

@StarLard
Copy link
Author

StarLard commented Sep 13, 2020

Hi @paulb777 I've updated to the latest version of the 6.32-spm-beta as per recommendation in #6360 and am still experiencing this issue.

Also while excluding arm64 in my app target is a workaround there, this option is not available within the Swift Package itself so SwiftUI previews are still unavailable there. There's also no way to pass the -ObjC (as far as I'm aware) within a Swift Package outside of passing unsafe flags, but doing so will bar your package from distribution.

@StarLard
Copy link
Author

For anyone else experiencing this issue: A workaround to get previews working is to declare a separate package library target that depends on your main target and only import FirebaseAnalytics there; similar the the Firebase package structure itself.

@firebase firebase locked and limited conversation to collaborators Oct 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api: analytics beta-software Related to using beta iOS or Xcode versions. Swift Package Manager SwiftUI Issues directly related to Apple's SwiftUI framework
Projects
None yet
Development

No branches or pull requests

5 participants