kotlin androidpdfviewer lib doesn't seem to load

4.1k Views Asked by At

I am working with android pdfviewer lib to open and read a pdf, found at : https://github.com/barteksc/AndroidPdfViewer

But i got an error when i try to launch the pdf :

E/zygote64: 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)

E/PDFView: load pdf error 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)

i tried with differents implementation of the dependency but none worked :

implementation 'com.github.barteksc:pdfium-android:1.9.0'
implementation "com.github.barteksc:android-pdf-viewer:3.2.0-beta.1"
implementation "com.github.barteksc:android-pdf-viewer:2.8.2"

The error is found here :

public PdfDocument newDocument(ParcelFileDescriptor fd, String password) throws IOException {
        PdfDocument document = new PdfDocument();
        document.parcelFileDescriptor = fd;
        synchronized (lock) {
            document.mNativeDocPtr = nativeOpenDocument(getNumFd(fd), password);
        }

        return document;
    }

The fonction nativeOpenDocument from the lib doesn't seem to load.

I found some topic on github talking about it : https://github.com/barteksc/AndroidPdfViewer/issues/538 https://github.com/barteksc/PdfiumAndroid/issues/54 https://github.com/mshockwave/PdfiumAndroid/issues/13

But no solution found, as suggested i tried to change the dependency, tried to shut down my computer and my phone, tried to invalide cache and restart, tried on emulator but nothing work.

It would be very nice if someone could help me with that?

2

There are 2 best solutions below

0
On BEST ANSWER

I just found out that in my case i had an app and a module where is my own library aar where i use the androidpdfviewer.

Probleme was that i had the dependency only on the lib and not the app.

To solve the probleme i had to add the dependency in both the app and the library. It's seem obvious now that i see that i have 2 build.gradle. Now it's working fine.

Add classpath 'com.github.barteksc:pdfium-android:1.9.0' in you build.gradle but the project not the module.

0
On

This is how I'm loading pdf url:

fragment_pdf_reader.xml:

<androidx.constraintlayout.widget.ConstraintLayout
//....
<com.github.barteksc.pdfviewer.PDFView
            android:id="@+id/pdfView"
            android:layout_width="match_parent"
            android:layout_height="@dimen/zeroDp"
            android:visibility="visible"
            app:layout_constraintBottom_toTopOf="@+id/tvFeedback"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view" />
//...
</androidx.constraintlayout.widget.ConstraintLayout>

Now inside your fragment get pdf url and make Http request with that pdf url and get ResponseBody pass it to the below method:

private fun downloadFile(responseBody: ResponseBody) {// get responseBody after making Http req . I'm using retrofit 
        binding?.let {
            //it.pdfView.removeAllViews()
            it.pdfView.fromBytes(responseBody.bytes())
                    .defaultPage(pageNumber)
                    .onPageChange(this@PdfReaderFragment)
                    .enableAnnotationRendering(true)
                    .onLoad(this@PdfReaderFragment)
                    .spacing(10) // in dp
                    .onPageError(this@PdfReaderFragment)
                    .onError { onError("File not in PDF format or corrupted") }
                    .load()
        }


    }

Implement these call backs :

OnPageChangeListener
OnLoadCompleteListener
OnPageErrorListener

implementation of above call backs are:

override fun loadComplete(nbPages: Int) {
        binding?.let {
            printBookmarksTree(it.pdfView.tableOfContents, "-")
        }

    }

override fun onPageError(page: Int, t: Throwable?) {
        Log.d("PDF", t?.message)
    }
override fun onPageChanged(page: Int, pageCount: Int) {
        pageNumber = page
        Log.d("PDF", "Page number $pageNumber $pageCount")
        setTitleMessageBackArrow(String.format("%s %s / %s", "your fragment title", page + 1, pageCount))
    }

    private fun printBookmarksTree(tree: List<Bookmark>, sep: String) {
        for (b in tree) {
            Log.e("PDF", String.format("%s %s, p %d", sep, b.title, b.pageIdx))
            if (b.hasChildren()) {
                printBookmarksTree(b.children, "$sep-")
            }
        }
    }