kwogger
kwogger

Reputation: 1422

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?

Upvotes: 8

Views: 9031

Answers (4)

SJoshi
SJoshi

Reputation: 1976

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.

Upvotes: 1

Henryk Konsek
Henryk Konsek

Reputation: 9168

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

Upvotes: 0

davetapley
davetapley

Reputation: 17968

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.

Upvotes: 3

emmby
emmby

Reputation: 100462

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.

Upvotes: 6

Related Questions