Search code examples
javaandroidpdfjava-native-interfacepdftron

Error trying to loadLibrary for PDFTron Android


I am getting a strange error when trying to integrate with PDFTron i.e PDFNet Android SDK:

UnsatisfiedLinkError: Couldn't load PDFNetC: findLibrary returned null. Expected armv7a, found cpu_info: abi: armeabi-v7a

I have already checked this link.

Most likely the native library provided by PDFNet Android SDK is not included in my project correctly. Inside the lib folder I the downloaded package, there are native libraries and java library that both need to be included to my project.

Created a folder called jniLibs in main folder and place all .so (with corresponding folder structure) inside the jniLibs folder, i.e.:

enter image description here

This will allow Android Studio to automatically recognize the native library path.

I also specified product flavor to filter which .so to use, such as:

productFlavors {
    armv7a {
        ndk {
            abiFilters "armeabi-v7a"
        }
    }
    arm {
        ndk {
            abiFilters "armeabi"
        }
    }
    x86 {
        ndk {
            abiFilters "x86"
        }
    }
    armv8 {
        ndk {
            abiFilters "arm64-v8a"
        }
    }
    x86_64 {
        ndk {
            abiFilters "x86_64"
        }
    }
    fat {
        ndk {
            abiFilters "armeabi-v7a", "armeabi", "arm64-v8a", "x86", "x86_64"
        }
    }
}

Then I specified the jniLibs directory inside build.gradle file of your project, i.e.:

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        resources.srcDirs = ['src']
        res.srcDirs = ['res']
        jniLibs.srcDirs = ['libs']
        svg.srcDir 'src/main/svg'
    }
}

Solution

  • Import PDFTron Library Project in Android Studio. Use that Library Project in your application.For this use compile project(':libraries:PDFViewCtrlTools') in your application's build.gradle.

    Now copy libPDFNetC.so file to "jni/libs/armeabi" folder in your application.

    in your application.mk write this.

    APP_ABI := armeabi-v7a
    APP_CPPFLAGS += -std=c++11 -exception
    APP_STL := gnustl_shared
    APP_PLATFORM=android-19
    APP_OPTIM := debug
    NDK_TOOLCHAIN_VERSION := 4.8
    

    in your Android.mk file add this

    include $(CLEAR_VARS)
    LOCAL_MODULE    := libPDFNetC
    # this libs path is relative to my jni files, so, src/main/jni/libs/libPrecompiledLib.a
    LOCAL_SRC_FILES := libs/armeabi/libPDFNetC.so
    include $(PREBUILT_SHARED_LIBRARY)
    

    Edit:

    PDFDoc doc = PDFDoc(InputStream var1)
    

    may be by using this method you can open PDF from url.

    Code:

    PDFViewCtrl mPDFViewCtrl = (PDFViewCtrl) view.findViewById(R.id.pdfViewer);
    PDFDoc doc = new PDFDoc(filePath);
    mPDFViewCtrl.setDoc(doc);
    

    xml

    <pdftron.PDF.PDFViewCtrl
        android:id="@+id/pdfViewer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical|horizontal"
        android:visibility="gone"/>