Skip to content

Compose for iOS, macOS and Web #74

Closed

Description

@arkivanov

Latest builds of JetBrains Compose support native iOS and macOS targets. Let's try to add it to extensions-compose-jetbrains module. Since it's far from stable, a separate branch seems reasonable.

Status update: experimental -native-compose versions of Decompose with Compose for Darwin (Apple/iOS) are published from the compose-experimental branch, and have <version>-compose-experimental version suffix.

Activity

  1. added
    enhancement
    investigate
    on Apr 14, 2022
  2. arkivanov commented on Apr 14, 2022

    @arkivanov
    OwnerAuthor

    Created the branch: compose-darwin.

  3. arkivanov commented on Apr 15, 2022

    @arkivanov
    OwnerAuthor

    The compiler crashes on CI, filed: JetBrains/compose-multiplatform#2007

  4. changed the title [-]Compose native iOS and macOS support[/-] [+]Compose for iOS, macOS and Web[/+] on Feb 2, 2023
  5. arkivanov commented on Feb 2, 2023

    @arkivanov
    OwnerAuthor

    Web (Canvas) support is now enabled. The new branch for all experimental targets is compose-experimental.

  6. added
    backlog
    and removed
    investigate
    on Apr 19, 2023
  7. blueberry404 commented on May 6, 2023

    @blueberry404

    Hi @arkivanov, first of all thank you to create this amazing library. I have been trying to integrate Decompose in Kotlin Multiplatform Compose project. I tried my best to thoroughly review your documentation and samples. I am unable to resolve following error on iOS:

    Task :shared:linkPodDebugFrameworkIosX64 FAILED
    error: Following dependencies exported in the podDebugFramework binary are not specified as API-dependencies of a corresponding source set:
    FAILURE: Build failed with an exception.

    • What went wrong:
      Execution failed for task ':shared:linkPodDebugFrameworkIosX64'.

    Following dependencies exported in the podDebugFramework binary are not specified as API-dependencies of a corresponding source set:

    Files: [/Users/Admin/.gradle/caches/modules-2/files-2.1/com.arkivanov.decompose/decompose-iosx64/2.0.0-alpha-02/c681ede152c090cec61131c93a90d75bac1c43fc/decompose.klib]

    Please add them in the API-dependencies and rerun the build.

    Here are the chunks of my build.gradle.kts:

    cocoapods {
            ....
            framework {
                baseName = "shared"
                isStatic = true
    
                export("com.arkivanov.decompose:decompose:2.0.0-alpha-02")
                export("com.arkivanov.essenty:lifecycle:1.1.0")
            }
            extraSpecAttributes["resources"] = "['src/commonMain/resources/**', 'src/iosMain/resources/**']"
        }
        
    sourceSets {
            val commonMain by getting {
               implementation("com.arkivanov.decompose:decompose:2.0.0-alpha-02")
               implementation("com.arkivanov.decompose:extensions-compose-jetbrains:2.0.0-compose-experimental-alpha-02")
             }
    }
    

    I also tried to add dependencies like here, but no luck probably because it doesn't use cocoapods.

    Please let me know if I am missing anything. Thank you.

  8. arkivanov commented on May 6, 2023

    @arkivanov
    OwnerAuthor

    @blueberry404 any module that is exported to the framework must be specific as api dependency, not implementation. You also missing the dependencies {} block. See the example.

  9. blueberry404 commented on May 6, 2023

    @blueberry404

    Thank you for quick response. Sorry, missing dependency block was pasting typo at my end. Key was to use api dependencies for both in iOSMain. I am able to compile it now. Thank you!

    dependencies {
                    api("com.arkivanov.decompose:decompose:$decompose")
                    api("com.arkivanov.decompose:extensions-compose-jetbrains:2.0.0-compose-experimental-alpha-02")
                }
    
  10. arkivanov commented on May 6, 2023

    @arkivanov
    OwnerAuthor

    Awesome! Glad it worked. Most likely you don't need to export extensions-compose-jetbrains, as it contains extensions specifically for Compose.

  11. blueberry404 commented on May 6, 2023

    @blueberry404

    Actually, I am going to write the common compose code for Android and iOS using jetbrain's compose, therefore I thought this is also I needed.

  12. arkivanov commented on May 6, 2023

    @arkivanov
    OwnerAuthor

    I think exporting is only required if you want co use that APIs in the iOS project (Xcode). If you are just using it from Kotlin, then you shouldn't need to export Compose extensions. Just implementation should enough. You can try at least.

  13. blueberry404 commented on May 6, 2023

    @blueberry404

    Ok I get your point now. I will try and let you know. Thank you!

  14. 8 remaining items

  15. arkivanov commented on Jul 7, 2023

    @arkivanov
    OwnerAuthor

    Looks correct @chrisjenx. Just one possible point to improve in case you find it useful. I find it cleaner to destroy the root component on willTerminateNotification, instead of the deinit block. Check out the example here.

  16. rezyfr commented on Sep 13, 2023

    @rezyfr

    linkPodDebugFrameworkIosX64 FAILED
    I also had above reported error for several hours with the correct setup like on the example

    turns out I use the 1.0.0 version for the essenty dependency, and it finally can compile after I update it to 1.1.0,
    just in case anyone has the same issue as me :)

  17. chrisjenx commented on Nov 22, 2023

    @chrisjenx
    Sponsor

    I changed to using the new AutomaticLifecycle, also not tried web yet, but we're using iOS in production now, not seen anything major that jumps out, need to test out persistance, but imo it seems pretty stable, iOS can probably not be considered "experimental" esp after moving to serilization plugin

  18. arkivanov commented on Nov 22, 2023

    @arkivanov
    OwnerAuthor

    Oh, it was only considered experimental because it was defined so by JetBrains. Now since we don't need org.jetbrains.compose.experimental.uikit.enabled=true anymore, the support of Compose for iOS will be moved to the stable branch in v3.0. Being tracked here: #451.

  19. chrisjenx commented on Nov 23, 2023

    @chrisjenx
    Sponsor

    Awesome, thank you for your superb work!

  20. markvtailor commented on Dec 4, 2023

    @markvtailor

    Hi! I'm trying to implement shared navigation in my KMP project with shared UI. It works fine with Android, but I encountered an error with iOS:

    error: Overload resolution ambiguity:
    public fun <C : Any, T : Any> Children(stack: ChildStack<TypeVariable(C), TypeVariable(T)>, modifier: Modifier = ..., animation: StackAnimation<TypeVariable(C), TypeVariable(T)>? = ..., content: (child: Child.Created<TypeVariable(C), TypeVariable(T)>) -> Unit): Unit defined in com.arkivanov.decompose.extensions.compose.jetbrains.stack
    public fun <C : Any, T : Any> Children(stack: Value<ChildStack<TypeVariable(C), TypeVariable(T)>>, modifier: Modifier = ..., animation: StackAnimation<TypeVariable(C), TypeVariable(T)>? = ..., content: (child: Child.Created<TypeVariable(C), TypeVariable(T)>) -> Unit): Unit defined in com.arkivanov.decompose.extensions.compose.jetbrains.stack

    App.kt:34:9: error: Cannot infer a type for this parameter. Please specify it explicitly.

    App.kt:36:50: error: @composable invocations can only happen from the context of a @composable function

    App.kt:36:50: error: @composable invocations can only happen from the context of a @composable function

    RootComponent.kt:24:32: error: Type mismatch: inferred type is Any but com.arkivanov.essenty.parcelable.Parcelable /* = com.arkivanov.parcelize.darwin.Parcelable */ was expected

    RootComponent.kt:24:32: error: Type mismatch: inferred type is Any but RootComponent.Configuration was expected

    I suspect there is some problems with iOS dependencies, but I was unable to track the exact cause. My build.gradle looks like this. Please let me know if I am missing anything. Thank you.

    kotlin {
        targetHierarchy.default()
        android {
            compilations.all {
                kotlinOptions {
                    jvmTarget = "1.8"
                }
            }
        }
    ```
    
        listOf(
            iosX64(),
            iosArm64(),
            iosSimulatorArm64()
        ).forEach {
            it.binaries.framework {
                baseName = "shared"
                isStatic = true
                val decompose_version = "2.2.0-compose-experimental"
                val essenty_version = "1.3.0"
    
                export("com.arkivanov.decompose:decompose:$decompose_version")
                export("com.arkivanov.essenty:lifecycle:$essenty_version")
    
                // Optional, only if you need state preservation on Darwin (Apple) targets
                export("com.arkivanov.essenty:state-keeper:$essenty_version")
            }
        }
    
    
        sourceSets {
            val androidMain by getting {
                dependencies {
                    implementation ("com.google.accompanist:accompanist-systemuicontroller:0.27.0")
                }
                dependsOn(commonMain.get())
            }
    
            named("commonMain") {
                dependencies {
                    api(compose.foundation)
                    api(compose.animation)
                    val decomposeVersion = "2.2.0"
                    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
                    implementation("com.arkivanov.decompose:decompose:$decomposeVersion-compose-experimental")
                    implementation("com.arkivanov.decompose:extensions-compose-jetbrains:$decomposeVersion-compose-experimental")
                    implementation("com.russhwolf:multiplatform-settings:1.1.1")
                    implementation("com.russhwolf:multiplatform-settings-no-arg:1.1.1")
                    implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.1")
                    implementation("com.moriatsushi.insetsx:insetsx:0.1.0-alpha10")
                    //put your multiplatform dependencies here
                    implementation(compose.runtime)
                    implementation(compose.foundation)
                    implementation(compose.material)
                    @OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
                    implementation(compose.components.resources)
                    implementation("dev.icerock.moko:resources:0.23.0")
                    implementation("dev.icerock.moko:resources-compose:0.23.0")
                    implementation("androidx.constraintlayout:constraintlayout-compose:1.0.1")
                }
            }
        }
    
        sourceSets {
            named("iosMain") {
                dependencies {
                    val decompose_version = "2.2.0-compose-experimental"
                    val essenty_version = "1.3.0"
                    api("com.arkivanov.decompose:decompose:$decompose_version")
                    api("com.arkivanov.essenty:lifecycle:$essenty_version")
                }
            }
        }
    
  21. arkivanov commented on Dec 4, 2023

    @arkivanov
    OwnerAuthor

    @markvtailor it's difficult to help without the reproducer or at least the code. Feel free to file a separate issue with the additional information.

  22. arkivanov commented on Dec 9, 2023

    @arkivanov
    OwnerAuthor

    Starting with Decompose 3.0.0-alpha01 all Compose variants are supported without any version suffixes.

  23. chrisjenx commented on Dec 9, 2023

    @chrisjenx
    Sponsor

    🥳 Nice work!

  24. malliaridis commented on Apr 7, 2024

    @malliaridis
    Contributor

    @arkivanov are the Docs outdated about WASM (see here)? May be addressed in #610 if that is the case.

  25. arkivanov commented on Apr 7, 2024

    @arkivanov
    OwnerAuthor

    Thanks for pointing that out! The docs should be fixed now.

  26. Germansdev commented on Sep 18, 2024

    @Germansdev

    Arkinov: Thank you very much for your great work!

    PROBLEM: When adding the decompose extensions and sync gradle, an error occurs:
    Thanks!
    Method org/gradle/accessors/dm/LibrariesForLibs_Decorated.getExtensions()Lorg/gradle/api/plugins/ExtensionContainer; is abstract
    Exception is:
    java.lang.AbstractMethodError: Method org/gradle/accessors/dm/LibrariesForLibs_Decorated.getExtensions()Lorg/gradle/api/plugins/ExtensionContainer; is abstract

    build.gradle.kts(composeApp)
    plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.jetbrainsCompose)
    alias(libs.plugins.androidApplication)
    alias(libs.plugins.compose.compiler)

    }

    repositories {
    google()
    mavenCentral()
    }
    kotlin {
    androidTarget()

    jvm("desktop")
    
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach { iosTarget ->
        iosTarget.binaries.framework {
            baseName = "ComposeApp"
            isStatic = true
        }
    }
    
    sourceSets {
        val desktopMain by getting
    
        all {
            languageSettings.optIn("kotlinx.cinterop.ExperimentalForeignApi")
        }
    
        commonMain.dependencies {
    

    ....
    //decompose:
    implementation(libs.decompose)
    implementation(libs.extensions.compose)
    implementation(libs.essenty.lifecycle)
    ...}

    androidMain.dependencies {
    implementation(libs.decompose)
    ...}

    iosMain.dependencies {...}

    desktopMain.dependencies {
    implementation(compose.desktop.currentOs)
    implementation(libs.kotlinx.coroutines.swing)

        }
    }
    

    }

    android {
    namespace = "cmp.project.free_market"
    compileSdk = AndroidSdk.COMPILE
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    sourceSets["main"].res.srcDirs("src/androidMain/res")

    defaultConfig {
        applicationId = "cmp.project.free_market"
        minSdk = AndroidSdk.MIN
        targetSdk = AndroidSdk.TARGET
        versionCode = 1
        versionName = "1.0"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = true 
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"), 
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
    
        sourceCompatibility = JavaVersion.VERSION_17 
        targetCompatibility = JavaVersion.VERSION_17 
    }
    buildFeatures {
        compose = true
    }
    dependencies {
        debugImplementation(compose.uiTooling)
    }
    

    }
    dependencies {
    implementation(libs.androidx.ui.graphics.android)
    implementation(libs.androidx.media3.exoplayer)
    implementation(libs.androidx.navigation.runtime.ktx)
    implementation(libs.firebase.firestore.ktx)
    implementation(libs.firebase.storage.ktx)
    implementation(libs.androidx.ui.desktop)
    implementation(libs.androidx.ui.unit.desktop)
    implementation(libs.androidx.core)
    }
    compose.resources {
    publicResClass = true
    packageOfResClass = "me.sample.library.resources"
    generateResClass = always
    }

    compose.desktop {
    application {
    mainClass = "cmp.project.free_market.MainKt"

        nativeDistributions {
            targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
            packageName = "cmp.project.free_market"
            packageVersion = "1.0.0"
        }
    }
    

    }

    object AndroidSdk {
    const val MIN = 24
    const val COMPILE = 34
    const val TARGET = COMPILE
    }


    build.gradle.kts (Project)
    plugins {
    // this is necessary to avoid the plugins to be loaded multiple times
    // in each subproject's classloader
    alias(libs.plugins.androidApplication) apply false
    alias(libs.plugins.androidLibrary) apply false
    alias(libs.plugins.jetbrainsCompose) apply false
    alias(libs.plugins.compose.compiler) apply false
    alias(libs.plugins.kotlinMultiplatform) apply false
    alias(libs.plugins.googleServices) apply false //generar y agregar el json primero y luego descomentar
    // alias(libs.plugins.kotlinxSerialization)
    }

    repositories {
    google()
    mavenCentral()
    maven {
    url = uri("https://s01.oss.sonatype.org/content/repositories/releases/")
    }
    }


    libs.versions.toml:
    [versions]
    activityCompose = "1.9.1"
    agp = "8.5.2"
    compose-plugin = "1.6.11"
    kotlin = "2.0.10"
    gservices = "4.4.2"

    decompose = "3.1.0"
    #extensionsCompose = "3.1.0"
    essenty = "2.1.0"
    ....
    [libraries]
    decompose = { module = "com.arkivanov.decompose:decompose", version.ref = "decompose" }
    #extensions-compose = { module = "com.arkivanov.decompose:extensions-compose", version.ref = "extensionsCompose" }
    essenty-lifecycle = { module = "com.arkivanov.essenty:lifecycle", version.ref = "essenty" }
    ...
    [plugins]
    androidApplication = { id = "com.android.application", version.ref = "agp" }
    androidLibrary = { id = "com.android.library", version.ref = "agp" }
    jetbrainsCompose = { id = "org.jetbrains.compose", version.ref = "compose-plugin" }
    compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
    kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
    googleServices = { id = "com.google.gms.google-services", version.ref = "gservices"}


    Decompose extensions for Jetpack/Jetbrains Compose

    -keep class com.arkivanov.decompose.extensions.compose.jetbrains.mainthread.SwingMainThreadChecker

    gradle-wrapper.prperties:
    #Wed Sep 18 09:03:09 ART 2024
    distributionBase=GRADLE_USER_HOME
    distributionPath=wrapper/dists
    distributionUrl=https://services.gradle.org/distributions/gradle-8.7-bin.zip
    networkTimeout=10000
    validateDistributionUrl=true
    zipStoreBase=GRADLE_USER_HOME
    zipStorePath=wrapper/dists

    gradle.properties:

    kotlin.code.style=official

    #Gradle
    org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options="-Xmx2048M"

    #Android
    android.nonTransitiveRClass=true
    android.useAndroidX=true

    #Kotlin Multiplatform
    kotlin.mpp.enableCInteropCommonization=true

    #To hide warning message log cannot be built on this machine..ios..
    kotlin.native.ignoreDisabledTargets=true

    org.gradle.unsafe.accessors=false
    org.gradle.java.home=C:/Users/Willkommen/.jdks/corretto-17.0.7

  27. arkivanov commented on Sep 18, 2024

    @arkivanov
    OwnerAuthor

    Please try renaming extensions-compose to e.g. decompose-extensions-compose in your libs.versions.toml.

  28. Germansdev commented on Sep 18, 2024

    @Germansdev

    Thanks twice Master Arkivanov (quick and solved my karma!!) You are amazing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backlogenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions