Vario
Vario

Reputation: 530

Kotlin Multiplatform library for iOS with bitcode

we use Kotlin to share a library between Android and iOS.

We set up everything, but on iOS i need Bitcode enabled. After a research I found solution:

kotlin {
targets {
    fromPreset(presets.jvm, 'jvm') {
        mavenPublication {
            artifactId = 'my-lib-name'
        }
    }
    // Switch here to presets.iosArm64 to build library for iPhone device || iosX64 for emulator
    fromPreset(presets.iosArm64, 'iOS') {
        compilations.main.outputKinds('FRAMEWORK')
        compilations.main.extraOpts '-Xembed-bitcode' // for release binaries
        compilations.main.extraOpts '-Xembed-bitcode-marker'//  for debug binaries
    }
  }
}

But the question is now, do I have and if yes, how do I separate between release and debug binaries and the specific flags? Can i simply add both flags without any drawbacks?

Maybe somebody can enlighten me thanks

Upvotes: 11

Views: 2262

Answers (3)

Niklas
Niklas

Reputation: 25453

I only want to use Bitcode when it's actually needed. Xcode passes down an environment variable ENABLE_BITCODE which we can use to conditionally embed bitcode.

kotlin {
  iosArm64("ios") {
    binaries {
      framework {
        embedBitcode("YES".equals(System.getenv("ENABLE_BITCODE")) ? "bitcode" : "marker")
      }
    }
  }
}

Upvotes: 0

Ilya Matveev
Ilya Matveev

Reputation: 191

Since Kotlin 1.3.20 bitcode embedding for iOS frameworks works out of the box. You also can configure embedding manually if you need:

kotlin {
    iosArm64("ios") {
        binaries {
            framework {
                // The following embedding modes are available:
                //   - "marker"  - Embed placeholder LLVM IR data as a marker.
                //                 Has the same effect as '-Xembed-bitcode-marker.'
                //   - "bitcode" - Embed LLVM IR bitcode as data.
                //                 Has the same effect as the '-Xembed-bitcode'.
                //   - "disable" - Don't embed LLVM IR bitcode.
                embedBitcode("marker")
            }
        }
    }
}

Upvotes: 10

hotkey
hotkey

Reputation: 148169

Currently, all of the binary link tasks for the same iOS target share the compiler and linker options, so there's no way to setup the options for them separately. Please follow KT-26887 for updates.

If you can afford running several builds with different options, you can set the options conditionally and run the build with a flag:

compilations.main.outputKinds('FRAMEWORK')

if (project.findProperty("releaseFramework") == "true")
    compilations.main.extraOpts '-Xembed-bitcode' // for release binaries
else
    compilations.main.extraOpts '-Xembed-bitcode-marker'//  for debug binaries

Then run the build with or without the flag, respectively:

./gradlew linkDebugFrameworkIOS

and

./gradlew linkReleaseFrameworkIOS -PreleaseFramework=true

Upvotes: 5

Related Questions