AndroidPdfViewer icon indicating copy to clipboard operation
AndroidPdfViewer copied to clipboard

UnsatisfiedLinkError with PDFium on Android 15 Emulator with 16 KB Page Size

Open yd-threra-and opened this issue 1 year ago • 28 comments

Environment

  • Library version: 3.2.0-beta.1
  • Android version: 15 (Vanilla Ice Cream)
  • Emulator: 16 KB page size

Steps to Reproduce

  1. Create an Android emulator with 16 KB page size following the steps in the Android Developers Guide.
  2. Use the PDF Viewer library to load a PDF document with the following code from some fragment from our demo test app:
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val outputFile = File(requireContext().filesDir, "sample.pdf")
        if (!outputFile.exists()) {
            try {
                val inputStream: InputStream = requireContext().assets.open("sample.pdf")
                val outputStream = FileOutputStream(outputFile)
                val buffer = ByteArray(1024)
                var length: Int
                while (inputStream.read(buffer).also { length = it } > 0) {
                    outputStream.write(buffer, 0, length)
                }
                outputStream.close()
                inputStream.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    
        binding.pdfView.fromFile(outputFile)
            .enableSwipe(true)
            .swipeHorizontal(false)
            .enableDoubletap(true)
            .defaultPage(0)
            .onLoad { nbPages -> /* Do something on load complete */ }
            .onPageError { page, t -> /* Handle page error */ }
            .load()
    }
    
  3. Observe the error.

Error Log

java.lang.UnsatisfiedLinkError: No implementation found for long com.shockwave.pdfium.PdfiumCore.nativeOpenDocument(int, java.lang.String) (tried Java_com_shockwave_pdfium_PdfiumCore_nativeOpenDocument and Java_com_shockwave_pdfium_PdfiumCore_nativeOpenDocument__ILjava_lang_String_2) - is the library loaded, e.g. System.loadLibrary? at com.shockwave.pdfium.PdfiumCore.nativeOpenDocument(Native Method) at com.shockwave.pdfium.PdfiumCore.newDocument(PdfiumCore.java:135) at com.github.barteksc.pdfviewer.source.FileSource.createDocument(FileSource.java:38) at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:53) at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:27) at android.os.AsyncTask$3.call(AsyncTask.java:394) at java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644) at java.lang.Thread.run(Thread.java:1012)

Additional Context

This issue affects applications running on Android 15 with 16 KB page size, not limited to those targeting Android 15. The issue is critical as it impacts our project's ability to function correctly on these environments.

Request

We would appreciate any guidance on resolving this issue or information on upcoming fixes. We are willing to assist with testing or contributing to a solution if needed.

Thank you for your attention to this matter.

yd-threra-and avatar Jun 24 '24 22:06 yd-threra-and

Just a heads-up that this fork (of a fork (of a fork)) seems to add 16KB support. Hopefully it'll eventually get merged in, and actually be usable!

JakeSteam avatar May 09 '25 19:05 JakeSteam

@JakeSteam Thanks for the heads-up about the fork that addresses the 16KB page size alignment issue.

Do you know if there's an estimated timeline for merging this fix into this repository? Alternatively, is the fork stable enough to consider temporarily integrating into our project?

yd-threra-and avatar May 20 '25 08:05 yd-threra-and

@yd-threra-and I ended up forking lion1988dev's AndroidPdfViewer, and updating the dependency to v-mas' PdfiumAndroid fork with 16KB support, and it works well in my project.

I ultimately decided that depending on all these forks of forks from abandoned GitHub accounts was too important for a mission critical component of our app (PDF viewing), so created a private (company-owned, sorry!) repo with them both. This is then included as a submodule, to ensure it won't unexpectedly disappear. This solution seems the safest!

If you're at all able, I highly recommend moving to one of the actually maintained image-based PDF renderers. I'm stuck with this one because I require clickable links in the PDF, and require embedding within Composables (e.g. not a standalone fragment).

JakeSteam avatar May 20 '25 10:05 JakeSteam

@JakeSteam, Thanks a lot for the detailed info - much appreciated!

Just to clarify a few things based on your solution:

  • Did you fork both AndroidPdfViewer and PdfiumAndroid into your organization/private repo?

  • Any tips on linking the two cleanly inside the project? (Besides using Git submodules — wondering if you explored AARs or other strategies.)

  • You mentioned "actually maintained image-based PDF renderers" as an alternative — could you share any specific libraries you’ve tried or recommend? We don’t require clickable links, so switching to an image-based viewer could work for us.

(We're just using AndroidPdfViewer for basic zoomable viewing in fragments, no clickable links or Compose integration.)

Thanks again — this is super helpful!

yd-threra-and avatar May 21 '25 12:05 yd-threra-and

So, I used a single repo with a AndroidPdfViewer module, which then depended on a PdfiumAndroid module I just downloaded from the relevant 16KB fork. The overall repo was then included as a git submodule in the actual app, which is also under my company's organisation, and the AndroidPdfViewer module depended on (which then depended on PdfiumAndroid).

However, I later decided to instead use created .aar files (to avoid rebuilds) as you mentioned, and haven't settled on any process better than "uncomment the dependency, change the submodule, republish the .aars, and copy them across" yet! I'm optimistic I'll probably never manually change these files, except to replace the whole thing with a maintained fork one day.

If you don't need clickable links, there are MUCH better options available. Here's the verbatim notes from my little investigation a few weeks ago, I integrated most and Pdf-Viewer & Bouquet were my favourites:

Alternative libraries

Whilst there are lots of libraries, they almost all use android.graphics.pdf under the hood which renders PDF pages into images, and loses the links:

  • AndroidX: Only supports Android versions STUV, and renders as images, is alpha.
  • Bouquet: Built well, but renders as images and overrides any tap events.
  • Pdf-Viewer: Lightweight, but renders as images.
  • PdfBox: Slow, ported library. Renders as images but provides access to (some) underlying data.
  • Nutrient: A (paid & expensive!) library that is massively overkill, and lacking info on linking.
  • Rendering PDFs in a webview: May be viable with "pdf.js", it looks pretty awkward though.

Hope that helps!

JakeSteam avatar May 21 '25 12:05 JakeSteam

@JakeSteam Thanks so much for the detailed explanation and for sharing your notes on the alternative libraries - that’s incredibly helpful!

I really appreciate you taking the time to outline everything so clearly. I may reach out again down the line if I have any follow-up questions.

Thanks again!

yd-threra-and avatar May 22 '25 15:05 yd-threra-and

I've updated PDFiumAndroid project to support 16KB memory page sizes by upgrading the Gradle plugin and NDK. This is intended as a temporary solution to help developers prepare for Android 15/16.

Check out the sample (3.2.6) : https://github.com/marain87/AndroidPdfViewer

marain87 avatar Jun 03 '25 09:06 marain87

Hi @marain87, it seems that https://github.com/marain87/AndroidPdfViewer 3.2.6 is currently broken due to its dependency on com.github.marain87:PdfiumAndroid:16KB-SNAPSHOT, which can not be found. Would it be possible to either:

  • Add 16KB support to the master branch of PdfiumAndroid and publish a stable release
  • Restore or re-publish the missing 16KB-SNAPSHOT version?

This would help unblock anyone depending on 3.2.6. Thanks in advance for your help!

Leedwon avatar Jul 07 '25 10:07 Leedwon

Hi @Leedwon , it seems there's an issue with accessing the PdfiumAndroid JAR files on JitPack, as they appear to have been removed.

To address this, I've merged the '16KB' branch into the master and published a new version of PdfiumAndroid (v1.9.7). Additionally, there is a new version of AndroidPdfViewer available (v3.2.7).

I would also suggest creating a fork of the repository as your own backup.

marain87 avatar Jul 07 '25 13:07 marain87

Thanks for the help it's working now

Leedwon avatar Jul 07 '25 14:07 Leedwon

To address this, I've merged the '16KB' branch into the master and published a new version of PdfiumAndroid (v1.9.7). Additionally, there is a new version of AndroidPdfViewer available (v3.2.7).

HI @marain87 it seems like I am unable to import the 3.2.7 version... I can only import until version 3.2.4. Could you please check out the Jitpack.

subhajit-rajak avatar Jul 12 '25 10:07 subhajit-rajak

@subhajit-rajak I think you are using the library that I forked from, please check the implementation started with 'com.github.marain87'

marain87 avatar Jul 12 '25 13:07 marain87

Thank you for pointing that out @marain87 . Can you do me a little favor and attach the entire dependency in your next reply. I couldn't find it anywhere. It will be helpful to others as well. Thank you

subhajit-rajak avatar Jul 12 '25 13:07 subhajit-rajak

Hey @marain87 Still getting two .so files ( libpdfium.cr.so and libpdfiumandroid.so )

sandipkush10 avatar Jul 13 '25 01:07 sandipkush10

@subhajit-rajak The version you should use 'com.github.marain87:AndroidPdfViewer:3.2.7', which is a fork of the project. If 'lion1988dev' approves and merges the changes, there is a possibility that it will change to archived. I would highly recommend that you create a local backup of the version you are using to make sure you have continued access to the code.

@sandipkush10 Are you using the correct dependency? arm64-v8a/x86_64 libs are aligned. (only arm64-v8a/x86_64 libs need to be aligned for 16KB).

Image

marain87 avatar Jul 14 '25 02:07 marain87

Thank you @marain87.

subhajit-rajak avatar Jul 14 '25 02:07 subhajit-rajak

@marain87 Yes I am using correct implementation('com.github.marain87:AndroidPdfViewer:3.2.7') dependency with maven { url 'https://jitpack.io' }

Image

sandipkush10 avatar Jul 14 '25 05:07 sandipkush10

@sandipkush10 Ref to https://developer.android.com/guide/practices/page-sizes?authuser=3#identify-native-code Regarding the 16KB alignment, the "No" in the "Compressed" column of the documentation is not indicative of the alignment issue.

I've used the elf_alignment_test script, as suggested in the Android developer guide, to verify this in my project in the previous reply. You can find the script and instructions here: https://developer.android.com/guide/practices/page-sizes?authuser=3#alignment-use-script

If you still have any issues with it, you may try another fork project. ref to #1229

marain87 avatar Jul 14 '25 06:07 marain87

Thank you @marain87. your dependency implementation('com.github.marain87:AndroidPdfViewer:3.2.7') is supporting 16KB pages.

sandipkush10 avatar Jul 14 '25 07:07 sandipkush10

@marain87 Yes I am using correct implementation('com.github.marain87:AndroidPdfViewer:3.2.7') dependency with maven { url 'https://jitpack.io' } Image

Can you send me the so files for pdfium that I use search and I did not find the size 16kb only 4kb

Maromax-4002 avatar Sep 10 '25 13:09 Maromax-4002

@marain87 : I have successfully implemented this dependency implementation('com.github.marain87:AndroidPdfViewer:3.2.7') which help to make supporting 16KB pages.

Thank you @marain87 it was very helpful.

panky1 avatar Sep 11 '25 15:09 panky1

Hi guys ! Can someone please give me a more deep of explanation on how to replace this 'com.github.mhiew:android-pdf-viewer:3.2.0-beta.3' with a working one which is compatible with the google 16KB policy. Cheers!

slyvelikov avatar Oct 02 '25 14:10 slyvelikov

Hi @slyvelikov You just need to remove the existing dependency com.github.mhiew:android-pdf-viewer:3.2.0-beta.3 and replace it with com.github.marain87:AndroidPdfViewer:3.2.7.

anishsonismtlabs-max avatar Oct 04 '25 07:10 anishsonismtlabs-max

@marain87 Thanks! You are a saver!

achatina avatar Oct 08 '25 10:10 achatina

Any ideas why it would still show the warning (but just at the top) when all other warnings are gone? I have switched to com.github.marain87:AndroidPdfViewer:3.2.8.

Also, to be clear, when I comment out the dependency, the warning is gone (along with the lib folder from the analyzer report, which is expected).

Image

Thanks in advance! And thank you very much for your contributions @marain87!

imilakovic avatar Oct 26 '25 03:10 imilakovic

Hi @imilakovic Based on your other post, it seems your project has other .so files. It's possible that one of these files doesn't support a 16KB page size. You can try using the command-line script from the Android documentation to check the .so files in your project.

Ref: https://developer.android.com/guide/practices/page-sizes?hl=en#elf-alignment

marain87 avatar Oct 27 '25 04:10 marain87

Hi @imilakovic Based on your other post, it seems your project has other .so files. It's possible that one of these files doesn't support a 16KB page size. You can try using the command-line script from the Android documentation to check the .so files in your project.

Ref: https://developer.android.com/guide/practices/page-sizes?hl=en#elf-alignment

Hey @marain87, thank you for your reply! I don't think that's the case, because when I comment it out, all .so files are gone.

Also, I've tried two other libraries - io.github.oothp:android-pdf-viewer:3.2.0-beta05, which had even more .so files (with warning just like in the screenshot above) and then io.github.ahmerafzal1:ahmer-pdfviewer:2.0.1, which had a couple .so files, but those were showing up as "Aligned for 16 kb page size". I'd gladly use the latter, but it forced me to bump the compile/target SDK to 36 and minSDK to 24.

imilakovic avatar Oct 27 '25 05:10 imilakovic

@marain87 I'm so grateful that you saved my project!!!

weihsu0928 avatar Oct 28 '25 06:10 weihsu0928