How to use Log in kotlin multiplatform Moblie

4.8k views Asked by At

Hey I am working in Ktor. I found this answer to show log in specific platform iOS and Android. But when I am building this through gradle I am getting error on this when running this command on terminal. Normal build is successfully without any error, but getting error when I used below command.

./gradlew build --warning-mode all 

Error

* What went wrong:
Could not determine the dependencies of task ':kotlinmultiplatformsharedmodule:compileIosMainKotlinMetadata'.
> Could not resolve all artifacts for configuration ':kotlinmultiplatformsharedmodule:allSourceSetsCompileDependenciesMetadata'.
   > Could not resolve io.ktor:ktor-client-logging-native:2.0.1.
     Required by:
         project :kotlinmultiplatformsharedmodule
      > Could not resolve io.ktor:ktor-client-logging-native:2.0.1.
         > Could not get resource 'https://s3.amazonaws.com/salesforcesos.com/android/maven/release/io/ktor/ktor-client-logging-native/2.0.1/ktor-client-logging-native-2.0.1.pom'.
            > Could not GET 'https://s3.amazonaws.com/salesforcesos.com/android/maven/release/io/ktor/ktor-client-logging-native/2.0.1/ktor-client-logging-native-2.0.1.pom'. Received status code 403 from server: Forbidden
      > Could not resolve io.ktor:ktor-client-logging-native:2.0.1.
         > Could not get resource 'https://mobile-sdk.jumio.com/io/ktor/ktor-client-logging-native/2.0.1/ktor-client-logging-native-2.0.1.pom'.
            > Could not GET 'https://mobile-sdk.jumio.com/io/ktor/ktor-client-logging-native/2.0.1/ktor-client-logging-native-2.0.1.pom'. Received status code 403 from server: Forbidden

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

build.gradle.kts

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
    id("kotlinx-serialization")
}

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 = "13.0"
        framework {
            baseName = "kotlinmultiplatformsharedmodule"
        }
    }

    sourceSets {
        val ktorVersion = "2.0.1"
        val commonMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-core:$ktorVersion")
                implementation("io.ktor:ktor-client-logging:$ktorVersion")
                implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
                implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
                implementation("io.ktor:ktor-client-auth:$ktorVersion")
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.3.2")
                implementation("io.insert-koin:koin-core:3.2.0-beta-1")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val androidMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
                implementation("io.ktor:ktor-client-logging-jvm:$ktorVersion")
            }
        }
        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)
            dependencies {
                implementation("io.ktor:ktor-client-darwin:$ktorVersion")
                implementation("io.ktor:ktor-client-logging-native:$ktorVersion")
            }
        }
        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
    }
}

Can someone know how can I use log in iOS/darwin side. Thanks

4

There are 4 answers

2
Brady On

Leave the Ktor logging library in commonMain, and remove it in any other source sets. The right implementation for each platform will be brought in automatically.

val commonMain by getting {
    implementation("io.ktor:ktor-client-logging:$ktor_version")
}

Also, since you're using a private maven repository, make sure the artifacts are actually there.

As for logging for Android / JVM, it doesn't work out of the box, unless you add a dependency on an SLF4J implementation, per the docs: https://ktor.io/docs/client-logging.html#add_dependencies

If you'd rather not bring in the dependency, you can just implement a simple logger:

class DebugKtorLogger : Logger {
    override fun log(message: String) {
        Log.v("Ktor", message)
    }
}

and install it:

val client = HttpClient(OkHttp) {
    install(Logging) {
        logger = DebugKtorLogger()
        level = BODY
    }
}
1
Róbert Nagy On

If you check for the ktor-client-logging-native artifact on the Maven Repository you'll find that the latest version is 1.3.1.

So they likely dropped the artifact under that name from v2. If you search for ktor-client-logging similarly, you'll find artifacts with -iosx64, -iosarm64 architectures that have a v2.0.1.

I couldn't find a proper Ktor documentation, but I believe that if you only add the io.ktor:ktor-client-logging dependency in commonMain it will figure out which platform specific dependency to pull in.

TL;DR

You can remove both

implementation("io.ktor:ktor-client-logging-jvm:$ktorVersion")

and

implementation("io.ktor:ktor-client-logging-native:$ktorVersion")

As they will be pulled in automatically by specifying the commonMain dependency

val commonMain by getting {
    implementation("io.ktor:ktor-client-logging:$ktorVersion")
}
0
AliSh On

I had trouble getting logs to show up in the logcat for my multiplatform app even after setting up the Ktor client. The fix for me was changing the logger from Logger.Default to Logger.SIMPLE.

Here's what I did:

  1. Add the Ktor client logging dependency in your commonMain module:

    commonMain.dependencies {
        implementation("io.ktor:ktor-client-logging:$ktor")
    }
    
  2. Update the logging setup:

    install(Logging) {
        logger = Logger.SIMPLE
        level = LogLevel.ALL
    }
    

After making these changes, my logs started showing up in the logcat.

0
Yurowitz On

You need to use something like Napier KMM library in order to make use of the ktor-client-logging dependency. You can find Napier on GitHub from this link:

https://github.com/AAkira/Napier

Here's an official JetBrains YouTube video on how to implement Napier to log Ktor stuff...

https://www.youtube.com/watch?v=_Q62iJoNOfg