shangmisunmi / SunmiPrinterDemo

printer test demo
287 stars 137 forks source link

printerlibrary:1.0.15 crashes after R8 #60

Open gmk57 opened 2 years ago

gmk57 commented 2 years ago

Stack trace:

java.lang.NoSuchMethodError: No static method a(Ljava/lang/String;)Ljava/lang/String; in class Landroid/os/SystemProperties; or its super classes (declaration of 'android.os.SystemProperties' appears in /system/framework/framework.jar)
    at com.sunmi.peripheral.printer.SunmiPrinterService$Stub$Proxy.setTable(SunmiPrinterService.java:632)
    at com.sunmi.peripheral.printer.SunmiPrinterService$Stub$Proxy.<init>(SunmiPrinterService.java:628)
    at com.sunmi.peripheral.printer.SunmiPrinterService$Stub.asInterface(SunmiPrinterService.java:37)
    at com.sunmi.peripheral.printer.InnerPrinterCallback.onServiceConnected(InnerPrinterCallback.java:21)

or

java.lang.NoSuchFieldError: No field a of type I in class Landroid/os/SystemProperties; or its superclasses (declaration of 'android.os.SystemProperties' appears in /system/framework/framework.jar)
    at com.sunmi.peripheral.printer.SunmiPrinterService$Stub$Proxy.setTable(SunmiPrinterService.java:632)
    at com.sunmi.peripheral.printer.SunmiPrinterService$Stub$Proxy.<init>(SunmiPrinterService.java:628)
    at com.sunmi.peripheral.printer.SunmiPrinterService$Stub.asInterface(SunmiPrinterService.java:37)
    at com.sunmi.peripheral.printer.InnerPrinterCallback.onServiceConnected(InnerPrinterCallback.java:21)

This is caused by

String model = SystemProperties.get("ro.sunmi.hardware").toUpperCase(Locale.ENGLISH);

at line 664 of (decompiled) SunmiPrinterService.java

Fix

Add to proguard-rules.pro: -keep class android.os.SystemProperties { get(java.lang.String); } Ideally this should be added to library's consumer-rules.pro.

shenjingfs commented 2 years ago

It still crashes, even if rule are added -keep class android.os.SystemProperties {*;}

Log java.lang.NoSuchFieldError: No field VERSION_CODE of type Ljava/lang/String; in class Landroid/os/SystemProperties; or its superclasses (declaration of 'android.os.SystemProperties' appears in /system/framework/framework.jar)

Fix:

Downgrade to 1.0.13 solved! And there is no need to add additional proguard rules

masmil1988 commented 2 years ago

It still crashes, even if rule are added -keep class android.os.SystemProperties {*;}

Log java.lang.NoSuchFieldError: No field VERSION_CODE of type Ljava/lang/String; in class Landroid/os/SystemProperties; or its superclasses (declaration of 'android.os.SystemProperties' appears in /system/framework/framework.jar)

Fix:

Downgrade to 1.0.13 solved! And there is no need to add additional proguard rules

Downgrading to 1.0.14 is enough to avoid the crash. Anyway, this still remains an issue...

rynkowsg commented 2 years ago

The solution suggested by @gmk57 works for the problem he posted. I've had the same issue and after applying it works. And if it was done correctly, should be added to the library's consumer rules.

The problem posted by @shenjingfs seems to be something else, and I haven't seen it.

rynkowsg commented 2 years ago

I guess the bizarre idea behind adding within a library android.os.SystemProperties is to access the system's android.os.SystemProperties which are not available for an app/lib developer.

The SystemProperties is implemented with JNI - the impl is split into two parts: C++'s android_os_SystemProperties.cpp (implementing the access) and Java's android.os.SystemProperties (providing Java interface).

The printerlibrary provides a copy of android.os.SystemProperties (probably an exact copy of SystemProperties.java from Sunmi Android OS source code). Just the face of the JNI-bridged implementation, to access natively implemented part.

Sounds like quite a hack 😬

rynkowsg commented 1 year ago

Looking at the content of the sunmi libraries, it looks they fixed the issue in 1.0.18.

1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19
Screen Shot 2022-09-21 at 12 11 49 - 1 0 14 Screen Shot 2022-09-21 at 12 13 20 - 1 0 15 Screen Shot 2022-09-21 at 12 14 01 - 1 0 16 Screen Shot 2022-09-21 at 12 09 02 - 1 0 17 Screen Shot 2022-09-21 at 12 10 03 - 1 0 18 Screen Shot 2022-09-21 at 12 10 51 - 1 0 19