【Android Protobuf 序列化】Protobuf 使用 ( protobuf-gradle-plugin 插件简介 | Android Studio 中配置插件 | AS 中编译源文件 )
文章目录
一、protobuf-gradle-plugin 插件简介
上一篇博客 【Android Protobuf 序列化】Protobuf 使用 ( protoc 编译器简介 | 下载 protoc 编译器 | 使用 protoc 编译器编译 .proto 源文件 ) 中 , 在命令行中使用 protoc 编译器 , 将 .proto 源文件编译成了 Java 源文件 ;
Google 提供了专门用于编译 .proto 源文件的 Gradle 插件 protobuf-gradle-plugin ;
protobuf-gradle-plugin 项目地址 : https://github.com/google/protobuf-gradle-plugin
在 Android Studio 中 , 借助该 protobuf-gradle-plugin Gradle 插件 , 可以自动完成 Protobuf 源文件的编译工作 ;
protobuf-gradle-plugin 插件配置方法 , 参考 https://github.com/google/protobuf-gradle-plugin 项目主页的 MarkDown 文档 ;
二、Android Studio 中配置 protobuf-gradle-plugin 插件
protobuf-gradle-plugin 插件配置 :
- Gradle 版本最低 5.6 5.6 5.6 ,
- Java 版本最低 8.0 8.0 8.0 ,
- 开启 Maven Central 仓库
mavenCentral()
, - 最新版本是 0.8.16 0.8.16 0.8.16 ;
在 Project 的 build.gradle 中进行如下配置 :
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.16'
}
}
在 Module 的 build.gradle 中进行如下配置 :
apply plugin: 'com.android.application' // or 'com.android.library'
apply plugin: 'com.google.protobuf'
如果使用 Protobuf 3.0 ~ 3.7 之间的版本 , 使用的是 protobuf-lite 版本 , 该版本相对于普通的 Protobuf , 生成的源文件更精简 , 这是为了适配在 Android 设备上使用而定制的 , 更适合移动端使用 ;
dependencies {
// You need to depend on the lite runtime library, not protobuf-java
implementation 'com.google.protobuf:protobuf-lite:3.0.0'
}
protobuf {
protoc {
// You still need protoc like in the non-Android case
artifact = 'com.google.protobuf:protoc:3.7.0'
}
plugins {
javalite {
// The codegen for lite comes as a separate artifact
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
// In most cases you don't need the full Java output
// if you use the lite output.
remove java
}
task.plugins {
javalite { }
}
}
}
}
如果使用 Protobuf 3.8 之后的版本 , 使用如下设置 :
dependencies {
// You need to depend on the lite runtime library, not protobuf-java
implementation '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"
}
}
}
}
}
三、Android Studio 中编译 Protobuf 源文件
配置完成后 , 选择 " 菜单栏 / Build / Make Project " 编译整个工程 ,
编译完成后 , 在 " app\build\generated\source\proto\debug\javalite " 目录下生成 Protobuf 源码对应的 Java 类 , AddressBook.java 源文件生成在 " app\build\generated\source\proto\debug\javalite\com\example\tutorial\protos " 目录中 ;
Android Studio 会将 Protobuf 生成的这些 Java 类引入到当前项目的 Classpath 中 , 并打包到 APK 中 , 在开发时 , 可以引用这些类 ;
四、参考资料
Protobuf 参考资料 :
-
Protobuf 官网主页 : https://developers.google.com/protocol-buffers
-
Protobuf 语法指南 : https://developers.google.com/protocol-buffers/docs/proto
-
Protobuf Java 语言对应用法 : https://developers.google.com/protocol-buffers/docs/javatutorial
-
Protobuf 下载地址 : https://developers.google.com/protocol-buffers/docs/downloads
-
Protobuf 源码地址 : https://github.com/protocolbuffers/protobuf
-
Protobuf 发布版本下载地址 : https://github.com/protocolbuffers/protobuf/releases
-
protobuf-gradle-plugin 项目地址 : https://github.com/google/protobuf-gradle-plugin
博客源码 :
相关文章
- Android Studio JNI体验
- Android自定义控件ImageViwe(四)——多点触控实现图片的自由移动
- Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
- Android精通:View与ViewGroup,LinearLayout线性布局,RelativeLayout相对布局,ListView列表组件
- Android studio的错误:radle sync failed: Cause: failed to find target android-21 :
- android颜色值的表示方法android:background="#FFFFFFFF"的意思
- Android Studio导入第三方类库的方法
- Android 数据库升级解决方案
- Android实现截图分享qq,微信
- Android studio开多个窗口引起的问题
- android 开发 解码gif图片,获取每帧bitmap
- debian sid 安装android studio
- Android Studio 工具之 Json 工具 Gson 的源码和各个版本 jar 下载地址整理
- Android Studio 常见错误 之 Fragment null must be a public static class to be properly recreated 的问题处理
- Android Studio 键盘快捷键
- android studio 使用本地gradle的配置
- android studio 使用本地gradle的配置
- Android Studio 关联第三方模拟器_ADB命令配置和VT设置教程链接
- Android Studio 关联第三方模拟器_ADB命令配置和VT设置教程链接
- Android Studio手动配置Gradle的方法
- Android studio 编译项目时报错: Gradle 'XXX' project refresh failed