Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Proguard Options - Stricter Rules & Remove Log Statements

I have enabled proguard by un-commenting the line in project.properties.

I decompiled my APK to check if it worked. I can see that all my methods, variable names and class names have been changed to a,b,c.. etc which is good. However my Activity classes have not been renamed. Also the Log.d statements were not removed as expected.

How can I make it harder for someone to read the decompiled code and also remove the log statements?

like image 261
Santiago Avatar asked Dec 01 '22 04:12

Santiago


2 Answers

Various methods related to the Android application lifecycle cannot be renamed, because:

  • Inheritance; the superclass method is enclosed within the Android library, which cannot be renamed through Proguard. Renaming onCreate() to a() will therefore cause a compilation problem due to the @Override annotation.
  • In the lifecycle of your activity, Android executes various functions that are expected to have specific names (e.g. onCreate() and onResume()). If your onCreate() is renamed to a(), only the superclass implementation will be executed -- not yours.

As for why your Activities' class names can't be renamed:

  • This would assume making modifications to the manifest, which simply isn't supported by Proguard.
  • There may be unwanted implications of making these modifications automatically. For instance, shortcuts from a user's home screen may no longer work if a class that was previously obfuscated to A, may now be obfuscated to B.

Getting your Log statements removed through Proguard is fairly simple using the assumenosideeffects option. See this related question for details:

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
}
like image 190
Paul Lammertsma Avatar answered Dec 15 '22 20:12

Paul Lammertsma


Paul Lammertsma has provided an accurate answer to the first part of your question. Elaborating on the second part:

You can only remove logging if optimization is not disabled, which requires specifying a different global configuration file in project.properties:

  proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt

These settings in proguard-project.txt then remove all logging:

-assumenosideeffects class android.util.Log {
    public static boolean isLoggable(java.lang.String, int);
    public static int v(...);
    public static int i(...);
    public static int w(...);
    public static int d(...);
    public static int e(...);
}

There are various equivalent forms with more or fewer wildcards.

Similar questions and answers:

  • Removing Log call using proguard
  • Removing unused strings during ProGuard optimisation
  • Removing logging with ProGuard doesn't remove the strings being logged
  • How to config my proguard-project.txt file to remove just Logs
like image 42
Eric Lafortune Avatar answered Dec 15 '22 21:12

Eric Lafortune