Android 和协议缓冲区

发布于 2024-08-11 04:19:18 字数 717 浏览 3 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鸢与 2024-08-18 04:19:18

我知道这不是对你的问题的直接答案,但额外的 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.

请别遗忘我 2024-08-18 04:19:18

还有其他轻量级替代品吗?

我认为这意味着“使用协议缓冲区”,而不是“在 Android 应用程序中使用协议缓冲区”。如果您已经致力于协议缓冲区,我深表歉意。

网站旨在“比较序列化性能以及序列化库的其他方面” JVM”。您会发现那里列出了许多替代方案。

虽然目前没有提及不同实现的内存占用,但我确信这是 wiki 上的人们会感兴趣的一个指标。

Are there any other lightweight alternatives?

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.

帅气尐潴 2024-08-18 04:19:18

为了让任何人都能看到这个古老的线程,答案是使用 Square 的 Wire 库 (https://github.com /square/wire

正如他们自己提到的:

有线消息声明公共最终字段而不是通常的 getter 方法。这减少了生成的代码和执行的代码。更少的代码对于 Android 程序特别有利。

我相信他们也在内部使用 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:

Wire messages declare public final fields instead of the usual getter methods. This cuts down on both code generated and code executed. Less code is particularly beneficial for Android programs.

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.

柳若烟 2024-08-18 04:19:18

在您的项目中使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文