Proguard:`NoSuchFieldError`尽管字段在`seeds.txt`

时间:2016-06-17 02:58:13

标签: java android java-native-interface proguard android-proguard

我正在尝试将proguard应用于使用JNI库的Android应用程序  当应用程序运行时,我遇到了这个崩溃:

Thread[24,tid=8563,WaitingForJniOnLoad,Thread*=0xb510a400,peer=0x12f57190,"Service Thread"] recursive attempt to load library "/data/app/com.my.package-1/lib/arm/nativeLibrary.so"
 No pending exception expected: java.lang.NoSuchFieldError: no "I" field "mNativeInstanceId" in class "Lcom/library/package/NativeDetector;" or its superclasses
   at java.lang.String java.lang.Runtime.nativeLoad(java.lang.String, java.lang.ClassLoader, java.lang.String) (Runtime.java:-2)
   at java.lang.String java.lang.Runtime.doLoad(java.lang.String, java.lang.ClassLoader) (Runtime.java:428)
   at void java.lang.Runtime.loadLibrary(java.lang.String, java.lang.ClassLoader) (Runtime.java:369)
   at void java.lang.System.loadLibrary(java.lang.String) (System.java:989)
   at void com.library.package.NativeDetector.<clinit>() (SourceFile:48)
   at com.library.package.DectorTypeA com.library.package.Detectors.createInstance() (SourceFile:63)
   at com.library.package.DectorTypeA com.library.package.imageAnalysis.DetectorProvider.a() (SourceFile:31)
   at com.library.package.DectorTypeA com.library.package.imageAnalysis.DetectorProvider.provideDetector() (SourceFile:24)
   at com.library.package.DectorTypeA com.library.package.myService.e() (SourceFile:205)
   at com.library.package.DectorTypeA com.library.package.myService.c(com.library.package.myService) (SourceFile:43)
   at void com.library.package.myService$2.run() (SourceFile:226)

错误消息看起来像mNativeInstanceId已被混淆。但是,库模块的seeds.txt具有以下行:

com.library.package.NativeDetector: int mNativeInstanceId

意思是它没有被混淆。

如何修复此崩溃?

图书馆模块

proguard-rules.pro

-keepparameternames
-renamesourcefileattribute SourceFile
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod,LocalVariableTable

-keep public class * {
    public protected *;
}

-keepclassmembernames class * {
    java.lang.Class class$(java.lang.String);
    java.lang.Class class$(java.lang.String, boolean);
}

-keepclasseswithmembernames,includedescriptorclasses class * {
    native <methods>;
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}


-keep class com.library.package.NativeDetector

-keepclassmembers class com.library.package.NativeDetector {
    private <fields>;
}

NativeDetector.java

class NativeDetector {

    private int mNativeInstanceId; //intialized in JNI code
    ...
}

编辑:我也尝试了一个带有这样的行的proguard文件,结果没有变化:

-keep class com.om.library.package.NativeDetector {
    private <fields>;
}

1 个答案:

答案 0 :(得分:2)

您遇到的问题可能是因为您仅将 proguard 规则添加到库模块。除非您将其指定为consumerProguardFiles,否则不会自动为应用程序选取这些内容。