Hey I am learning to KMM with swift package manager. I am trying to build swiftpackage, but when I tried it giving me error on build. I am learning this form here. I am getting error when I run this command. I am adding my video. This is my project link. Can someone guide me what I am doing wrong here?
./gradlew kotlinmultiplatformsharedmodule:createSwiftPackage
build.gradle.kts
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework
plugins {
kotlin("multiplatform")
id("com.android.library")
id("com.chromaticnoise.multiplatform-swiftpackage") version "2.0.3"
}
kotlin {
multiplatformSwiftPackage {
packageName("kotlinmultiplatformsharedmodule")
swiftToolsVersion("5.3")
targetPlatforms {
iOS { v("13") }
}
outputDirectory(File("../app/"))
}
android()
val xcf = XCFramework()
listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach {
it.binaries.framework {
baseName = "kotlinmultiplatformsharedmodule"
xcf.add(this)
}
}
sourceSets {
val commonMain by getting
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
val androidMain by getting
val androidTest by getting
val iosX64Main by getting
val iosArm64Main by getting
val iosSimulatorArm64Main by getting
val iosMain by creating {
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
iosSimulatorArm64Main.dependsOn(this)
}
val iosX64Test by getting
val iosArm64Test by getting
val iosSimulatorArm64Test by getting
val iosTest by creating {
dependsOn(commonTest)
iosX64Test.dependsOn(this)
iosArm64Test.dependsOn(this)
iosSimulatorArm64Test.dependsOn(this)
}
}
}
android {
compileSdk = 32
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdk = 21
targetSdk = 32
}
}
Related
I am trying to read/write files in the commonMain module.
I created a new Kotlin Multiplatform App for Android and iOS using the Android Studio Wizard. (New -> New Project ... -> Koltin Multiplatform App)
Then I added okio as dependency to common in the shared/build.gradle.kts file.
sourceSets {
val okio = "3.3.0"
val commonMain by getting {
dependencies {
implementation("com.squareup.okio:okio:$okio")
}
}
...
In the module commonMain I cannot access FileSystem.SYSTEM (Unresolved Reference: SYSTEM) and therefore not read or write any files.
In the module iOSMain and androidMain I can access FileSystem.SYSTEM. Is this expected behaviour? This would require me to write an expected/actual mapping for all okio apis. Or is there an other way?
Or do I need to configure my project differently to be able to access FileSystem.SYSTEM inside the commonMain-Module?
shared/build.gradle
plugins {
kotlin("multiplatform")
kotlin("native.cocoapods")
id("com.android.library")
}
kotlin {
android()
iosX64()
iosArm64()
iosSimulatorArm64()
cocoapods {
summary = "Some description for the Shared Module"
homepage = "Link to the Shared Module homepage"
version = "1.0"
ios.deploymentTarget = "14.1"
podfile = project.file("../iosApp/Podfile")
framework {
baseName = "shared"
}
}
sourceSets {
val okio = "3.3.0"
val commonMain by getting {
dependencies {
implementation("com.squareup.okio:okio:$okio")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
val androidMain by getting
val androidTest by getting
val iosX64Main by getting
val iosArm64Main by getting
val iosSimulatorArm64Main by getting
val iosMain by creating {
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
iosSimulatorArm64Main.dependsOn(this)
}
val iosX64Test by getting
val iosArm64Test by getting
val iosSimulatorArm64Test by getting
val iosTest by creating {
dependsOn(commonTest)
iosX64Test.dependsOn(this)
iosArm64Test.dependsOn(this)
iosSimulatorArm64Test.dependsOn(this)
}
}
}
android {
namespace = "de.pixel.kmmpodstryout"
compileSdk = 32
defaultConfig {
minSdk = 21
targetSdk = 32
}
}
Greetings.kt in commonMain from the sample app generated by the wizard:
package de.pixel.kmmpodstryout
import okio.FileSystem
import okio.Path.Companion.toPath
class Greeting {
private val platform: Platform = getPlatform()
fun greet(): String {
val path = "helloworld.txt".toPath()
FileSystem.SYSTEM <--- "Unresolved Reference SYSTEM"
return "Hello, ${platform.name} ${readFile()}!"
}
}
We are trying to build single business logic with kmm to share it with ios and android and react project
IOS Project contains
swiftUI
cocoapods
react
typescript and react framework
package.json
the ios and react project doesn't include any kotlin code the business logic is compiled to .framework library and configured by default how do the same? build business logic as js library?
plugins {
kotlin("multiplatform")
kotlin("native.cocoapods")
kotlin("plugin.serialization") version "1.7.21"
id("com.android.library")
}
kotlin {
android()
iosX64()
iosArm64()
iosSimulatorArm64()
js {
browser {
webpackTask {
output.libraryTarget = "commonjs2"
}
}
binaries.executable()
}
cocoapods {
summary = "Some description for the Shared Module"
homepage = "Link to the Shared Module homepage"
version = "1.0"
ios.deploymentTarget = "14.1"
podfile = project.file("../iosApp/Podfile")
framework {
baseName = "shared"
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation(Coroutines.coroutinesCore)
implementation(Ktor.ktorCore)
implementation(Ktor.ktorContentNegotiation)
implementation(Ktor.ktorLogging)
implementation(Ktor.ktorSerializationJson)
implementation(SLF4j.slf4j)
implementation(Koin.koinCore)
implementation(MultiplatformSettings.settings)
implementation(Json.gson)
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
val androidMain by getting {
dependencies {
implementation(Ktor.ktorAndroid)
}
}
val androidTest by getting
val iosX64Main by getting
val iosArm64Main by getting
val iosSimulatorArm64Main by getting
val iosMain by creating {
dependsOn(commonMain)
dependencies {
implementation(Ktor.ktorIos)
}
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
iosSimulatorArm64Main.dependsOn(this)
}
val iosX64Test by getting
val iosArm64Test by getting
val iosSimulatorArm64Test by getting
val iosTest by creating {
dependsOn(commonTest)
iosX64Test.dependsOn(this)
iosArm64Test.dependsOn(this)
iosSimulatorArm64Test.dependsOn(this)
}
val jsMain by getting {
dependsOn(commonMain)
dependencies {
implementation(Ktor.ktorJs)
}
}
}
}
android {
namespace = "com.sample.kmm"
compileSdk = 33
defaultConfig {
minSdk = 21
targetSdk = 33
}
}
Kotlin Multiplatform share business logic with react project like ios
I am adding OkHttp in HttpClient. But I am getting error. Unresolved reference: OkHttp. I tried to add library in commonMain of build.gradle.kts, but I think I am missing some steps or doing something wrong. I want to use ktor Http but getting weird issue on my side. Can someone guide me, what am I am missing in my code?
import io.ktor.client.*
import io.ktor.client.engine.*
import io.ktor.client.engine.okhttp.*
fun createHttpClient() {
val client = HttpClient(OkHttp)
}
build.gradle.kts
plugins {
kotlin("multiplatform")
kotlin("native.cocoapods")
id("com.android.library")
}
version = "1.0"
kotlin {
android()
iosX64()
iosArm64()
iosSimulatorArm64()
cocoapods {
summary = "Some description for the Shared Module"
homepage = "Link to the Shared Module homepage"
ios.deploymentTarget = "14.1"
framework {
baseName = "kotlinmultiplatformsharedmodule"
}
}
sourceSets {
val commonMain by getting{
dependencies {
implementation("io.ktor:ktor-client-okhttp:2.0.0")
implementation("io.ktor:ktor-client-core:2.0.0")
implementation("io.insert-koin:koin-core:3.2.0-beta-1")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
val androidMain by getting
val androidTest by getting
val iosX64Main by getting
val iosArm64Main by getting
val iosSimulatorArm64Main by getting
val iosMain by creating {
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
iosSimulatorArm64Main.dependsOn(this)
}
val iosX64Test by getting
val iosArm64Test by getting
val iosSimulatorArm64Test by getting
val iosTest by creating {
dependsOn(commonTest)
iosX64Test.dependsOn(this)
iosArm64Test.dependsOn(this)
iosSimulatorArm64Test.dependsOn(this)
}
}
}
android {
compileSdk = 32
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdk = 21
targetSdk = 32
}
}
Since OkHttp is designed to work on JVM and Android, you can't use it in the common code. You can create a different engine for each platform and inject it into the common code.
Or you can use expect-actual, see: https://ktor.io/docs/http-client-engines.html#mpp-config. You create an expect fun createHttpClient(): HttpClient in the common code, and actual fun createHttpClient(): HttpClient with the implementation for corresponding platform in each of the platform modules.
I am trying to build a KMP library targeting iOS, Android, JS(Browser), Mac, Windows and Linux. For now I am only using Ktor and SQLDelight as a dependency. But getting the following issue in nativeMain's actual implementation while creating driver for SQLDelight
While the same code doesn't give any issue for iOS main which is also using the same NativeSqliteDriver (I need them separately since Ktor client for iOS and desktop platforms are separate).
Following is my build.gradle.kts
plugins {
kotlin("multiplatform") version "1.5.31"
id("maven-publish")
id("com.android.library")
kotlin("plugin.serialization") version "1.5.31"
id("com.squareup.sqldelight") version "1.5.3"
}
group = "me.group"
version = "1.0-SNAPSHOT"
val xcFrameworkName = "AddressLib"
repositories {
google()
mavenCentral()
}
kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = "1.8"
}
testRuns["test"].executionTask.configure {
useJUnit()
}
}
js(LEGACY) {
browser {
commonWebpackConfig {
cssSupport.enabled = true
}
}
}
val xcFramework = XCFramework(xcFrameworkName)
val hostOs = System.getProperty("os.name")
val isMingwX64 = hostOs.startsWith("Windows")
when {
hostOs == "Mac OS X" -> macosX64("native") {
binaries.framework(xcFrameworkName) {
xcFramework.add(this)
}
}
hostOs == "Linux" -> linuxX64("native")
isMingwX64 -> mingwX64("native")
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
android()
ios {
binaries.framework(xcFrameworkName) {
xcFramework.add(this)
}
}
val coroutinesVersion = "1.5.2-native-mt"
val serializationVersion = "1.3.1"
val ktorVersion = "1.6.5"
val sqlDelightVersion = "1.5.3"
val napierVersion = "2.2.0"
val koinVersion = "3.1.4"
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-serialization:$ktorVersion")
implementation("io.ktor:ktor-client-logging:$ktorVersion")
implementation("com.squareup.sqldelight:runtime:$sqlDelightVersion")
implementation("io.insert-koin:koin-core:$koinVersion")
implementation("io.github.aakira:napier:$napierVersion")
}
}
val commonTest by getting
val jvmMain by getting {
dependencies {
implementation("io.ktor:ktor-client-java:$ktorVersion")
implementation("com.squareup.sqldelight:sqlite-driver:$sqlDelightVersion")
}
}
val jvmTest by getting
val jsMain by getting {
dependencies {
implementation("io.ktor:ktor-client-js:$ktorVersion")
implementation("com.squareup.sqldelight:sqljs-driver:$sqlDelightVersion")
}
}
val jsTest by getting
val nativeMain by getting {
dependencies {
implementation("io.ktor:ktor-client-curl:$ktorVersion")
implementation("com.squareup.sqldelight:native-driver:$sqlDelightVersion")
}
}
val nativeTest by getting
val androidMain by getting {
dependencies {
implementation("io.ktor:ktor-client-android:$ktorVersion")
implementation("com.squareup.sqldelight:android-driver:$sqlDelightVersion")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
}
}
val iosMain by getting {
dependencies {
implementation("io.ktor:ktor-client-ios:$ktorVersion")
implementation("com.squareup.sqldelight:native-driver:$sqlDelightVersion")
}
}
val iosTest by getting
}
sqldelight {
database("AddressDatabase") {
packageName = "com.library.address.database"
}
}
}
android {
compileSdkVersion(31)
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(24)
targetSdkVersion(31)
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
publishing {
repositories {
maven {
credentials {
username = "<username>"
password = "<pwd>"
}
url = URI("https://mymavenrepo.com")
}
}
}
So it seems the issue was somewhat due to same dependency being added to the build gradle twice and it's corresponding code being added twice as well. To solve the same I had to make a separate source set like the following
val sqlDriverNativeMain by creating {
dependsOn(commonMain)
dependencies {
implementation("com.squareup.sqldelight:native-driver:$sqlDelightVersion")
}
}
val iosMain by getting {
dependsOn(sqlDriverNativeMain)
dependencies {
implementation("io.ktor:ktor-client-ios:$ktorVersion")
}
}
val nativeMain by getting {
dependsOn(sqlDriverNativeMain)
dependencies {
implementation("io.ktor:ktor-client-curl:$ktorVersion")
}
}
and after that move the driver creation code inside the sourceSet directory named sqlDriverNativeMain. This resolved the issue.
I am trying to set up Kotlin Multiplatform Library in my android existing application, but getting the following error:
Build file '/Users/tomtharakan/Documents/Projects/android/MyApplication/kmmsharedmodule/build.gradle.kts' line: 1
Plugin [id: 'org.jetbrains.kotlin.multiplatform'] was not found in any of the following sources:
Gragle Config:
plugins {
kotlin("multiplatform")
kotlin("native.cocoapods")
id("com.android.library")
}
version = "1.0"
kotlin {
android()
iosX64()
iosArm64()
//iosSimulatorArm64() sure all ios dependencies support this target
cocoapods {
summary = "Some description for the Shared Module"
homepage = "Link to the Shared Module homepage"
ios.deploymentTarget = "14.1"
framework {
baseName = "kmmsharedmodule"
}
}
sourceSets {
val commonMain by getting
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
}
}
val iosX64Main by getting
val iosArm64Main by getting
//val iosSimulatorArm64Main by getting
val iosMain by creating {
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
//iosSimulatorArm64Main.dependsOn(this)
}
val iosX64Test by getting
val iosArm64Test by getting
//val iosSimulatorArm64Test by getting
val iosTest by creating {
dependsOn(commonTest)
iosX64Test.dependsOn(this)
iosArm64Test.dependsOn(this)
//iosSimulatorArm64Test.dependsOn(this)
}
}
}
android {
compileSdk = 30
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdk = 21
targetSdk = 30
}
}