You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置ProGuard使子类混淆后字段名与父类不重复?

How to Prevent ProGuard from Generating Duplicate Obfuscated Field Names Between Parent and Child Classes?

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:52:54