Android: Propose using @Keep so we can use ProGaurd's minify?

Because of the JNI methods, it’s not possible to use ProGuard to remove unused code and obfuscate. The closest solution I’ve found is to add this line to proguard-rules.pro:

-keep public class org.libsdl.app.** { public *; }

But this leaves SDL’s Java code mostly unobfuscated. If we added @Keep before every JNI method, this would automatically protect JNI methods from removal, allowing ProGuard to do its thing.

Does anyone have any other solutions?

But this leaves SDL’s Java code mostly unobfuscated. If we added @Keep
before every JNI method, this would automatically protect JNI methods
from removal, allowing ProGuard to do its thing.

Is there any reason not to do this? Doesn’t work on older compilers,
or anything?

–ryan.

I don’t know much about Android development and I’ve never used ProGuard, but:
Is there a point in obfuscating SDL’s Java Code? And I guess it doesn’t contain unused code that can be removed either?

I’ve just looked into this. Works with compileSdkVersion 26. Requires import android.support.annotation.Keep;. Gradle plugin 2.3.3 requires you to add dependancy compile ‘com.android.support:support-annotations:28.0.0’, which in turn needs buildscript { repositories { jcenter() maven { url ‘https://maven.google.com/’ name ‘Google’ }, whereas with Gradel plugin 3.2.1 there seemed to be nothing further to add.

Does that sound ok? The other way to do it is to list all the JNI methods in the proguard file, which risks mismanagement.

@Keep also retains the class name, which doesn’t break the reference from the manifest file, so it seems a clean and safe solution all around.

It will reduce APK size and make it harder to reverse-engineer; all symbols are converted to short-names (usually just 1 character long). It’s not just SDL’s Java Code that gets processed but also everything that’s imported, I believe. It’s good practice to ProGuard if you release something. The latest ProGuard will also do some code optimisation.

You can use the exported mapping.txt to make sense of crash report stack traces in Google Play.

1 Like