Skip to content

Propagate desktop JVM dependencies only when invoked by Compose previews#94

Merged
paxbun merged 5 commits into
gobley:mainfrom
paxbun:fix/jna-duplicate-classes
Apr 5, 2025
Merged

Propagate desktop JVM dependencies only when invoked by Compose previews#94
paxbun merged 5 commits into
gobley:mainfrom
paxbun:fix/jna-duplicate-classes

Conversation

@paxbun
Copy link
Copy Markdown
Member

@paxbun paxbun commented Apr 3, 2025

Changes

  • Added GradleUtils.getComposePreviewVariant(), which returns a Variant when the build was invoked by Compose previews.
    • It returns Variant.Debug during IDE sync, which effectively prevents support for release mode Compose previews. An issue for this is opened at Release mode Compose previews are not supported #99.
    • It returns Variant.Release when invoked from release mode Compose previews, but since during IDE sync the JAR file for the debug build is used, the release preview tries to find the debug mode Rust dynamic library.
    • It returns null during normal builds.
  • The Rust dynamic library JAR file and the desktop JNA are used inside runtimeOnly only if GradleUtils.getComposePreviewVariant() returns Variant.Debug. This makes them removed during normal builds.
  • Made DependencyUtils.resolveUniFfiDependencies always register JNA as a dependency, as it is used inside Compose previews or unit tests so it is not included in the final app. This is to prevent resolving dependencies during configuration.

Testing

Tested with the Android tutorial added by #62. Compose test code is as follows.

Column(Modifier.safeContentPadding()) {
    val url = this::class.java.classLoader
        ?.getResource("darwin-aarch64/libapp.dylib")
        ?.toString()
    if (url == null) {
        Text("NOT FOUND")
    } else {
        Text(
            AnnotatedString.Builder().apply {
                append(url)
                val debugIdx = url.indexOf("debug.jar")
                if (debugIdx != -1) {
                    addStyle(SpanStyle(color = Color.Red), debugIdx, debugIdx + 5)
                }
            }.toAnnotatedString()
        )
    }
    Text("BuildConfig.DEBUG: ${BuildConfig.DEBUG}")
    Text("JNA Version: ${Native.VERSION}")
    Text("Addition using Rust: 2 + 3 = ${uniffi.app.add(2, 3)}")
    val greeting = remember { uniffi.app.Greeter("Hello") }
    Text(greeting.greet("Rust"))
    DisposableEffect(greeting) {
        onDispose {
            greeting.close()
        }
    }
}

image

Tested with a multi-module project as well.

image

Issues Fixed

Fixes: #92

@paxbun paxbun added bug Something isn't working / This PR fixes it gradle Related to the Gradle plugins uniffi Related to UniFFI, the bindgen, or the bindings labels Apr 3, 2025
@paxbun paxbun changed the title Fix/jna duplicate classes Propagate JNA dependencies only when invoked by Compose previews Apr 3, 2025
@paxbun paxbun force-pushed the fix/jna-duplicate-classes branch from 02ce9a0 to b972988 Compare April 4, 2025 17:16
@paxbun paxbun force-pushed the fix/jna-duplicate-classes branch from b972988 to ec8e922 Compare April 5, 2025 05:41
@paxbun paxbun changed the title Propagate JNA dependencies only when invoked by Compose previews Propagate desktop JVM dependencies only when invoked by Compose previews Apr 5, 2025
@paxbun paxbun marked this pull request as ready for review April 5, 2025 07:59
@paxbun paxbun requested a review from SalvatoreT April 5, 2025 08:04
paxbun added a commit to paxbun/gobley that referenced this pull request Apr 5, 2025
@paxbun paxbun merged commit 762083c into gobley:main Apr 5, 2025
12 checks passed
@paxbun paxbun deleted the fix/jna-duplicate-classes branch April 5, 2025 23:55
paxbun added a commit that referenced this pull request Apr 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working / This PR fixes it gradle Related to the Gradle plugins uniffi Related to UniFFI, the bindgen, or the bindings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Android debug app cannot be built due to duplicate classes from JNA

2 participants