Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to keep a override method of ThreadLocal field ? #457

Open
merseyalma opened this issue Dec 20, 2024 · 1 comment
Open

How to keep a override method of ThreadLocal field ? #457

merseyalma opened this issue Dec 20, 2024 · 1 comment
Labels
usage question Related to configuration and usage of Proguard.

Comments

@merseyalma
Copy link

merseyalma commented Dec 20, 2024

Hi!

public class A
 {
    private final ThreadLocal<DateFormat> format = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };
}

In the above code, I need to keep initialValue method, How to write in proguard.cfg file?

I write the configuration like below

-keepclassmembers class A { *; }

but, it obfuscated as below

  private final ThreadLocal<DateFormat> format = new ThreadLocal<DateFormat>(this) {
      protected DateFormat f() {
        return new SimpleDateFormat("yyyy-MM-dd");
      }
    };

it results in format.get() is null.

@mrjameshamilton
Copy link
Contributor

Hi @merseyalma !

The overridden "iniitalValue" method is actually in an anonymous class called A$1. The ProGuard Playground is useful for questions like these, since you can see visually which keep rules match the entities in your app.

Here is a Playground with your example: https://playground.proguard.com/p/BmAJpS

In short, you'll need a rule that covers the anonymous class like this:

-keep class A$1 {
	*** initialValue();
}

@mrjameshamilton mrjameshamilton added the usage question Related to configuration and usage of Proguard. label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
usage question Related to configuration and usage of Proguard.
Projects
None yet
Development

No branches or pull requests

2 participants