English 中文(简体)
Android and Protocol Buffers
原标题:

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?

最佳回答

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.

问题回答

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.

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.

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





相关问题
Choosing the right subclass to instantiate programmatically

Ok, the context is some serialization / deserialization code that will parse a byte stream into an object representation that s easier to work with (and vice-versa). Here s a simplified example ...

WCF Problem Sending Object To Client

Background Converting from using .Net Remoting to WCF. Most of the methods on the WCF server are working fine, but ran into one that isn t working today. This is the service contract: [...

WCF DataMember Serializing questions

Ok, so I was part way through the long winded process of creating DTOs for sending my model over the wire and I don t feel like I m going down the right route. My issue is that most of the entities ...

deserialize the XML Document --- Need Help

I am using the below code snippet now to deserialize the XML document ... [WebMethod] public XmlDocument OrderDocument(XmlDocument xmlDoc) { XmlSerializer serializer = new XmlSerializer(typeof(...

How do I serialize a child class?

how do I include the serialized data from a child class where both impliment iserializeable? Class A Implements ISerializable dim _B as new B Class B Implements ISerializable ...

WCF: Serialize complex objects with read-only members

Looking for some guidance on a WCF service I’m prototyping. I have a WCF service hosted in IIS that will pass data to my clients. I have a separate shared assembly that contains all my business ...

Long/multiple SQL queries vs Serialization

I m trying to improve the performance of my web app where a page does a long query to pull data from different tables on a database. It pulls invoice data with multiple item lines, invoice status, and ...

热门标签