如何使用ProGuard混淆接口方法?配置未生效求助
Hey there, let's break down why your current rule isn't working and get you a solution that actually does what you need.
First, the problem with your original rule:
-keep public interface * { !** <methods>; }
That syntax isn't valid in ProGuard. The !** pattern doesn't work like that inside the method block of a -keep rule—ProGuard doesn't recognize it, so the rule gets ignored entirely.
Scenario 1: Keep Public Interfaces, Obfuscate Their Methods
If your goal is to keep all public interfaces intact (not deleted or renamed) but obfuscate their method names, use these rules:
# Keep the public interface definitions themselves (prevents deletion/renaming) -keep public interface * { <methods>; # Ensures methods aren't removed, but allows name obfuscation } # Explicitly allow obfuscation of the interface method names -allowobfuscation public interface * { <methods>; }
Scenario 2: Keep Specific Interface Methods, Obfuscate the Rest
If you need to preserve certain methods (like a callback method) but obfuscate all others, adjust the rules to target only the methods you want to keep:
# Keep the public interface itself -keep public interface * { # Retain this specific method's name and signature public void onCompleted(); # Include all other methods (they'll be eligible for obfuscation) <methods>; } # Allow obfuscation for all methods except the one we explicitly kept -allowobfuscation public interface * { !public void onCompleted(); <methods>; }
Important Note: Interface Implementations
If your interfaces are implemented by other classes, ProGuard will retain interface method names to maintain consistency with the implemented methods. To fix this, you also need to allow obfuscation of the implemented methods:
# Allow obfuscation of methods in classes that implement any interface -allowobfuscation class * implements * { <methods>; }
This ensures that both the interface methods and their implementations get their names obfuscated together, avoiding breaking changes.
内容的提问来源于stack exchange,提问作者Sony

