Skip to content

Embedding Native Libraries

Linking a native library lets the compiler and linker resolve it at build time, but the library’s .so also needs to be present in the resulting archive for the app to run. This is what embedding does — see Linking Native Libraries for the corresponding build-time setup.

scd has builtin support for embedding libraries into resulting archives. When it embeds a library, it places it in the location the platform expects for native libraries, so it’s found at runtime automatically — no further setup is needed. The option used to embed a library can be repeated to embed multiple libraries, and combined with platform-specific variants to embed different libraries per platform.

scd supports embedding any .so file into the resulting archive for a given platform with --embed-library:<platform>, regardless of how — or whether — the library was linked at build time. This covers libraries linked manually with -Xcc/-Xlinker flags, as well as libraries loaded dynamically at runtime that don’t need build-time linking at all:

Terminal window
scd archive --platform android-arm64-v8a --embed-library:android-arm64-v8a ./MyDependency/lib/libfoo.so

When a native library is linked as an XCFramework binary target, scd detects its .so automatically and embeds it into the resulting archive — no extra flags are needed.

When a native library is linked from an AAR archive, only the options required at link time are set up automatically. To embed the library’s .so into the resulting archive, pass it explicitly with --embed-library:<platform>. --embed-library expects a path to the .so file itself, not the .aar — extract it from the AAR’s jni/<abi>/ directory first:

Terminal window
scd archive --platform android-arm64-v8a \
--link-library ./MyDependency.aar \
--embed-library:android-arm64-v8a ./MyDependency/jni/arm64-v8a/libfoo.so

See SPM Build Options and Libraries Options for the full set of related options.