简体   繁体   English

使用ProGuard时如何保留从JNI访问的类变量

[英]How to preserve class variables accessed from JNI when using ProGuard

I've read through the manual -keep options but my JNI code is getting SIGSEGV when it accesses any of my Java class variables. 我已经通读了手动-keep选项,但是当JNI代码访问任何Java类变量时,它都会得到SIGSEGV。

V/MyClass:native:my_native_method(30592): entered
I/DEBUG   (18752): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
I/DEBUG   (18752): Build fingerprint: 'samsung/DEVICE/DEVICE:ANDROIDVERSION/XXXXXX/XXXXXX:user/release-keys'
I/DEBUG   (18752): pid: 30592, tid: 30619  >>> com.example <<<
I/DEBUG   (18752): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0000000c
I/DEBUG   (18752):  r0 413f5890  r1 409b1fc8  r2 00000000  r3 00000001
I/DEBUG   (18752):  r4 1d400256  r5 00000000  r6 5ca8fa80  r7 1d400256
I/DEBUG   (18752):  r8 5ca8fbe8  r9 56c6cea0  10 00000000  fp 5ca8fbfc
I/DEBUG   (18752):  ip 409ad030  sp 5ca8fa10  lr 400c4260  pc 40950524  cpsr 60000030
I/DEBUG   (18752):  d0  0000006442c80000  d1  3ff0000042c80000
I/DEBUG   (18752):  d2  0000000000000000  d3  43ec000043a60000
I/DEBUG   (18752):  d4  0000000000000000  d5  0000000000000000
I/DEBUG   (18752):  d6  00000000c2600000  d7  3f8000003f800000
I/DEBUG   (18752):  d8  43ec000044930000  d9  44084000448d0000
I/DEBUG   (18752):  d10 0000000000000000  d11 0000000000000000
I/DEBUG   (18752):  d12 0000000000000000  d13 0000000000000000
I/DEBUG   (18752):  d14 0000000000000000  d15 0000000000000000
I/DEBUG   (18752):  scr 60000010

By adding some log statements I could see that this happens whenever the native code accesses one of the Java class variables. 通过添加一些日志语句,我可以看到,只要本机代码访问Java类变量之一,就会发生这种情况。

At first I tried: 一开始我尝试过:

-keep class com.example.MyClass {
    String mystring1;
    static String mystring2;
    byte[] mybytearray1;
    ...
}

I've even tried this in my proguard-project.txt: 我什至在proguard-project.txt中尝试了此操作:

-keep class com.example.MyClass {
    *;
}

But still I get SIGSEGV in my JNI library. 但是我仍然在我的JNI库中得到了SIGSEGV。

I looked in bin/proguard/seeds.txt and the variables ARE in there. 我查看了bin / proguard / seeds.txt,其中的变量位于其中。

But something goes wrong. 但是出了点问题。

The code runs perfectly until I enable ProGuard. 在启用ProGuard之前,代码可以完美运行。

UPDATE: In researching this I am wondering whether the requirement about Android GC being able to relocate pointers is affecting ProGuard. 更新:在研究这一点时,我想知道有关Android GC能够重定位指针的要求是否会影响ProGuard。 In my native C code I have this: 在我的本机C代码中,我有:

JNIEXPORT jint JNICALL  Java_com_example_MyClass_my_1native_1method(
    JNIEnv* env,
    jobject thizz)
{
    jobject thiz = (*env)->NewGlobalRef(env,thizz); // prevent newer GC from relocating pointer

Could this be the source of the problem for ProGuard? 这可能是ProGuard问题的根源吗?

This project involves a main project that incorporates a number of subprojects. 该项目涉及一个包含多个子项目的主项目。 It is one of the subproject libraries that calls the native library. 它是调用本机库的子项目库之一。 I had put the -keep class *; 我上了-keep课*; preservation into this subproject's proguard-project.txt. 保存到该子项目的proguard-project.txt中。 I didn't think it needed to be put into the main project proguard file. 我认为不需要将其放入主项目的proguard文件中。 But that was the problem. 但这就是问题所在。 As soon as I duplicated the library subproject's -keep class statement in the main project proguard file then the SIGSEGV disappeared. 在主项目proguard文件中复制库子项目的-keep类语句后,SIGSEGV就消失了。 It would be nice if proguard could make it unnecessary to duplicate entries like this. 如果proguard使得不必重复这样的条目,那将是很好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM