Skip to content

santimattius/kmp-networking

Repository files navigation

KMP Networking

This is a Kotlin Multiplatform project targeting Android, iOS.

class ServiceCreator(baseUrl: String) {

    private val client = HttpClient {
        install(ContentNegotiation) {
            json(Json { isLenient = true; ignoreUnknownKeys = true })
        }
    }
    private val ktorfit = Ktorfit.Builder()
        .baseUrl(baseUrl)
        .httpClient(client)
        .build()

    fun createPictureService() = ktorfit.create<PictureService>()
}

interface PictureService {

    @GET("random")
    suspend fun random(): Picture
}
  • /composeApp is for code that will be shared across your Compose Multiplatform applications. It contains several subfolders:

    • commonMain is for code that’s common for all targets.
    • Other folders are for Kotlin code that will be compiled for only the platform indicated in the folder name. For example, if you want to use Apple’s CoreCrypto for the iOS part of your Kotlin app, iosMain would be the right folder for such calls.
  • /iosApp contains iOS applications. Even if you’re sharing your UI with Compose Multiplatform, you need this entry point for your iOS app. This is also where you should add SwiftUI code for your project.

Prepare the environment

  • Install and configure the latest JDK 17+.
  • If you have Gradle installed, make sure you use Gradle 8.1 or later.
  • Install and configure the latest Android Studio for Android samples.
  • Install and configure the latest Xcode for iOS samples.

Use the KDoctor tool to ensure that your development environment is configured correctly:

  1. Install KDoctor with Homebrew:

    brew install kdoctor
    
  2. Run KDoctor in your terminal:

    kdoctor
    

    If everything is set up correctly, you'll see valid output:

    Environment diagnose (to see all details, use -v option):
    [✓] Operation System
    [✓] Java
    [✓] Android Studio
    [✓] Xcode
    [✓] Cocoapods
    
    Conclusion:
      ✓ Your system is ready for Kotlin Multiplatform Mobile development!
    

Otherwise, KDoctor will highlight which parts of your setup still need to be configured and will suggest a way to fix them.

Learn more about Kotlin Multiplatform

About

Example using Ktorfit

Resources

Stars

Watchers

Forks