简体   繁体   中英

Could Kotlin Multiplatform Mobile work with Jetpack Compose?

There are 2 problems that do not allow to generate compatibility between these technologies

Jetpack Compose

The first problem is that it only works with Android Studio Canary x.

  • Android Studio 4.2 Beta 6

Jetpack Compose is a preview feature, and support for Compose is included only in Canary versions of Android Studio. To use Compose in your app project, download and install the latest Canary version of the IDE.

Jetpack Compose works with:

  • Android Studio Canary
  • Kotlin 1.4.30
  • distributionUrl: 6.8.2
  • AGP: 7.0.0-alpha11
  • JavaVersion.VERSION_11

Kotlin Multiplatform Mobile

And the second problem is that Android Studio Canary builds have a bug in Kotlin Multiplatform Mobile. I am using the following:

  • Android Studio Arctic Fox | 2020.3.1 Canary 11
Android Studio Arctic Fox | 2020.3.1 Canary 11
Build #AI-203.7148.57.2031.7209405, built on March 15, 2021
Runtime version: 11.0.8+0-b944-P17168821 amd64
VM: OpenJDK 64-Bit Server VM by N/A
Current Desktop: ubuntu:GNOME
  • In gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-all.zip
  • In build.gradle (Project)
buildscript {
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.0-alpha11'
    }
}
  • JavaVersion.VERSION_11

Error ( issue/KT-43944 ):

A problem occurred configuring project ':kmm_shared'.
> Configuration with name 'testApi' not found.
* Exception is:
Caused by: org.gradle.api.artifacts.UnknownConfigurationException: Configuration with name 'testApi' not found.

The only workaround at the moment for KMM to work is use Android Studio Beta (but this is not compatible with Jetpack Compose).

Install Android Studio – version 4.2 or higher.

Given this there would be 2 solutions that I could not face. Have Compose work on the Android Studio Beta build or fix the KMM bug in Android Studio Canary.

It should be noted that KMM and Jetpack Compose use different versions of AGP and gradle distributions. Then it occurs to me, I don't know if it is possible, that deferred versions coexist in the same project.

Any suggestion is welcome.

Thanks

Compatibility between technologies exists. I was able to debug the error in my project thanks to the tests I did in MortyComposeKMM .


The workaround for issue/KT-43944 was not working for me because I had in buildSrc a dependency of gradle in 4.1.2 that generated an error of the type Failed to notify build listener. > Please initialize at least one Kotlin target in 'x (:x)'. Failed to notify build listener. > Please initialize at least one Kotlin target in 'x (:x)'.

Solution:

build.gradle.kts (:buildSrc)

dependencies {
    implementation("com.android.tools.build:gradle:7.0.0-alpha11")
    implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31")
}
  • Use JavaVersion.VERSION_11

Tweet

As suggested in https://youtrack.jetbrains.com/issue/KT-43944 you must add the following block in the build.gradle.kts(:shared) as a workaround:

android {
    configurations {
        create("androidTestApi")
        create("androidTestDebugApi")
        create("androidTestReleaseApi")
        create("testApi")
        create("testDebugApi")
        create("testReleaseApi")
    }
}

You can even use Android Studio 4.0.1 to work with Compose in Multiplatform. The only problem is you lose a few things like Preview.

Check the examples from JetBrains Compose Desktop repo:

https://github.com/JetBrains/compose-jb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM