https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
u

Umar Ata

10/21/2020, 6:22 PM
Hi there, My name is Umar, I am from India Nice meeting you to all worlds best developers I have a question I actually transformed one of my android application logic code to kotlin multiplatform Problem: When the iOS developer needs the logic code I need to execute the command
*cd* "$SRCROOT/.."
./gradlew :shared:packForSimulator -PXCODE_CONFIGURATION=${CONFIGURATION}
shared is the name of my multiplatform module I am sharing the common code between Android and iOS so the problem is that whenever I run above command from runScript of XCode it actually builds the framework for Selected device like either iPhone device or iPhone Simulator and it never gives me one framework that runs on both iOS device and simulator
u

Umar Ata

10/21/2020, 6:44 PM
Thank you, I want to know that if I use Fat framework so can I use same in Android?
k

Kris Wong

10/21/2020, 6:44 PM
i don't understand the question. Android doesn't use frameworks.
u

Umar Ata

10/21/2020, 6:47 PM
I have one android app, I want the same logic in both iOS and Android, so I copied my logic code in newly created shared multiplatform module and in Android I am using it as a project dependency like
Copy code
implementation project(':shared')
and for iOS I use script to generate xcode-frameworks
and then use it in there
I don’t want to code separate framework for iOS, I only want to make framework that runs for both iOS device and simulator
for now I created two functions in gradle each for iOS device and simulator
Copy code
val packForXCode by tasks.creating(Sync::class) {
    group = "build"
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    //val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
    val targetName = "iosArm64"//+  if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
    val framework = kotlin.targets.getByName<KotlinNativeTarget>(targetName).binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)
    val targetDir = File(buildDir, "xcode-frameworks")
    from({ framework.outputDirectory })
    into(targetDir)
}
val packForSimulator by tasks.creating(Sync::class) {
    group = "build"
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val sdkName = /*System.getenv("SDK_NAME") ?: */"iphonesimulator"
    val targetName = "ios" +/*+ if (sdkName.startsWith("iphoneos")) "Arm64" else*/ "X64"
    val framework = kotlin.targets.getByName<KotlinNativeTarget>(targetName).binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)
    val targetDir = File(buildDir, "xcode-frameworks")
    from({ framework.outputDirectory })
    into(targetDir)
}
so when I want to generate framework for iPhone device I use ./gradlew shared:packFor XCode and for simulator I use ./gradlew shared:packForSimulator
in this way I don’t need to select iPhone or Simulator in XCode
g

georg

10/21/2020, 7:03 PM
You could also create an XCFramework. It contains both the framework for the device and the simulator. I created a Gradle plugin that can do that for you. Check it out on Github: https://github.com/ge-org/multiplatform-swiftpackage
k

Kris Wong

10/21/2020, 7:21 PM
a normal framework file contains the same. there's no need for a plugin
g

georg

10/21/2020, 7:25 PM
With a fat framework you need to remove the unused architectures (i.e. simulator) before uploading to the App Store. Otherwise Apple will reject the binary. An xcframework does not require this step
k

Kris Wong

10/21/2020, 7:25 PM
gotcha
or just use the coacoapods plugin and don't worry about it
g

georg

10/21/2020, 7:29 PM
True. It takes care of removing the simulator frameworks. But then you’re stuck with Cocoapods 😉 There are plenty of options out there. Just pick whatever suits your needs
u

Umar Ata

10/21/2020, 7:32 PM
So if I setup cocoa pods for my framework then I don’t need to generate framework?
but then it will become publicly available ?
g

georg

10/21/2020, 7:37 PM
It wont’t be public unless you publish it. There’s a nice article by Jetbrains about the integration of Cocoapods and KMP. https://kotlinlang.org/docs/reference/native/cocoapods.html
u

Umar Ata

10/21/2020, 7:38 PM
okay
16 Views