Kotlin Multiplatform Mobile: Project already has a CocoaPods dependency with name SDWebImage - kotlin

I have created a demo project to integrate cocoapods into the KMM project. I have followed this link from the official website. At step 3 while reimporting the project, I am receiving the following error.
Project already has a CocoaPods dependency with name SDWebImage , after which I am unable to even import this lib in KMM.
Can anyone please help with this?
Update: Adding build.gradle.kts
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
id("com.android.library")
kotlin("native.cocoapods")
}
// CocoaPods requires the podspec to have a version.
version = "1.0"
kotlin {
android()
ios {
cocoapods {
// Configure fields required by CocoaPods.
summary = "Some description for a Kotlin/Native module"
homepage = "Link to a Kotlin/Native module homepage"
pod("SDWebImage")
// You can change the name of the produced framework.
// By default, it is the name of the Gradle project.
frameworkName = "shared"
}
}
sourceSets {
val commonMain by getting
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation("com.google.android.material:material:1.2.1")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13")
}
}
val iosMain by getting
val iosTest by getting
}
}
android {
compileSdkVersion(29)
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(24)
targetSdkVersion(29)
}
}
val packForXcode 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)
}
tasks.getByName("build").dependsOn(packForXcode)

When you’re using cocoapods plugin you don’t need to manually declare packForXcode target, maybe that’s the problem. Try to remove everything after val packForXcode
cocoapods section should be inside kotlin, not inside ios

Related

(KotlinSourceSet with name 'androidMain' not found.) How can I add android as a build target to a Kotlin Multiplatform project

I am trying to add android() as a build target to a Kotlin Multiplatform library so that I can add a specific library for the android target. All the other targets (jvm, linux, ios) work fine, but android seems to have issues, because the KotlinSourceSet is not being create like the others.
That's where I get the error:
KotlinSourceSet with name 'androidMain' not found.
I wonder, do I have to add the SourceSet manually? Am I missing some crucial build step?
Here is my gradle.build.kts
plugins {
id("maven-publish")
kotlin("multiplatform") version "1.8.0"
}
buildscript {
repositories {
google()
}
dependencies {
classpath ("com.android.tools.build:gradle:4.2.2")
classpath ("org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0")
}
}
group = "zzz.xxxx"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
kotlin {
jvm()
linuxX64("linux")
ios()
android()
sourceSets {
val commonMain by getting {
dependencies {
implementation("...")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
val jvmMain by getting {
dependencies {
implementation("...")
}
}
val androidMain by getting {
dependencies {
implementation ("...")
}
}
val jvmTest by getting
}
}
I tried adding a androidMain folder, but it is not getting recognized as a KotlinSourceSet. Non of the resources online seem to help either.
To add android as a build target your need to setup android-gradle-plugin first.
With example setting of android plugin gradle.build.kts will be:
plugins {
id("maven-publish")
kotlin("multiplatform") version "1.8.0"
id("com.android.library") version "7.3.0" // (1) add plugin
}
//...
// (2) setup plugin
android {
compileSdk = 33
defaultConfig {
minSdk = 24
multiDexEnabled = true
}
sourceSets {
getByName("main") {
manifest.srcFile("src/androidMain/AndroidManifest.xml")
}
}
}
UPDATE
Before you are adding Android Gradle plugin:
Check that Google Maven repository is listed in your settings.gradle(.kts) file
Check the Android Gradle plugin is added to the project build.gradle(.kts) file
Check the Android Gradle plugin is compatible with your Gradle version

KMM & KMP share business logic with react app

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

Problems with Android Studio and Cocoapods

I´m complete new to Android Studio with multiplatform projects and Kotlin.
Actually I´m wondering because of two things.
After writing the logic in Kotlin shared/src/commonMain/... I wanted to test it by writing an Activity on the Android part. I made a new one in shared/src/AndroidMain/... by rightclick on the folder.
The new Activity has been generated in shared/src/Main/... - a new folder hirachy. Why??
After that, my gradle.build in shared/ cannot be synced anymore. I´m getting the error:
Cocoapods Integration requires pod version to be specified.
Please specify pod version by adding 'version = ""' to the cocoapods block.
Alternatively, specify the version for the entire project explicitly.
Pod version format has to conform podspec syntax requirements: https://guides.cocoapods.org/syntax/podspec.html#version
I´ve put 'version="1.0"' on every part of the gradle files where it could be right but the message still remains.
Can anybody explain me excactly where to put the version number? I´ve read a lot of pages, saying all the same but nothing works for my project.
Here my shared/build.gradle.kts
plugins {
kotlin("multiplatform")
id("org.jetbrains.gradle.apple.applePlugin") version "212.4638.14-0.13.1"
//kotlin("native.cocoapods")
id ("org.jetbrains.kotlin.native.cocoapods")
id("com.android.library")
id ("com.google.gms.google-services")
kotlin("plugin.serialization")
id("org.jetbrains.kotlin.android")
}
version = "1.0"
kotlin {
android()
iosX64()
iosArm64()
iosSimulatorArm64()
cocoapods {
version = "1.0"
summary = "Some description for the Shared Module"
homepage = "Link to the Shared Module homepage"
ios.deploymentTarget = "14.1"
podfile = project.file("../iosApp/Podfile")
framework {
baseName = "shared"
}
specRepos {
url("https://github.com/Kotlin/kotlin-cocoapods-spec.git")
}
pod("FirebaseAuth")
}
sourceSets {
val commonMain by getting{
dependencies{
implementation("dev.gitlive:firebase-database:1.6.1")
implementation ("dev.gitlive:firebase-auth:1.6.1")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.0-RC")
}
version = "1.0"
}
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)
version = "1.0"
}
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 = 27
targetSdk = 32
}
buildFeatures {
viewBinding = true
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies{
implementation("dev.gitlive:firebase-database:1.6.1")
implementation ("dev.gitlive:firebase-auth:1.6.1")
implementation("com.google.android.material:material:1.4.0")
implementation("androidx.appcompat:appcompat:1.5.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.navigation:navigation-fragment-ktx:2.3.5")
implementation("androidx.navigation:navigation-ui-ktx:2.3.5")
//implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.0-RC")
}
Any help would be highly appreciated :-)
Platform Windows 11
AndroidStudio 2021.2.1 Patch 2
Kotlin 1.7.10

IntelliJ Show an Error on kotlinx.serialization, but compiling works

I have a problem where IntelliJ tells me that the #Serializable annotation cannot be accessed as it's internal to kotlin.io etc. The kotlinx.serialization package also shows as an unresolved reference.
Apparently, this has something to do with using Kotlin 1.7.x as when I downgrade to Kotlin 1.6.10 the issue no longer presents itself. However, Gradle compiles and runs the application without issue even on 1.7.10. Therefore, this seems like ultimately an issue with IntelliJ and/or one of my settings.
This is a Kotlin Multiplatform project. I've tried many things to fix this:
Reloading Gradle Project
Refreshing Gradle Dependencies
Completely refreshing all project indexes via the new Repair IDE tool
Deleting .idea and .gradle folders
Adjusting the project structure versions
Generally messing around with versions and settings
My build.gradle.kts:
plugins {
application
kotlin("multiplatform") version Versions.kotlin
kotlin("plugin.serialization") version Versions.kotlin
}
group = "com.example"
version = "4.0"
repositories {
mavenCentral()
}
kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = "17"
}
withJava()
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}
js {
binaries.executable()
browser {
commonWebpackConfig {
cssSupport.enabled = true
}
webpackTask {
outputFileName = "app.js"
}
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.ktor:ktor-client-core:${Versions.ktor}")
implementation("io.ktor:ktor-client-websockets:${Versions.ktor}")
implementation("io.ktor:ktor-serialization-kotlinx-json:${Versions.ktor}")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.serialization}")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val jvmMain by getting {
dependencies {
implementation("io.ktor:ktor-server-core:${Versions.ktor}")
implementation("io.ktor:ktor-server-auth:${Versions.ktor}")
implementation("io.ktor:ktor-serialization:${Versions.ktor}")
implementation("io.ktor:ktor-server-html-builder:${Versions.ktor}")
implementation("io.ktor:ktor-client-cio:${Versions.ktor}")
implementation("io.ktor:ktor-server-cio:${Versions.ktor}")
implementation("io.ktor:ktor-server-websockets:${Versions.ktor}")
}
}
val jvmTest by getting
val jsMain by getting {
dependencies {
implementation("io.ktor:ktor-client-js:${Versions.ktor}")
implementation("io.ktor:ktor-client-json:${Versions.ktor}")
implementation("io.ktor:ktor-client-auth:${Versions.ktor}")
implementation("io.ktor:ktor-client-websockets:${Versions.ktor}")
implementation("io.ktor:ktor-client-serialization:${Versions.ktor}")
implementation("org.jetbrains.kotlin-wrappers:kotlin-react:${Versions.react}")
implementation("org.jetbrains.kotlin-wrappers:kotlin-react-dom:${Versions.react}")
}
}
val jsTest by getting
}
}
application {
mainClass.set("com.example.app.ServerKt")
}
tasks.named<Copy>("jvmProcessResources") {
val jsBrowserDistribution = tasks.named("jsBrowserDistribution")
from(jsBrowserDistribution)
}
tasks.named<JavaExec>("run") {
dependsOn(tasks.named<Jar>("jvmJar"))
classpath(tasks.named<Jar>("jvmJar"))
}
object Versions {
const val ktor = "2.0.3"
const val kotlin = "1.7.10"
const val coroutines = "1.6.3"
const val serialization = "1.3.3"
const val react = "18.2.0-pre.358-compat"
}
The class that is having issues is:
import kotlinx.serialization.Serializable
#Serializable
data class Customer(val id: Int, val firstName: String, val lastName: String)
I appreciate any help I can get

Cannot import dependecies in kotlin multiplatform mobile

I created a new kotlin multiplatform mobile project. I followed official documentation.
Basic project is working, I was able to build it on android without problems.
I wanted to add some api and I found ktor, which I have never used before. I followed docs here: https://kotlinlang.org/docs/mobile/use-ktor-for-networking.html and tutorial here: https://proandroiddev.com/kotlin-multiplatform-very-beginners-guide-part-2-api-d54f7326dc57 and all changes I did are:
I added ktor libraries into build.gradle.kts(:shared):
sourceSets {
val commonMain by getting {
dependencies {
implementation ("io.ktor:ktor-client-core:1.5.0")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation("com.google.android.material:material:1.2.1")
implementation("io.ktor:ktor-client-android:1.5.0")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13")
}
}
val iosMain by getting {
dependencies {
implementation("io.ktor:ktor-client-ios:1.5.0")
}
}
val iosTest by getting
}
And I created Api class where I wanted to create and use the HttpClient:
class Api() {
private val client = HttpClient()
suspend fun fetch(): String {
return ""
}
}
BUT HttpCLient() is "Unresolved reference" and it cannot be imported. I also tried to manually add import io.ktor.client.HttpClient
but io is "Unresolved reference".
Also I tried numerous rebuilds/cleans/syncs.
What am I doing wrong? Am I missing something?
I encountered the same problem on windows 7 with Android Studio 4.1.3. It was solved when in the build script of the project, in the dependencies section of the buildscript, it was updated the version of the kotlin-gradle-plugin artifact from 1.4.10 to 1.4.32. Below is a copy paste from project build.gradle.kts:
buildscript {
repositories {
gradlePluginPortal()
jcenter()
google()
mavenCentral()
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32")
classpath("com.android.tools.build:gradle:4.1.3")
}
}