Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 926
Elye Avatar asked May 06 '26 23:05

Elye


1 Answers

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'
}
like image 86
hardartcore Avatar answered May 09 '26 16:05

hardartcore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!