我的未混淆代码有效。。
当运行Proguard模糊处理时——只需1次优化我得到以下错误:
Exception in thread "main" java.lang.Error: Structure.getFieldOrder() on class com.sun.jna.platform.mac.SystemB$Timeval does not provide enough names [0] ([]) to match declared fields [2] ([tv_sec, tv_usec])
at com.sun.jna.Structure.getFields(Unknown Source)
at com.sun.jna.Structure.deriveLayout(Unknown Source)
at com.sun.jna.Structure.calculateSize(Unknown Source)
at com.sun.jna.Structure.allocateMemory(Unknown Source)
at com.sun.jna.Structure.ensureAllocated(Unknown Source)
at com.sun.jna.Structure.ensureAllocated(Unknown Source)
at com.sun.jna.Structure.getPointer(Unknown Source)
at oshi.util.platform.mac.SysctlUtil.sysctl(Unknown Source)
at oshi.software.os.mac.MacOperatingSystem.<clinit>(Unknown Source)
at oshi.SystemInfo.createOperatingSystem(Unknown Source)
at oshi.util.Memoizer$1.get(Unknown Source)
at oshi.SystemInfo.getOperatingSystem(Unknown Source)
我尝试了几个关于保持指令如下:
-keep class com.sun.** { *; }
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class com.sun.jna.**{ *; }
-keep class * implements com.sun.jna.**{ *; }
-keep class oshi.**{ *; }
-keep class * implements oshi.**{ *; }
-keepclassmembers class * extends com.sun.jna.** {
<fields>;
<methods>;
}
-keepclassmembers class * extends oshi.** {
<fields>;
<methods>;
}
如何配置Proguard以避免此异常?
从JNA 5.x开始,Structure
类字段由@FieldOrder
注释标识。对于导致问题的Timeval
结构,其定义如下:
@Structure.FieldOrder({ "tv_sec", "tv_usec" })
class Timeval extends Structure {
public NativeLong tv_sec; // seconds
public int tv_usec; // microseconds
}
堆栈跟踪指示它正在(正确地(查找public
字段,但没有找到注释声明的字段顺序。
这个问题表明Proguard会混淆注释,这可能是问题的根源。
JNA的FAQ建议使用以下语法。可能需要更广泛的通配符定义,而不是明确定义<fields>
和<methods>
。
-dontwarn java.awt.*
-keep class com.sun.jna.* { *; }
-keepclassmembers class * extends com.sun.jna.* { public *; }
由于注释在JNA的Structure类中被定义为@interface
,因此这个问题的答案所建议的解决方案表明,应该通过以下方法来解决问题:
# Annotated interfaces (including methods which are also kept in implementing classes)
-keep @com.sun.jna.Structure.FieldOrder interface * {
*;
}
其他可能有效的解决方案,更广泛地说:
-keepattributes Annotation
-keepattributes *Annotation*