Is it possible to load a native library in a Jetpack Compose @preview function?

Viewed 350

I've begun implementing a feature on my application with Jetpack Compose. The application uses Swig to generate code that allows me to interact with a C++ layer from Kotlin/Java. In order to use the C++ layer, I load the library like this in the app's main activity:

static {
    System.loadLibrary("{swig library name}");
}

When I attempt to use Jetpack Compose previews, I get the following render problem:

java.lang.UnsatisfiedLinkError: 'void {package containing swig's generated code}.swigWrappersJNI.swig_module_init()'

The function does use a class generated by Swig, so I'm assuming that's why this is happening.

I tried getting around this by adding the following to the preview function:

System.loadLibrary("{swig library name}")

but that just results in the following render problem:

java.lang.UnsatisfiedLinkError: no {swig library name} in java.library.path:

Does anybody know how/if I can properly link the library so that I can use Compose's preview feature as I work on this app?

Edit:

For a little more info, I've tried the following methods for loading my library:

@Composable
@Preview
fun ___Preview() {
    System.loadLibrary("{swig library name}");

    // Actual preview code here
}
object LibraryLoader {
    fun load() {
        System.loadLibrary("{swig library name}")
    }
}

@Composable
@Preview
fun ___Preview() {
    LibraryLoader.load()

    // Actual preview code here
}

(This one was a bit of a shot in the dark. I thought the key difference could've been loading it within a static block, but this had no effect.)

The previews can be successfully deployed to a device/emulator.

I think the issue here is that the library lives in lib/{abi}/{swig library name}.so, so it exists when running on a device/emulator (with a supported ABI) but not for Compose previews.

What I've done to work around it is create a copy of my model classes that don't use any of Swig's generated code so I can take advantage of previews when creating the layout, then replacing them with the actual model classes for testing functionality. Definitely not a solution I'm particularly happy with but it may be the only way to use Compose previews in this type of situation.

0 Answers
Related