Skip to content

Load enc/dec native libraries only if needed to allow exclusion from packaging #36

@vfsfitvnm

Description

@vfsfitvnm

After including com.ensody.kompressor:kompressor-brotli-ktor the release APK size of my app went from 3.1 MB to 7.4 MB 🥲

The most impacting thing is libbrotlienc.so (3 MB, literally as big as a whole music streaming application), which I don't actually need, as I only need Brotli to decompress payloads returned by the server.

So, I simply attempted to strip it off from the APK:

android {
    packaging {
        resources.excludes.add("META-INF/nativebuild.json") // also needed but irrelevant for this issue
        jniLibs.excludes.add("lib/arm64-v8a/libbrotlienc.so")
    }
}

But this caused the following error:

Rejecting re-init on previously-failed class java.lang.Class<com.ensody.kompressor.brotli.BrotliWrapper>: java.lang.UnsatisfiedLinkError: dlopen failed: library "libbrotlienc.so" not found
  (removed irrelevant lines)
  at void com.ensody.nativebuilds.loader.NativeBuildsJvmLoader.load(com.ensody.nativebuilds.loader.NativeBuildsJvmLib) (NativeBuildsJvmLoader.kt:46)
  at void com.ensody.kompressor.brotli.BrotliWrapper.<clinit>() (BrotliWrapper.kt:24)
  at void com.ensody.kompressor.brotli.BrotliDecompressorImpl.<init>() (BrotliDecompressor.jvm.kt:11)
  at com.ensody.kompressor.core.SliceTransform com.ensody.kompressor.brotli.BrotliDecompressor_jvmKt.BrotliDecompressor() (BrotliDecompressor.jvm.kt:8)

In other words, BrotliDecompressor explicitly loads libbrotlienc.so, which not needed.
Also confirmed by code:

init {
NativeBuildsJvmLoader.load(NativeBuildsJvmLibBrotlicommon)
NativeBuildsJvmLoader.load(NativeBuildsJvmLibBrotlidec)
NativeBuildsJvmLoader.load(NativeBuildsJvmLibBrotlienc)
NativeBuildsJvmLoader.load(this)
}

I would really like to not include a native library I'm not using. Would be possible to load native libraries only if needed?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions