Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
7.7k views
in Technique[技术] by (71.8m points)

java - ProGuard: keep implementations for interface annotated with @Keep

I would like to annotate some interfaces in my application with a custom @Keep annotation and configure ProGuard so as to

  • not obfuscate the annotated interface and its methods,
  • not obfuscate implementations for those interface methods in implementing classes.

I tried something like

# Kept interfaces and all their methods
-keep interface @com.foo.bar.annotation.Keep * {
    <methods>;
}

# Classes implementing kept interfaces
-keep class * implements @com.foo.bar.annotation.Keep *

but obviously the syntax is invalid. I tried other things, but the ProGuard documentation and its examples are not really clear about the exact syntax and what is possible under which circumstances.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Sorry for answering my own question, but I just happened to bump into the solution when playing around. Actually it is much simpler than I thought:

# Annotated interfaces (including methods which are also kept in implementing classes)
-keep @com.foo.bar.annotation.Keep interface * {
    *;
}

Obviously it was wrong to specify <methods> on an interface keep clause, maybe because ProGuard only filters for actual method implementations, not mere method declarations as can be found in interface declarations.

The above syntax seems to keep the full interface (class name, method names) plus all implementing method names, which is logical if I think about it, because if I do implement an interface I cannot change (obfuscate) the method names anyway.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...