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

如何为多个命名空间配置ProGuard Keep选项?

ProGuard Keep Rules for Multiple Namespaces: No Repeats Needed?

Great question! Let's break down how to apply identical keep rules to multiple namespaces in ProGuard without repeating your configuration.

First, a quick reality check: ProGuard's wildcard syntax doesn't support regex-style grouping like [com|io]—that syntax won't work as you might expect. ProGuard only uses * (matches any characters except dots) and ** (matches any characters including dots) for wildcard matching.

But don't worry, there are clean, maintainable ways to reuse your keep rules across multiple namespaces:

Option 1: Reuse Rules with Custom Variables

ProGuard lets you define reusable rule blocks using custom variables, which is the most efficient way to avoid duplication. Here's how it works:

# First, define a reusable block with your core keep configuration
-mySharedKeepRules {
    # Add your actual rules here—e.g., keep public methods, specific fields, etc.
    public <methods>;
    private static final <fields>;
    # Any other rules you need to apply to both namespaces
}

# Now apply the shared rules to each target namespace
-keepclassmembers class com.yourcompany.yourpackage.** {
    -mySharedKeepRules
}

-keepclassmembers class io.yourcompany.yourpackage.** {
    -mySharedKeepRules
}

This way, you only write your keep logic once, and reference it for every namespace you need to target. If you ever need to update the rules, you only have to edit one place.

Option 2: Separate (But Duplicate-Free) Rules

If you prefer not to use custom variables, you can write separate -keepclassmembers lines for each namespace, but copy-paste the rule body. While this is straightforward, it's less maintainable—any future changes to the rules will require updating every instance. Example:

-keepclassmembers class com.yourcompany.yourpackage.** {
    public <methods>;
    private static final <fields>;
}

-keepclassmembers class io.yourcompany.yourpackage.** {
    public <methods>;
    private static final <fields>;
}

Key Takeaway

You can't use regex-style grouping like [com|io] to match multiple prefixes in a single line, but using custom variables lets you reuse your keep rules across as many namespaces as needed without repetition.

内容的提问来源于stack exchange,提问作者Steven Jeuris

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 18:52:48