2

In my project, the native library is using reflection to call back into the Java code. So I followed this thread and added the following to proguard-project.txt

-keep class com.example.abc.MainActivity { *; }
-keep class com.example.abc.NativeClass { *; }

to preserve all methods and fields in NativeClass(where all native methods are declared) and Mainactivity where the calls to native methods are made. Within these native methods, again calls are made to NativeClass.java methods(pure java methods) which in turn calls java methods defined in Mainactivity.java. The app runs fine without proguard. But after exporting the project by enabling proguard, the app crashes at points where native method calls(which in turn calls java methods in NativeClass) are made. I have tried all possible commands to enter in proguard-project.txt to no avail. OpenCV methods are called in native code. How should I handle OpenCV native codes when enabling proguard. Please help.

Following are logs from tombstone file

/data/data/com.example.imageanalyse/lib/libfilters.so
be98d7a0  0000f2b8  [heap]
be98d7a4  be98d7b4  [stack]
be98d7a8  56d47798  /dev/ashmem/dalvik-LinearAlloc (deleted)
be98d7ac  5bdb9d6b  /data/data/com.example.imageanalyse/lib/libfilters.so
be98d7b0  00000000  
be98d7b4  00000000  
--------- tail end of log /dev/log/main
09-25 15:20:56.930  3356  3357 D dalvikvm: GC_CONCURRENT freed 18K, 48% free 20256K/38855K,    
paused 2ms+8ms
09-25 15:20:56.980  3356  3356 D dalvikvm: GC_FOR_ALLOC freed 5120K, 58% free 16405K/38855K, 
paused 22ms
09-25 15:20:57.030  3356  3356 D dalvikvm: GC_FOR_ALLOC freed 1278K, 58% free 16449K/38855K, 
paused 22ms
09-25 15:20:57.070  3356  3356 D dalvikvm: GC_FOR_ALLOC freed 1269K, 58% free 16449K/38855K, 
paused 22ms
09-25 15:20:57.090  3356  3356 F libc    : Fatal signal 11 (SIGSEGV) at 0x00000004 (code=1)
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
Community
  • 1
  • 1
Johny Smith
  • 154
  • 1
  • 1
  • 7
  • This can help you. http://stackoverflow.com/questions/7880107/in-proguard-how-to-preserve-a-set-of-classes-method-names Try it and comment us – CaptainTeemo Sep 25 '14 at 06:11
  • @CaptainTeemo I already have tried adding all those entries in proguard-project.txt. But doesn't -keep class com.example.abc.NativeClass { *; } alone retain all methods and fields of NativeClass? I think the problem is with calling opencv methods. Because even those native methods which doesnot callback java method, fail to execute. The app simply crashes there as well. BTW thanks for your help. – Johny Smith Sep 25 '14 at 07:46
  • Are this classes calling other classes which are not "-keep"-ed? BTW, a LogCat will be cool. If you cant't find the messages probably are located in the All Messages filter under the tag DEBUG. – CaptainTeemo Sep 25 '14 at 08:08
  • @CaptainTeemo I have updated the question with some logs. Can you please judge the cause of the error from those logs? – Johny Smith Sep 25 '14 at 10:02
  • I can't see anything with this log. I a bit human unreadable. Can you try to put the log that I told you? 1. Connect your device to see the LogCat. 2. Go to the 'All Messages' filter. 3. In green letters, with the tag DEBUG, there will be a huge dump that references also your native code functions. (I also hate debug native code in Android) – CaptainTeemo Sep 26 '14 at 01:02
  • With DEBUG tag I get all such logs. No human readable instructions came with DEBUG tag. For example: d0 74614d2f65726f63 d1 6c6c412f796e6f6f. So it is full of such messages. – Johny Smith Sep 26 '14 at 06:59

1 Answers1

3

Your configuration already correctly accounts for any native code trying to access fields or methods in com.example.abc.MainActivity or com.example.abc.NativeActivity.

Chances are that libfilters.so still tries to access some other classes, fields, or methods in the Java code. You should try to preserve the related classes, fields, and methods.

I don't see it in the OpenCV source code right away, but if the native code tries to access some classes, fields, or methods in the Java code, you should again preserve those:

-keep class org.opencv.** { *; }

If that helps, you can refine it to the classes, fields, and methods that are strictly required.

Eric Lafortune
  • 45,150
  • 8
  • 114
  • 106
  • I added -keep class org.opencv.** { *; }. Still the same error. In native code, I actually call a java method defined in NativeClass like: jclass thisclass = env->GetObjectClass(o); jmethodID metExtract = env->GetMethodID(thisclass,"extractROI","(Lorg/opencv/core/Mat;)V"); Is there anything wrong in this? – Johny Smith Sep 26 '14 at 06:50
  • 1
    You then need to preserve all hard-coded names and signatures from that code: `extractROI(...)` and `org.opencv.core.Mat`. In this case: `-keepclassmembers class com.example.abc.NativeClass { void extractROI(org.opencv.core.Mat); }` and `-keep class org.opencv.core.Mat`. Your current configuration already keeps this method and this class though, as part of a much larger set of classes, fields, and methods. You should check if there are other cases that aren't covered yet. – Eric Lafortune Sep 27 '14 at 10:28