Xcode fails to build Kotlin Multiplatform Library with PhaseScriptExecution

Viewed 298

I have a Kotlin Multiplatform library in iOS which is installed as a Pod in my Xcode project. I am able to build in Xcode fine with a simulator target. I am also able to run Xcode > Product > Archive without issues. The issue is when I try to generate a Beta build.

When I run fastlane beta, I get:

Exit Code 65
** ARCHIVE FAILED **
[09:00:39]: ▸ The following build commands failed:
[09:00:39]: ▸   PhaseScriptExecution [CP-User]\ 
Build\ USER/Library/Developer/Xcode/DerivedData/MyApp-hcjgimlnfvcffcbpuwpfwifkgikb/Build/Intermediates.noindex/
ArchiveIntermediates/MyApp/IntermediateBuildFilesPath/Pods.build/Beta-iphoneos/mymultiplatformlibrary.build/
Script-8FCFFD85020DFCF30210C03ECFF91358.sh (in target 'mymultiplatformlibrary' from project 'Pods')

Podfile

 pod 'mymultiplatformlibrary', :path => '<shared library path>'

Xcode: 13.2

iOS min target: 12.0

1 Answers

After inspecting the fastlane log file in: /Users//Library/Logs/gym/MyApp-MyApp.log, I found the following:

More details are available by https://github.com/square/cocoapods-generate

Unable to detect Kotlin framework build type for CONFIGURATION=Beta automatically.
Specify 'KOTLIN_FRAMEWORK_BUILD_TYPE' to 'debug' or 'release'

* What went wrong:
A problem occurred configuring project ':mymultiplatformlibrary'.

> Could not identify build type for Kotlin framework 'MyMultiplatformLibrary' built via 
> cocoapods plugin with CONFIGURATION=Beta.
Add xcodeConfigurationToNativeBuildType["Beta"]=NativeBuildType.DEBUG
or xcodeConfigurationToNativeBuildType["Beta"]=NativeBuildType.RELEASE to cocoapods plugin configuration

I went into my Kotlin Multiplatform library and edited the build.gradle.kts to contain xcodeConfigurationToNativeBuildType.

// build.gradle.kts
cocoapods {
        frameworkName = "MyMultiplatformLibrary"
        podfile = project.file("../../MyApp/Podfile")
        ios.deploymentTarget = "12.0"
        xcodeConfigurationToNativeBuildType["Debug"] = NativeBuildType.DEBUG
        xcodeConfigurationToNativeBuildType["Beta"]= NativeBuildType.RELEASE
        xcodeConfigurationToNativeBuildType["Release"] = NativeBuildType.RELEASE
    }

What was happening was that fastlane beta was calling xcodebuild archive with the following arguments, and it was failing:

/Applications/Xcode_13_2.app/Contents/Developer/usr/bin/xcodebuild 
-workspace MyApp.xcworkspace 
-scheme MyApp 
-configuration Beta -destination generic/platform=iOS 
-archivePath "/Users/USER/Library/Developer/Xcode/Archives/2022-02-23/MyApp 2022-02-23 17.48.31.xcarchive” 
archive

In xcodebuild archive, when configuration was Beta, it failed, but when it was Release, it succeeded.

How it works is:

  1. Xcode tries to archive the 'mymultiplatformlibrary' target. Xcode looks at MyMultiplatformLibrary .podspec, runs the Kotlin Native Cocoapods plugin, tries to output the .framework file in /build/cocoapods/framework.
  2. In the .podspec you can see that the Kotlin Native cocoapods plugin runs with the same $CONFIGURATION variable specified by Xcode, in this case, "Beta". -Pkotlin.native.cocoapods.configuration=$CONFIGURATION
  3. When the Kotlin native cocoapods plugin was trying to generate the framework file, it couldn't find what native build type to use for "Beta", so it failed to generate the framework file. Thus Xcode archiving failed.
Related