protobuf on android
Introduction
Protocol buffers are Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.
This article is about how to setup protobuf on android.
Android app settings
Following settings is not mandatory, it’s just the pre-settings of the sample app.
language: Kotlin
minSdkVersion 26
minSdkVersion 29
multiDexEnabled true
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Steps
Add plugin for gradle
The latest version is 0.8.12. It requires at least Gradle 5.6 and Java 8. It is available on Maven Central. To add dependency to it in the build.gradle of root:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.12'
}
}
Add plugin to your project
apply plugin: 'com.android.application' // or 'com.android.library'
apply plugin: 'com.google.protobuf'
Customizing source directories
-
Create directory app/src/main/proto.
-
Put *.proto files to app/src/main/proto.
Add dependency
Add dependency to app build.gradle and configure protobuf tasks.
dependencies {
// You need to depend on the lite runtime library, not protobuf-java
compile 'com.google.protobuf:protobuf-javalite:3.8.0'
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.8.0'
}
generateProtoTasks {
all().each { task ->
task.builtins {
java {
option "lite"
}
}
}
}
}
Everything’s ok, clean project and build. you can use the generated class.