SJacobs SJacobs - 5 months ago 78
Java Question

Proguard - Error: A JNI error has occured

I've been trying to use ProGuard to obfuscate an application of mine. I have disabled every option exception for obfuscate. Loader is my main class.

The screenshot below is the result when I try to run my obfuscated jar.
No errors were given while obfuscating either.

enter image description here

My config

-injars 'C:\Users\Corsair\Desktop\obfuscate\Example.jar'
-outjars 'C:\Users\Corsair\Desktop\obfuscate\ExampleOut.jar'

-libraryjars 'C:\Program Files\Java\jre1.8.0_91\lib\rt.jar'

-dontskipnonpubliclibraryclassmembers
-dontshrink
-dontoptimize
-dontusemixedcaseclassnames
-dontpreverify
-dontnote
-dontwarn

-verbose

-keep class Loader

Answer

If this is the only configuration that you are using, also native methods will get obfuscated. As a result, their name will not match the ones in the native library anymore, and thus you will see an error like this when trying to load the library using System.loadLibrary.

You need to add at least a rule like this:

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

This will instruct ProGuard to keep all native methods in any class it processes.

Edit:

Additional rules that are needed to get it working:

  • Remove -dontpreverify, preverify is needed for Java 7+
  • Keep the main method

This will keep the main method:

-keep class Loader {
    public static void main(...);
}