tauri icon indicating copy to clipboard operation
tauri copied to clipboard

docs(android): add dynamic library name guide for plugins (fix #14569)

Open Subham-KRLX opened this issue 3 weeks ago • 10 comments

Closes #14569

Documents how to properly load native libraries in Android plugins with dynamic naming instead of hardcoding System.loadLibrary("app_lib").

Adds guide explaining:

  • Library naming convention from Cargo.toml
  • Portable code pattern using constants
  • Error handling and quick reference

Reference for updating tauri-apps/tauri-docs.

Subham-KRLX avatar Nov 29 '25 10:11 Subham-KRLX

@IT-ess ,@FabianLars can you please review this.

Subham-KRLX avatar Nov 29 '25 17:11 Subham-KRLX

@IT-ess ,@cntrvsy can you please review this. Wish I could, try reaching out on the discord https://discord.gg/tauri

cntrvsy avatar Nov 29 '25 17:11 cntrvsy

Thanks for the PR ! This is a first step, but this doc will mainly be useful for the users of plugin that do need Rust lib linking. The actual issue is for plugin developers to make the lib linking dynamic, and that part should be documented instead. This kind of knowledge would allow plugin devs to make more flexible plugins while keeping them "plug and play". I don't know if this requires changes in Tauri's code, or if the solution is already out there but I just don't know how to do it.

IT-ess avatar Nov 29 '25 17:11 IT-ess

Thanks for the feedback @IT-ess! To clarify the scope: should this PR document how plugin developers can implement automatic library loading (so users don't need to configure anything), or should it stay focused on helping plugin users adopt better practices? If it's the former, does Tauri already support automatic library name detection, or would that require code changes? Happy to research and expand the PR accordingly.

Subham-KRLX avatar Nov 29 '25 17:11 Subham-KRLX

It is the former, and I don't know if that requires modifications in the code. I guess it does need some, otherwise it would probably have been documented either

IT-ess avatar Nov 29 '25 17:11 IT-ess

@IT-ess Found the solution! Plugin devs can auto-generate the library name using Gradle BuildConfig:

In android/build.gradle.kts:

android {
    defaultConfig {
        // Read from Cargo.toml at build time
        val cargoToml = file("../../Cargo.toml")
        val libraryName = cargoToml.readText()
            .lines()
            .find { it.trim().startsWith("name") }
            ?.substringAfter("\"")?.substringBefore("\"")
            ?.replace("-", "_") ?: "unknown"
        
        buildConfigField("String", "LIBRARY_NAME", "\"$libraryName\"")
    }
    buildFeatures { buildConfig = true }
}
In Example.kt:

kotlin
companion object {
    init {
        System.loadLibrary(BuildConfig.LIBRARY_NAME) // Auto-generated!
    }
}

Subham-KRLX avatar Nov 30 '25 09:11 Subham-KRLX

I'm not sure but the config should be added to the build.gradle.kts of the plugin user, not the plugin itself ? :thinking: I'll give it a try later, I don't have my computer with me right now

IT-ess avatar Nov 30 '25 10:11 IT-ess

@IT-ess It goes in the plugin's build.gradle.kts not the user's app.

Flow:

  1. Plugin developer (you) adds BuildConfig to your plugin's android/build.gradle.kts once
  2. Plugin users just install your plugin - no config needed ✅

Structure: tauri-plugin-yourplugin/ └── android/ ├── build.gradle.kts ← BuildConfig HERE (plugin dev sets up once) └── src/.../Example.kt ← Uses BuildConfig.LIBRARY_NAME

user-app/ └── (just imports plugin, no changes needed)

This makes your plugin automatically load the correct library so users don't have to configure anything. Try it out when you get a chance!

Subham-KRLX avatar Nov 30 '25 11:11 Subham-KRLX

I just tested that, and it doesn't work at all :face_with_diagonal_mouth:

IT-ess avatar Dec 01 '25 07:12 IT-ess

@IT-ess Could you share what error you got when testing the BuildConfig approach? That will help me find the right solution.

Subham-KRLX avatar Dec 02 '25 12:12 Subham-KRLX