[Fixed] Build failed in KMM project in IOS Emulator due to Xcode Run Script phase without specified outputs – Android

by
Ali Hasan
android gradle-kotlin-dsl kotlin-multiplatform

Quick Fix: In the parent module’s build.gradle.kts, set the kotlin and plugin versions to 1.9.10 and 8.1.0 respectively. Similarly, update the kotlin compiler extension version in the androidApp’s build.gradle.kts to 1.5.3.

The Problem:

In a Kotlin Multiplatform Mobile project, iOS build fails in Xcode due to a warning about a Run Script build phase without specified outputs. This warning is followed by a build failure, leading to an unsuccessful build process. The error appears when attempting to run the iosApp target within the iosApp project. Guidance is sought to address this issue and enable successful iOS builds.

The Solutions:

Solution 1: Update Kotlin and Compose Options Versions

  1. Open the build.gradle.kts file of the parent module.
  2. Update the Kotlin version for all submodules:
    • Add id("com.android.application").version("8.1.0").apply(false)
    • Add id("com.android.library").version("8.1.0").apply(false)
    • Add kotlin("android").version("1.9.10").apply(false)
    • Add kotlin("multiplatform").version("1.9.10").apply(false)
  3. Update the Kotlin compiler extension version in the androidApp module’s build.gradle.kts file:
    • Under composeOptions, set kotlinCompilerExtensionVersion to "1.5.3".
  4. Refer to the provided links:

This solution addresses the warning about the Run Script build phase and fixes the build failure in the iOS emulator. It ensures compatibility between the Kotlin and Compose Options versions used in the project.

Solution 2: Workaround for Xcode Version 15

To resolve the build failure caused by the missing output specification in the Xcode Run Script phase, you can add a temporary fix to your Kotlin Multiplatform Mobile project. This workaround is specifically applicable when you encounter the issue while building the iOS portion of your KMM project in Android Studio and involves modifying the build.gradle.kts file in the “shared module.”

  1. Open the build.gradle.kts file located in the “shared module” of your project.
  2. Find the section where you define the iOS targets using the iosX64(), iosArm64(), and iosSimulatorArm64() functions.
  3. Within the forEach loop that iterates through these targets, add the following code snippet:
  4. ```kotlin
    if (System.getenv("XCODE_VERSION_MAJOR") == "1500") {
        linkerOpts += "-ld64"
    }
    ```
    
  5. This code checks if you are using Xcode version 1500 (which is associated with Xcode 14) and adds the “-ld64” linker option if that’s the case. This option is necessary for building the iOS framework correctly.
  6. Save and apply the changes to your build.gradle.kts file.

By applying this workaround, you are temporarily resolving the issue until it is officially fixed in a future Kotlin version. Once Kotlin 1.9.10 or higher becomes available and you are able to update your Kotlin version, this workaround will no longer be necessary.

Solution 3: Utilize the Correct Java Development Kit (JDK) Version

The issue could be caused by an outdated Java Development Kit (JDK) installed on Android Studio. Follow these steps to address this:

  1. Open Android Studio.
  2. Navigate to Settings > Build, Execution, Deployment > Build Tools > Gradle.
  3. Under Gradle JDK, check the selected JDK version.
  4. If an older JDK version is selected, click the dropdown arrow and choose openjdk-21 or a higher version.
  5. Click Apply and then OK to save the changes.

By following these steps, you can ensure that you have the correct JDK version installed and configured in Android Studio, which should resolve the build failure due to the Run Script phase without specified outputs.

Q&A

What could be a possible solution to the build failure related to a script build phase in KMM project on iOS?

Update the Kotlin version in both ‘parent module’ and ‘androidApp’ build.gradle.kts files.

How to resolve the build issue in iOS Simulator when building a KMM project?

Add a specific linker option to the shared module’s build.gradle.kts file.

What could be causing build errors related to an outdated JDK in Android Studio?

Check the ‘Build Tools -> Gradle’ settings to ensure you have an up-to-date version of JDK installed.

Video Explanation:

The following video, titled "XCode Error PhaseScriptExecution failed with a nonzero exit code ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

XCode Error PhaseScriptExecution failed with a nonzero exit code when you update from 14.2 to 14.3. 6.9K views · 9 months ago ...more ...