Elye
Elye

Reputation: 60141

For JetpackCompose, can we create our Composable Function in another library?

I'm trying to separate out some Jetpack Compose functions into another library.

On my App Module, I have

setContent {
    MyApplicationTheme {
        Surface(color = MaterialTheme.colors.background) {
            Greeting()
        }
    }
}

However, composable function MyApplicationTheme comes from another library

@Composable
fun MyApplicationTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable() () -> Unit) { ... }

enter image description here

It compiles fine. But when I run and reach the function triggering MyApplicationTheme, it crashes state

    java.lang.NoSuchMethodError: No static method MyApplicationTheme(ZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V in class Lcom/example/based_lib/theme/ThemeKt; or its super classes (declaration of 'com.example.based_lib.theme.ThemeKt' appears in /data/data/com.example.myapplication/code_cache/.overlay/base.apk/classes2.dex)

I have a sample design demonstrate the crash... (just need to click the button into the Jetpack Compose activity, it will crash)

https://github.com/elye/issue_android_jetpack_compose_another_library_crash

If I move the MyApplicationTheme from the BasedLib to the Main App, then all compile fine.

Is having a Composable Function in another library not supported, or I need to do something in the library to have the Composable Function usable by another library?

Upvotes: 3

Views: 1418

Answers (1)

hardartcore
hardartcore

Reputation: 17037

Add this lines to your build.gradle file which is in based-lib module:

buildFeatures {
    compose true
}
composeOptions {
    kotlinCompilerExtensionVersion compose_version
    kotlinCompilerVersion '1.5.10'
}

Upvotes: 4

Related Questions