What are features of using Proguard in project with Protocol Buffers?

梦想与她 提交于 2019-12-05 21:15:54
Jon Skeet

It looks like this may just be a matter of the package being misaligned. Look at the error:

 Caused by: java.lang.RuntimeException: Generated message class 
     "org.mypackage.Control$MessageControlHandCard$Builder" 
      missing method "getCardId".
 ...

So that's org.mypackage.Control.

Now look at your Proguard configuration:

-keep public class org.mypackage.messages.* {
}

That's using org.mypackage.messages, which wouldn't include org.mypackage.Control.

Now presumably those aren't your real package names - but if they're representative, it sounds like you need to change your .proto file to emit classes in the package org.mypackage.messages instead of org.mypackage. (You could change your Proguard configuration instead, but it sounds like that would pick up too much.)

Alternatively, you might be able to just use the inheritance tree. I'm not a Proguard user myself, but judging by the examples, you might want:

-keep public class * extends com.google.protobuf.GeneratedMessage

I'd expect that to work on all your protocol buffer classes regardless of package. You may find that there are other fields/methods which protobuf expects in the "top level" class though.

EDIT: Looking at the documentation further, it may be that you need:

-keep public class * extends com.google.protobuf.GeneratedMessage { *; }

to preserve all members as well. There are other "keep" options which are looking at instead of -keep, such as -keepnames.

I prefer specifying the field name instead of keeping whole class members, which producing much smaller output.

-keepclassmembers your.class.name {
  int sampleIntField;
  String sampleStringField;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!