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

即时应用场景下,如何在代码混淆时保留指定类并缩短类名?

Shortening Class Names While Keeping Accessibility for Instant Apps

Great question—this is such a common pain point when working with large codebases in instant apps, where you need to balance obfuscation (to keep APK sizes lean) and cross-module accessibility (so feature modules can reliably call into your base module classes). Let’s break down a tailored solution for your 3000-target-class scenario.

Core Rule Strategy: Keep Classes, Allow Renaming, and Trim Unused Code

You’ve already used -keep and -keepclassmembers to fix "class not found" errors, but to shorten class names while ditching unnecessary fields/members, you’ll want to combine those with allowobfuscation and allowshrinking flags. Here’s the baseline rule:

# Keep the class itself (so feature modules can resolve it) but allow renaming
# and remove any unused classes from your 3000-target list
-keep,allowshrinking,allowobfuscation class com.your.base.module.package.** {
    # Explicitly exclude all fields (since you don't need to retain them)
    !<fields>;
    
    # Only keep methods that are actually called by feature modules
    # If no methods need retention, you can omit this line or use !<methods>;
    public <methods>;
}

What Each Flag Does:

  • allowobfuscation: Lets ProGuard rename the class to a short identifier (like a, b, etc.) instead of preserving the full original name.
  • allowshrinking: Automatically removes any classes from your 3000-target list that aren’t actually used by feature modules—cuts down on unnecessary bloat.
  • !<fields>: Excludes all member variables from being retained, aligning with your requirement to skip preserving class members.

Handling Your 3000-Class List Efficiently

Writing 3000 separate rules manually is a nightmare—use this workflow instead:

  1. Create a dedicated ProGuard file (e.g., base-module-targets.pro)
  2. Add a rule for each curated class using the same pattern:
# Example entry for one class
-keep,allowshrinking,allowobfuscation com.your.base.module.package.ClassX {
    !<fields>;
    public <methods>;
}
-keep,allowshrinking,allowobfuscation com.your.base.module.package.ClassY {
    !<fields>;
    public <methods>;
}
# Repeat for all 3000 classes
  1. Import this file into your main ProGuard config to keep things organized:
-include path/to/base-module-targets.pro

Critical Instant App Checkpoints

  • Consistent Mapping Across Modules: Instant apps rely on consistent code references between modules. Generate a mapping file with -printmapping proguard-mapping.txt in your base module, and reuse this mapping in all feature modules. This ensures shortened class names stay uniform across your app’s modules.
  • Validate Retained Classes: Use -printseeds seeds.txt to generate a list of all classes/members ProGuard retained. Cross-check this against your 3000-target list to ensure nothing critical was accidentally removed.
  • Edge Case: Reflection: If any of your classes are accessed via reflection, you’ll need to add explicit rules to retain those specific methods (but since you don’t need to preserve members, this only applies if reflection is used for method calls).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:21:02