简体   繁体   中英

Shared Activity with Compose in Kotlin Multiplatform project

I have a problem with setting up an activity that resides in shared code in a KMM project and uses Jetpack Compose. When the activity is in Android source (/app) this works fine. But I'd like to publish a shared (/common) library without any application.

As an example I forked a great minimal KMM+Compose project: https://github.com/cyberhenoch/PeopleInSpace :

  • added the compose activity (SharedActivity) to the shared code
  • updated the dependencies in shared code androidMain

It compiles fine but at runtime I get:

Process: com.surrus.peopleinspace, PID: 4067 java.lang.NoSuchMethodError: No static method setContent $default(Landroidx/activity/ComponentActivity;Landroidx/compose/runtime/CompositionContext;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)V in class Landroidx/activity/compose/ComponentActivityKt; or its super classes (declaration of 'androidx.activity.compose.ComponentActivityKt' appears in /data/app/com.surrus.peopleinspace-8P35Dcwzn9_G-xN9YdwFrA==/base.apk) at com.surrus.android.SharedActivity.onCreate(SharedActivity.kt:12)

I tried to add the options described here https://developer.android.com/jetpack/compose/setup#configure_gradle but

  • not all can be added to the common module (kotlinOptions?)
  • in the original project they are not required and it works perfectly good
  • after adding several of them in different combinations it does not even compile ( exception: java.lang.NoClassDefFoundError: org/jetbrains/kotlin/serialization/DescriptorSerializerPlugin at java.base/java.lang.ClassLoader.defineClass1(Native Method) )

What should I do to make it work? Any help would be appreciated.

The change required is adding org.jetbrains.compose plugin.

Basing on IDEA (not Android Studio) template for creating multiplatform project with Compose Jetpack Compose for Desktop (Experimental) -> Mutiplatform uses Kotlin 1.4.30

Project build.gradle.kts

buildscript {
    repositories {
        gradlePluginPortal()
    }
}

...

allprojects{
    ...
    maven { url = uri("https://maven.pkg.jetbrains.space/public/p/compose/dev") }
}

Common module build.gradle.kts

plugins{
    ...
    id("org.jetbrains.compose") version "0.3.1"
}

Apart from that after adding the plugin Android Studio messed up the modules showing "no module selected" error and the issue described in this question . But opening it in IDEA showed an option to fix multimodule gradle project. I fixed it this way and reopened in Android Studio.

The changes required can be seen in the example repo .

AS version 2020.3.1 Canary 15 & IDEA version Community 2021.1

ThanksJohn O'Reilly !

I have a problem with setting up an activity that resides in shared code in a KMM project and uses Jetpack Compose. When the activity is in Android source (/app) this works fine. But I'd like to publish a shared (/common) library without any application.

As an example I forked a great minimal KMM+Compose project: https://github.com/cyberhenoch/PeopleInSpace :

  • added the compose activity (SharedActivity) to the shared code
  • updated the dependencies in shared code androidMain

It compiles fine but at runtime I get:

Process: com.surrus.peopleinspace, PID: 4067 java.lang.NoSuchMethodError: No static method setContent $default(Landroidx/activity/ComponentActivity;Landroidx/compose/runtime/CompositionContext;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)V in class Landroidx/activity/compose/ComponentActivityKt; or its super classes (declaration of 'androidx.activity.compose.ComponentActivityKt' appears in /data/app/com.surrus.peopleinspace-8P35Dcwzn9_G-xN9YdwFrA==/base.apk) at com.surrus.android.SharedActivity.onCreate(SharedActivity.kt:12)

I tried to add the options described here https://developer.android.com/jetpack/compose/setup#configure_gradle but

  • not all can be added to the common module (kotlinOptions?)
  • in the original project they are not required and it works perfectly good
  • after adding several of them in different combinations it does not even compile ( exception: java.lang.NoClassDefFoundError: org/jetbrains/kotlin/serialization/DescriptorSerializerPlugin at java.base/java.lang.ClassLoader.defineClass1(Native Method) )

What should I do to make it work? Any help would be appreciated.

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