CodeMinion / another_brother

Another Brother Flutter SDK
BSD 3-Clause "New" or "Revised" License
21 stars 21 forks source link

Force close on Android Release build #41

Closed PrimaelQuemerais closed 5 months ago

PrimaelQuemerais commented 1 year ago

Hello, I'm running into an issue using this library. Everything works perfectly in Debug and Profil releases however my application force closes when building a Release version.

I'm getting no error on the Flutter side but here's the code that's likely to cause the crash :

Future<PrinterStatus?> testPrint() async {
    try {
      File testFile =
          File('${(await getTemporaryDirectory()).path}/test_print.png');
      ByteData bytes = await rootBundle.load('assets/logos/test.png');
      testFile.writeAsBytesSync(bytes.buffer.asUint8List(
        bytes.offsetInBytes,
        bytes.lengthInBytes,
      ));
      return await brotherPrinter().printFile(testFile.path);
    } catch (error, stacktrace) {
      if (kDebugMode) {
        print(error);
        print(stacktrace);
      }
      return null;
    }
 }

And the brotherPrinter() function (This has been tested and works as expected) :

Printer brotherPrinter() {
    Printer printer = Printer();
    PrinterInfo printInfo = PrinterInfo();
    printInfo.printerModel = Model.TD_2120N;
    printInfo.printMode = PrintMode.FIT_TO_PAGE;
    printInfo.isAutoCut = false;
    printInfo.port = Port.NET;
    printInfo.customPaperInfo = CustomPaperInfo.newCustomDiaCutPaper(
        Model.TD_2120N, Unit.Mm, 51, 26, 2, 2, 3, 3, 0);
    printInfo.ipAddress = ip;
    printInfo.binCustomPaper = BinPaper_TD2120N.RD_W51H26mm;

    printer.setPrinterInfo(printInfo);
    return printer;
 }

Here's the stacktrace from add logcat :

03-06 17:25:59.302  9025  9025 F DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
03-06 17:25:59.302  9025  9025 F DEBUG   : Build fingerprint: 'samsung/gta8wifieea/gta8wifi:13/TP1A.220624.014/X200XXS1CWA3:user/release-keys'
03-06 17:25:59.302  9025  9025 F DEBUG   : Revision: '0'
03-06 17:25:59.302  9025  9025 F DEBUG   : ABI: 'arm64'
03-06 17:25:59.302  9025  9025 F DEBUG   : Processor: '7'
03-06 17:25:59.302  9025  9025 F DEBUG   : Timestamp: 2023-03-06 17:25:59.049600317+0100
03-06 17:25:59.302  9025  9025 F DEBUG   : Process uptime: 844s
03-06 17:25:59.302  9025  9025 F DEBUG   : Cmdline: com.example.app
03-06 17:25:59.302  9025  9025 F DEBUG   : pid: 5953, tid: 9020, name: DefaultDispatch  >>> com.example.app <<<
03-06 17:25:59.302  9025  9025 F DEBUG   : uid: 10275
03-06 17:25:59.302  9025  9025 F DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0000000000000000
03-06 17:25:59.302  9025  9025 F DEBUG   : Cause: null pointer dereference
03-06 17:25:59.302  9025  9025 F DEBUG   :     x0  0000000000000000  x1  6e69207265706170  x2  616d6d6f63206f66  x3  b40000757e1e6c10
03-06 17:25:59.302  9025  9025 F DEBUG   :     x4  0000000000000000  x5  b40000757e1e6c7f  x6  4e2028203a20646e  x7  726f70707553746f
03-06 17:25:59.302  9025  9025 F DEBUG   :     x8  7265746e69725074  x9  546c696146202c20  x10 506574616572436f  x11 2920656c69464454
03-06 17:25:59.302  9025  9025 F DEBUG   :     x12 6f4e2028203a2064  x13 74726f7070755374  x14 0000000000023628  x15 0000000000000050
03-06 17:25:59.302  9025  9025 F DEBUG   :     x16 00000073d23a0cc0  x17 0000007681ed9e40  x18 00000073d23a60c8  x19 b40000750169f8c0
03-06 17:25:59.302  9025  9025 F DEBUG   :     x20 000000000000006f  x21 0000000000000071  x22 000000754d2fac08  x23 b40000757e1e6a50
03-06 17:25:59.302  9025  9025 F DEBUG   :     x24 0000000000000000  x25 0000000000000000  x26 000000754d2fad3c  x27 000000754d2fac20
03-06 17:25:59.302  9025  9025 F DEBUG   :     x28 00000073d23a3000  x29 000000754d2fab80
03-06 17:25:59.302  9025  9025 F DEBUG   :     lr  00000073d21b0268  sp  000000754d2fab80  pc  0000007681ed9e50  pst 0000000080000000
03-06 17:25:59.302  9025  9025 F DEBUG   : backtrace:
03-06 17:25:59.302  9025  9025 F DEBUG   :       #00 pc 0000000000085e50  /apex/com.android.runtime/lib64/bionic/libc.so (__strlen_aarch64+16) (BuildId: 830333d8cc2ef7702e0ad326d565a910)
03-06 17:25:59.302  9025  9025 F DEBUG   :       #01 pc 0000000000098264  /data/app/~~5wM1v8om_qhNTH_nKsDqTw==/com.example.app-_u-7y4_AHUglPwj442G-iQ==/lib/arm64/libcreatedata.so (Java_com_brother_ptouch_sdk_JNIWrapper_sendLogToGlobalLoggingV3JNI+260) (BuildId: 57a5ea7f697923741534651004a9c7ef7c46ee8c)
03-06 17:25:59.302  9025  9025 F DEBUG   :       #02 pc 000000000043dd54  /apex/com.android.art/lib64/libart.so (art_quick_generic_jni_trampoline+148) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.302  9025  9025 F DEBUG   :       #03 pc 0000000000209398  /apex/com.android.art/lib64/libart.so (nterp_helper+152) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.302  9025  9025 F DEBUG   :       #04 pc 0000000000144788  /data/app/~~5wM1v8om_qhNTH_nKsDqTw==/com.example.app-_u-7y4_AHUglPwj442G-iQ==/oat/arm64/base.vdex (com.brother.ptouch.sdk.Logging.sendLogWithLevelV3+16)
03-06 17:25:59.302  9025  9025 F DEBUG   :       #05 pc 0000000000209398  /apex/com.android.art/lib64/libart.so (nterp_helper+152) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #06 pc 0000000000148238  /data/app/~~5wM1v8om_qhNTH_nKsDqTw==/com.example.app-_u-7y4_AHUglPwj442G-iQ==/oat/arm64/base.vdex (com.brother.ptouch.sdk.PrinterInfo.getCustomPaperCommand+224)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #07 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #08 pc 0000000000148e56  /data/app/~~5wM1v8om_qhNTH_nKsDqTw==/com.example.app-_u-7y4_AHUglPwj442G-iQ==/oat/arm64/base.vdex (com.brother.ptouch.sdk.Printer.createRasterPrint+974)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #09 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #10 pc 0000000000149b3e  /data/app/~~5wM1v8om_qhNTH_nKsDqTw==/com.example.app-_u-7y4_AHUglPwj442G-iQ==/oat/arm64/base.vdex (com.brother.ptouch.sdk.Printer.setPrinterInfo+6)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #11 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #12 pc 000000000022f18e  /data/app/~~5wM1v8om_qhNTH_nKsDqTw==/com.example.app-_u-7y4_AHUglPwj442G-iQ==/oat/arm64/base.vdex (h4.a0$b.invokeSuspend+346)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #13 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #14 pc 0000000000290622  /data/app/~~5wM1v8om_qhNTH_nKsDqTw==/com.example.app-_u-7y4_AHUglPwj442G-iQ==/oat/arm64/base.vdex (kotlin.coroutines.jvm.internal.a.resumeWith+22)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #15 pc 000000000020b074  /apex/com.android.art/lib64/libart.so (nterp_helper+7540) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #16 pc 000000000020dfa2  /data/app/~~5wM1v8om_qhNTH_nKsDqTw==/com.example.app-_u-7y4_AHUglPwj442G-iQ==/oat/arm64/base.vdex (f7.v0.run+258)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #17 pc 000000000020b074  /apex/com.android.art/lib64/libart.so (nterp_helper+7540) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #18 pc 00000000002946d8  /data/app/~~5wM1v8om_qhNTH_nKsDqTw==/com.example.app-_u-7y4_AHUglPwj442G-iQ==/oat/arm64/base.vdex (kotlinx.coroutines.internal.n.run+24)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #19 pc 000000000020b074  /apex/com.android.art/lib64/libart.so (nterp_helper+7540) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #20 pc 0000000000298bf8  /data/app/~~5wM1v8om_qhNTH_nKsDqTw==/com.example.app-_u-7y4_AHUglPwj442G-iQ==/oat/arm64/base.vdex (kotlinx.coroutines.scheduling.k.run+4)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #21 pc 000000000020b074  /apex/com.android.art/lib64/libart.so (nterp_helper+7540) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #22 pc 00000000002985ec  /data/app/~~5wM1v8om_qhNTH_nKsDqTw==/com.example.app-_u-7y4_AHUglPwj442G-iQ==/oat/arm64/base.vdex (kotlinx.coroutines.scheduling.a.y+0)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #23 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #24 pc 00000000002977b0  /data/app/~~5wM1v8om_qhNTH_nKsDqTw==/com.example.app-_u-7y4_AHUglPwj442G-iQ==/oat/arm64/base.vdex (kotlinx.coroutines.scheduling.a$c.d+28)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #25 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #26 pc 00000000002978e0  /data/app/~~5wM1v8om_qhNTH_nKsDqTw==/com.example.app-_u-7y4_AHUglPwj442G-iQ==/oat/arm64/base.vdex (kotlinx.coroutines.scheduling.a$c.n+56)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #27 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #28 pc 0000000000297890  /data/app/~~5wM1v8om_qhNTH_nKsDqTw==/com.example.app-_u-7y4_AHUglPwj442G-iQ==/oat/arm64/base.vdex (kotlinx.coroutines.scheduling.a$c.run+0)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #29 pc 000000000043436c  /apex/com.android.art/lib64/libart.so (art_quick_invoke_stub+556) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #30 pc 0000000000466138  /apex/com.android.art/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+156) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #31 pc 0000000000465e20  /apex/com.android.art/lib64/libart.so (art::JValue art::InvokeVirtualOrInterfaceWithJValues<art::ArtMethod*>(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, art::ArtMethod*, jvalue const*)+388) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #32 pc 0000000000613e74  /apex/com.android.art/lib64/libart.so (art::Thread::CreateCallback(void*)+1668) (BuildId: a49c773ef6221a996ecea990e9753caa)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #33 pc 00000000000fb13c  /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start(void*)+204) (BuildId: 830333d8cc2ef7702e0ad326d565a910)
03-06 17:25:59.303  9025  9025 F DEBUG   :       #34 pc 000000000008e490  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+64) (BuildId: 830333d8cc2ef7702e0ad326d565a910)
03-06 17:25:59.325  9031  9031 D ip_monitor-816: 2a01:cb08:819d:6e00:46d4:54ff:fe0a:90f0 dev wlan0 lladdr 44:d4:54:0a:90:f0 router PROBE
03-06 17:25:59.333   493   493 E tombstoned: Tombstone written to: tombstone_17
03-06 17:25:59.337  1063  9034 I DropBoxManagerService: add tag=data_app_native_crash isTagEnabled=true flags=0x2
03-06 17:25:59.341  1063  9033 W ContextImpl: Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1252 com.android.server.am.AppErrors.crashApplicationInner:722 com.android.server.am.AppErrors.crashApplication:598 com.android.server.am.ActivityManagerService.handleApplicationCrashInner:10387 com.android.server.am.NativeCrashListener$NativeCrashReporter.run:88 
03-06 17:25:59.341  1063  9033 W ActivityManager: crash : com.example.app,10275
03-06 17:25:59.341  1063  1169 I BootReceiver: Copying /data/tombstones/tombstone_17 to DropBox (SYSTEM_TOMBSTONE)
03-06 17:25:59.342  1063  1169 I DropBoxManagerService: add tag=SYSTEM_TOMBSTONE isTagEnabled=true flags=0x6
03-06 17:25:59.342  1063  9033 W ActivityTaskManager:   Force finishing activity com.example.app/.MainActivity

This is running on a Samsung Galaxy Tab A8 running Android 13. I can provide more information and run more tests if needed. Thank you very much for your help!

CodeMinion commented 1 year ago

Hi @PrimaelQuemerais !

Thanks for reaching out about this. The fact it only crashes in release mode it is quite interesting. From the logs it seems it's crashing as a result of a Null Pointer Exception and seems that strlen was called on a null pointer.

One of the places I could see your Flutter code sending a string would be on that testFile.path could you check if it's somehow null on release?

Could you also confirm if you are providing the proper permissions in Android?

Thanks again for reaching out and thanks in advance for the details,

PrimaelQuemerais commented 1 year ago

Hello @CodeMinion

Thank you very much for the quick reply. Here are some additional information.

These are the permissions in my AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE"/>

I checked if testFile.path returns the correct value and it does, in my case: /data/user/0/com.example.package/cache/test_print.png

I also checked if the file contains any data using testFile.readAsBytesSync() and it does yield the correct list of bytes.

I'm also experiencing the same issue when using the printFiles() function. And as stated before none of this happens in debug or profil, very strange.

CodeMinion commented 1 year ago

Hello @PrimaelQuemerais ,

Thanks for the additional details. It's quite odd you are only seeing it in release. Normally that would make me think something is getting lost as part of minimify or proguard. Though looking at the logs it seems to originate from getCustomPaperCommand which in turn calls sendLogWithLevelV3 and that makes the native call to Java_com_brother_ptouch_sdk_JNIWrapper_sendLogToGlobalLoggingV3JNI with a null string. All these call are happening on the Brother SDK so that's a bit tricky.

In the case with the printFile and printFileList they all send the path directly to the native library so it would not surprise me if we are seeing it in both.

Could you try printing without using the custom paper and see if you still see the crash? I am curious if it's related to that or something else entirely.

Thanks again for all your help,

PrimaelQuemerais commented 1 year ago

Good news!

As you suggested I tried to comment this line in my brotherPrinter() function :

Printer brotherPrinter() {
    Printer printer = Printer();
    PrinterInfo printInfo = PrinterInfo();
    printInfo.printerModel = Model.TD_2120N;
    printInfo.printMode = PrintMode.FIT_TO_PAGE;
    printInfo.isAutoCut = false;
    printInfo.port = Port.NET;
    /*printInfo.customPaperInfo = CustomPaperInfo.newCustomDiaCutPaper(
        Model.TD_2120N, Unit.Mm, 51, 26, 2, 2, 3, 3, 0);*/
    printInfo.ipAddress = ip;
    printInfo.binCustomPaper = BinPaper_TD2120N.RD_W51H26mm;

    printer.setPrinterInfo(printInfo);
    return printer;
 }

And now my app is printing without any issue in Release. Would you like me to run some more tests in order to determine the reason behind this error?

CodeMinion commented 1 year ago

Hello @PrimaelQuemerais ,

That's great news! Apologies for the delayed reply. Glad to hear it's working well on release.

Thank you for the offer for additional tests! With your original report I'll forward it to Brother during the hackathon taking place around April/May of this year and see if they know what it is or perhaps something they have a fix for by now.

Thanks again for all your help with this.