andob / android-awt

Combination of code from Apache Harmony and Apache Commons Imaging to replace classes in java.awt for Android.
Apache License 2.0
28 stars 15 forks source link

Crash on document.add(image) on android #2

Closed omkar-tenkale closed 3 years ago

omkar-tenkale commented 4 years ago

While using OpenPDF in android

Theres this error occuring when converting images to pdf as follows;

     File file = new File(destinationDirectory, pdfName + ".pdf");
        Log.e("PDF","Output to:"+file.toString());

        FileOutputStream fileOutputStream = new FileOutputStream(file);

        PdfWriter pdfWriter = PdfWriter.getInstance(document, fileOutputStream);
        document.open();

        System.out.println("CONVERTER START.....");

        String[] splitImagFiles = imagFileSource.split(",");

        for (String singleImage : splitImagFiles) {
            Image image = Image.getInstance(singleImage);
            document.setPageSize(image);
            document.newPage();
            image.setAbsolutePosition(0, 0);
            document.add(image);
        }

        document.close();

Tried adding

   ndk {
            abiFilters "arm64-v8a", "armeabi-v7a", "x86", "x86_64"
        }

but same error

    java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.myapp-tMMDQvGNIUp1-fffWZK0YQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.myapp-tMMDQvGNIUp1-fffWZK0YQ==/lib/arm64, /system/lib64, /system/product/lib64]]] couldn't find "liblcmm.so"
        at java.lang.Runtime.loadLibrary0(Runtime.java:1067)
        at java.lang.Runtime.loadLibrary0(Runtime.java:1007)
        at java.lang.System.loadLibrary(System.java:1667)
        at org.apache.harmony.awt.Utils$2.run(Utils.java:44)
        at java.security.AccessController.doPrivileged(AccessController.java:69)
        at org.apache.harmony.awt.Utils.loadLibrary(Utils.java:42)
        at org.apache.harmony.awt.gl.color.NativeCMM$1.run(NativeCMM.java:81)
        at org.apache.harmony.awt.gl.color.NativeCMM$1.run(NativeCMM.java:79)
        at java.security.AccessController.doPrivileged(AccessController.java:43)
        at org.apache.harmony.awt.gl.color.NativeCMM.loadCMM(NativeCMM.java:78)
        at org.apache.harmony.awt.gl.color.NativeCMM.<clinit>(NativeCMM.java:91)
        at org.apache.harmony.awt.gl.color.NativeCMM.cmmGetProfileElementSize(Native Method)
        at java.awt.color.ICC_Profile.getData(ICC_Profile.java:413)
        at java.awt.color.ICC_Profile.getIntFromHeader(ICC_Profile.java:734)
        at java.awt.color.ICC_Profile.getNumComponents(ICC_Profile.java:478)
        at com.lowagie.text.pdf.PdfICCBased.<init>(PdfICCBased.java:81)
        at com.lowagie.text.pdf.PdfWriter.addDirectImageSimple(PdfWriter.java:2996)
        at com.lowagie.text.pdf.PdfWriter.addDirectImageSimple(PdfWriter.java:2938)
        at com.lowagie.text.pdf.PdfContentByte.addImage(PdfContentByte.java:1225)
        at com.lowagie.text.pdf.PdfContentByte.addImage(PdfContentByte.java:1121)
        at com.lowagie.text.pdf.PdfContentByte.addImage(PdfContentByte.java:1105)
        at com.lowagie.text.pdf.PdfDocument.add(PdfDocument.java:2413)
        at com.lowagie.text.pdf.PdfDocument.add(PdfDocument.java:747)
        at com.lowagie.text.Document.add(Document.java:276)
        at com.myapp.activity.InvoiceActivity.imagesToPdf(InvoiceActivity.java:349)
        at com.myapp.activity.InvoiceActivity$6.onClick(InvoiceActivity.java:184)
        at androidx.appcompat.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
asturio commented 3 years ago

@omkar-tenkale

This seems to be a dependency problem of apache harmony. The Stacktrace says that "liblcmm.so" could not be found. As I understand the code, this is a JNI part of apache harmony. So you probably have to bundle the native library to your App.

I have no idea how this is done, but I found this: (German) https://wissensbasis.net/4198939/droidText-unstaisfiedLink-Fehler-beim-Erstellen-der-Image-Instanz (English) https://stackoverflow.com/questions/24357687/how-to-include-so-library-in-android-studio

So I think this is not really a bug in OpenPDF.

fakekowa commented 3 years ago

It seems like newer API levels of android is missing this or not allowing the applications to reach it, using a device with lower api level solves this which makes me wonder if it really is a problem that should be solved by bundling the library manually?

Azaratur commented 3 years ago

It seems like newer API levels of android is missing this or not allowing the applications to reach it, using a device with lower api level solves this which makes me wonder if it really is a problem that should be solved by bundling the library manually?

Google play console is rising target sdk api level, so this will become an issue in no-time. Also i saw on my phone (Google pixel 4xl android 11) that PNG file works as expected, JPG will not work.

Include this so library is not easy (at least for me) since i cannot find it anywhere...

andob commented 3 years ago

Hello. Will take a look at the problem. Sorry for the delay (it's been a year :D), but I've just now read your issue, GitHub didn't sent me email notifications.

andob commented 3 years ago

yes, this native library is missing from newer Android because google switched from Harmony to OpenJDK. There are two C libraries used: lcmm and jpegdecoder.

Found versions of those two here and here. I will search for newer versions, then I will compile them, change names to prevent name collision on old android versions and bundle them into android-awt library.

Problem is, I wasn't able to replicate the issue. @Azaratur Can you give me a picture that is not working?

Azaratur commented 3 years ago

Yes i knew about Harmony/OpenJDK. I did also search for newer version but without luck.. I guess it could be replaced with OpenJDK since i found almost the same methods, not sure it will work.

As far as i know it crash with any android 30 if the app is compiled on targetsdk 30 and with any jpg image. I did test on Pixel 2 XL and Pixel 4 XL both android 11. Both crashes with any png using an app compiled on targetsdk 30. Had different results on targetsdk 28.

andob commented 3 years ago

I've bundled lcmm and lcms C code into the library. I could also bundle this and this. But I don't know if it's important for the current issue.

@Azaratur I still couldn't replicate, but I made an update with the changes. Can you please import the latest release:

repositories {
    maven { url "http://maven.andob.info/repository/open_source" }
}
implementation 'com.github.librepdf:openpdf:1.3.24'
implementation 'ro.andob.androidawt:androidawt:1.0.4'

Then test your app and tell me if now it works or not?

Azaratur commented 3 years ago

Will test it in a bit.

Azaratur commented 3 years ago

Tested it and now it's working as expected! Thank you so much.

andob commented 3 years ago

great! you're welcome