4

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

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62

3 Answers3

3

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)'.

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

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62
1

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")
    }
}
Zach Sao
  • 436
  • 4
  • 14
  • This does not work, please read the description of the question where I mention that _The only workaround at the moment for KMM to work is use Android Studio Beta (but this is not compatible with Jetpack Compose):_ https://stackoverflow.com/a/66433024/5279996 – Braian Coronel Mar 26 '21 at 23:10
  • You must use Android studio Canary – Zach Sao Mar 26 '21 at 23:14
  • Canary should be used for Compose compatibility, but as there is a bug in KMM this is not enough. – Braian Coronel Mar 27 '21 at 02:15
  • Exactly, this block should fix the KMM bug in AS Canary. I've ran into the issue myself and that's how I got to run my KMM app with compose. Also you must make sure that your Android Gradle Plugin is at the latest version (7.0-alphaxx) . – Zach Sao Apr 03 '21 at 01:12
  • Yes, this block fixes the following error `Configuration with name 'testApi' not found` when the project has only kmm and compose. But in a real project when these technologies are integrated, this answer might not be enough for example if we use custom plugins with AGP 4.0 (which generates another bug) – Braian Coronel Apr 03 '21 at 03:31
0

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

JavierSegoviaCordoba
  • 6,531
  • 9
  • 37
  • 51