what may the cause of "Unresolved reference: Composable" in iosMain? androidx.compose.runtime.Compos...
r
what may the cause of "Unresolved reference: Composable" in iosMain? androidx.compose.runtime.Compose is imported but the compiler outputs error
j
Share your build.gradle.kts file. The Compose dependency might be declared in the wrong source set.
r
Copy code
plugins {
    kotlin("multiplatform")
    id("com.android.library")
    id("org.jetbrains.compose")
    id("com.squareup.sqldelight")
    id("dev.icerock.mobile.multiplatform-resources")
}

//@OptIn(org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi::class)
kotlin {
    //targetHierarchy.default()

    android {
        compilations.all {
            kotlinOptions {
                jvmTarget = "17"
            }
        }
    }

    targets.withType(org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget::class.java).all {
        binaries.withType(org.jetbrains.kotlin.gradle.plugin.mpp.Framework::class.java).all {
            export("dev.icerock.moko:mvvm-core:0.16.1")

        }
    }

    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "shared"
            isStatic = true
            export("dev.icerock.moko:resources:0.23.0")
            export("dev.icerock.moko:graphics:0.9.0")

        }
    }

    sourceSets {
        val commonMain by getting {
            dependencies {
                //put your multiplatform dependencies here
                implementation(compose.runtime)
                implementation(compose.foundation)
                implementation(compose.material3)
                implementation("org.jetbrains.kotlinx:atomicfu:0.21.0")
                implementation(compose.materialIconsExtended)
                @OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
                implementation(compose.components.resources)

                api("dev.icerock.moko:resources:0.23.0")


                implementation("com.squareup.sqldelight:runtime:1.5.5")
                implementation("com.squareup.sqldelight:coroutines-extensions:1.5.5")
                implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
            }
        }
        /* val commonTest by getting {
             dependencies {
                 implementation(kotlin("test"))
             }
         }*/
        val androidMain by getting {
            dependencies {
                implementation("com.squareup.sqldelight:android-driver:1.5.5")
                implementation("androidx.appcompat:appcompat:1.6.1")

                implementation("androidx.activity:activity-compose:1.7.2")
            }
        }

        val iosX64Main by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating{
            dependencies {
                implementation("com.squareup.sqldelight:native-driver:1.5.5")
            }
            dependsOn(commonMain) //make commonMain a dependency of iosMain
            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) //make commonTest a dependency of iosTest
             iosX64Test.dependsOn(this)
             iosArm64Test.dependsOn(this)
             iosSimulatorArm64Test.dependsOn(this)
         }*/
    }

}


android {
    namespace = "com.example.kmmphillippcontactsproject"
    compileSdk = 33
    defaultConfig {
        minSdk = 28
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
}

sqldelight {
    database("ContactsDatabase") {
        packageName = "com.example.kmmphillippcontactsproject"
        sourceFolders = listOf("sqldelight")
    }
}


dependencies {
    implementation("androidx.core:core:1.10.1")

    commonMainApi("dev.icerock.moko:mvvm-core:0.16.1")
    commonMainApi("dev.icerock.moko:mvvm-compose:0.16.1")
    commonMainApi("dev.icerock.moko:mvvm-flow:0.16.1")
    commonMainApi("dev.icerock.moko:mvvm-flow-compose:0.16.1")
}

multiplatformResources {
    multiplatformResourcesPackage = "com.example.kmmphillippcontactsproject"
    multiplatformResourcesClassName = "SharedRes"
}
j
I don't see an obvious issue looking at your build script. I'd probably avoid defining dependencies in the top-level
dependencies
block as well as the
kotlin.sourceSets
. You should move
implementation("androidx.core:core:1.10.1")
to
androidMain.dependencies
. The
dependencies.commonMainApi(...)
dependencies can go under
commonMain.dependencies
as
api(...)
.
You can use
targetHierarchy.default()
, which you have commented out to avoid the need to manually define
iosMain by creating
(can just be
iosMain by getting
) and you can remove the
dependsOn
calls.
r
i did what you recommend. Keeps outputting Unresolved reference: Composable, Much appreciated if you could have a look on the following repo https://github.com/tmromao/KMMPhillippContactsProject.git
i'm on a Mac Mini M1, Don't understand if it implies changes, or not, in the build.gradle.kts
import androids.compose.runtime.Composable displays "Composable" in a different colour, lighter than the other imports...
j
Is the repository public? The link is a 404.
m
Maybe try and ask Philipp on Instragram directly?
r
sorry Jeff L Now is public, please retry
yes @Morgane Soula that would be my next step. I'm learning the technology. Trying to setup a project file using moko and sqldelight to do a simple app but it's all SOs complicated. Very frustrating for someone that just want to build a simple app (school quizz) for Android and iOS
q
Hey Tiago, you need to follow the same structure you are doing in your commenMain
r
Please elaborate Qamar S.
j
You can remove all of the
dependsOn
calls now using the
targetHierarchy.default()
, as this sets up the source set hierarchy for you. You can also move the
export(...)
from
Copy code
targets.withType(org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget::class.java).all {
    binaries.withType(org.jetbrains.kotlin.gradle.plugin.mpp.Framework::class.java).all {
        export("dev.icerock.moko:mvvm-core:0.16.1")

    }
}
into
Copy code
it.binaries.framework {
    ...
}
as this is the only native target framework block.
q
means you need to put your component on the same sturcuture package you put in commonMain
j
I was able to build the iOS app without any errors on my machine.
Those were the only changes I made, but shouldn't be necessary for successful build.
Try invalidating caches and restarting Android Studio.
q
I just run it @Romão its working?
I faced this issue before and it was because I am not following the same structure for the actual function, so that I mentioned you need to follow the same sturcture.
r
Thanks Jeff L. Did what you explain. Got the same
IMG_20230722_185612.jpg
IMG_20230722_185647.jpg
Can the cause be the JDK? 17
q
nope I am using 17 JDK
did you invalidate cash?
cache?
r
Yes Qamar S. And i checked the structure
Structure is ok
q
yeah your project runs on my machine
I didnt face any error
r
AS understands the expected, actual implementation
But doesn t recognizes Compose
which Android Studio are you guys using?
q
this is mine
r
Im on flamingo, can it be the cause?
q
let me check
I have flamingo version
Nope
Works fine
r
so frustrating
can it be due to M1 memories constraints 8GB
q
Don't think so
j
I used Android Studio Flamingo, latest 2022.2.1 Patch 2 version. Do you have the KMM plugin installed?
q
I believe that AS is just caching, try to close it and restart your machine and reopen it.
j
Kotlin Multiplatform Mobile plugin 0.6.1(222)-14
r
checking
j
Try running kdoctor as well.
r
you took the dev.icerock.mobile.multiplatform-resourcess off, Jeff L, correct?
j
No, I just moved
export("dev.icerock.moko:mvvm-core:0.16.1")
to where the others are defined in the
framework
block.
r
ok
run kdoctor all "green"
1
have the latest AS Flamingo Patch 2
cleaning project files, invalidating cache and reboot
not much to
j
You might upgrade gradle in
gradle-wrapper.properties
. The latest version is 8.2.1
r
IMG_20230722_192245.jpg
j
There is a newer kdoctor version. Probably won't make a difference, but worth a try.
r
Got the following jeff
j
That's the Android gradle plugin version.
r
IMG_20230722_192639.jpg
j
In
gradle-wrapper.properties
change
distributionUrl=https\:<//services.gradle.org/distributions/gradle-8.0-bin.zip>
to
distributionUrl=https\:<//services.gradle.org/distributions/gradle-8.2.1-bin.zip>
The Android gradle plugin you could update from 8.0.1 to 8.0.2, but that's not going to be related.
r
The strangest is i get build succesful
j
So it's working for you now?
r
IMG_20230722_193313.jpg
it builds, but whem i try to run it got the unresolve reference composable
it does work Jeff! So happy, you made my day. Many thanks for your assistance let me buy you a drink 👏
🎉 1
you to @Qamar A. Safadi and @Morgane Soula thanks for your time 🙌
🎉 2
🙏 1
suppose upgrading graddle from 8.0 to 8.2.1 did the job.
and build.gradle is cleaner and understood (i think) many thanks @Jeff Lockhart with moko libs and sqldelight ready
j
Glad it's working for you now. It almost seems like there might have been a corrupt cache with the Gradle 8.0 version. Each Gradle version caches dependencies separately, so it would have redownloaded them all after upgrading.
🙌 1
r
yes it seems, i had issues with gradle 8.0 in the past. Didnt reached it can be upgraded in the wrapper file.
tomorrow will carry on with the project implementation, hopefully it will be easier. so glad putting this working with the assistance of you and the community. Developing for android, ios, KMM is hard but having people like you makes it more gratifying
j
Gradle configuration is one of the more challenging aspects of KMM development, for sure. Despite the occasional rough patch, it's fun to develop in Kotlin for multiple platforms and the community is great. 😊
👍 1
1
j
Nice to see this resolved. 🎉 I'd like to add that I've seen bugs fix themselves (so to say 😉) by upgrading Gradle. I'd say step one in working with KMP is upgrading the Gradle wrapper 🙂
🙌 1
1048 Views