如何配置ProGuard使子类混淆后字段名与父类不重复?
Problem Context
You have these original class definitions:
public class Superclass { private int intValue; } public class Subclass extends Superclass { private String stringValue; }
After ProGuard obfuscation, they might end up like this:
public class A { private int a; } public class B extends A { private String a; }
Since these classes are used for local JSON serialization, you don't need to keep the original field names—but you require that obfuscated field names don't duplicate between the subclass and its parent. You want to know if it's possible to rename the a field in class B to something unique (like b).
Solution
Absolutely! ProGuard has built-in configuration rules to control obfuscated member naming strategies, specifically to avoid this kind of parent-child field name collision.
Step-by-Step Configuration
Add this rule to your ProGuard configuration file:
# Generate globally unique names for all class members (fields and methods) -useuniqueclassmembernames
This -useuniqueclassmembernames flag tells ProGuard to create globally unique identifiers for every field and method across all classes. Not only does it fix the parent-child duplicate name issue you're seeing, but it also prevents duplicate member names between completely unrelated classes.
Additional Notes
If you only want to enforce this for specific classes (like your Superclass and Subclass instead of all classes), you can combine this with targeted -keep rules—but in most cases, enabling the global -useuniqueclassmembernames is the simpler, more reliable choice for serialization scenarios. It eliminates potential edge cases that could cause headaches during serialization/deserialization.
Also, this setting won't interfere with any JSON serializer requirements (like case sensitivity) — it just ensures that every obfuscated member has a distinct, non-conflicting name.
内容的提问来源于stack exchange,提问作者fikr4n

