Android 和协议缓冲区
我正在编写一个 Android 应用程序,它既可以存储数据,又可以使用协议缓冲区与服务器通信。不过,使用 LITE 标志编译的协议缓冲区的标准实现(在 JAR 库和生成的 .java 文件)的开销约为 30 KB,而程序本身只有约 30 KB。换句话说,协议缓冲区使程序大小增加了一倍。
在网上搜索时,我找到了参考到 Android 特定实现。不幸的是,似乎没有关于它的文档,并且从标准 .proto 文件生成的代码与其不兼容。有人用过吗?如何从 .proto 文件生成此实现的代码?还有其他轻量级的替代品吗?
I am writing an Android application that would both store data and communicate with a server using protocol buffers. However, the stock implementation of protocol buffers compiled with the LITE flag (in both the JAR library and the generated .java files) has an overhead of ~30 KB, where the program itself is only ~30 KB. In other words, protocol buffers doubled the program size.
Searching online, I found a reference to an Android specific implementation. Unfortunately, there seems to be no documentation for it, and the code generated from the standard .proto file is incompatible with it. Has anyone used it? How do I generate code from a .proto file for this implementation? Are there any other lightweight alternatives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道这不是对你的问题的直接答案,但额外的 30kb 对我来说听起来并没有那么糟糕。即使在 EDGE 上,下载也只需要额外 1 到 2 秒的时间。 Android 上的内存很紧张,但也不是那么紧张——30 kb 大约只是可用应用程序内存空间的百分之一的 1/10。
I know it's not a direct answer to your question, but an extra 30kb doesn't sound that bad to me. Even on EDGE that'll only take an extra 1 to 2 seconds to download. And memory is tight on android, but not THAT tight -- 30 kb is only about 1/10th of one percent of the available application memory space.
我认为这意味着“使用协议缓冲区”,而不是“在 Android 应用程序中使用协议缓冲区”。如果您已经致力于协议缓冲区,我深表歉意。
此网站旨在“比较序列化性能以及序列化库的其他方面” JVM”。您会发现那里列出了许多替代方案。
虽然目前没有提及不同实现的内存占用,但我确信这是 wiki 上的人们会感兴趣的一个指标。
I'm taking this to mean "to using protocol buffers", rather than "for using protocol buffers with an Android application". I apologise if you are already commited to protocol buffers.
This site is about "comparing serialization performance and other aspects of serialization libraries on the JVM". You'll find many alternatives listed there.
While there is no mention of the memory footprint of the different implementations at the moment I am sure it is a metric which the people on the wiki would be interested in.
为了让任何人都能看到这个古老的线程,答案是使用 Square 的 Wire 库 (https://github.com /square/wire)
正如他们自己提到的:
我相信他们也在内部使用 Lite 运行时进行构建。
当然还有 Proguard、新的 Android 2.0 缩小工具、[其他通用答案]等等。
Just to revive this archaic thread for anyone seeing it, the answer is to use Square's Wire library (https://github.com/square/wire)
As they mention themselves:
They also internally build using the Lite runtime I believe.
And of course Proguard, the new Android 2.0 minify tools, [other generic answers], etc etc.
在您的项目中使用 ProGuard[1]。它将减少 APK 文件中包含的 jar 的大小。
[1] http://developer.android.com/guide/developing/tools/混淆器.html
Use ProGuard[1] on your project. It will reduce the size of jars included in APK file.
[1] http://developer.android.com/guide/developing/tools/proguard.html