使用AndroidStudio处理Google Protobuf文件转Java文件
来源:互联网 发布:网络电视用什么播放器 编辑:程序博客网 时间:2024/06/08 01:50
Google Protobuf文件转Java文件
Protobuf协议目前分两个版本 protobuf v3 与protobuf v2两个版本
项目中使用Protobuf作为数据传输,使用AndroidStudio工具批量编译protobuf文件比较简单。
开始配置环境
protobuf文件
option java_package = "com.daycodeday.test";option java_outer_classname = "MtmColWrapper";message MtmCol {optional uint32 colnamelength = 1;required string colname = 2;optional uint32 coltype = 3;optional uint32 coltypestringlength = 4;optional string coltypestring = 5;optional uint32 length = 6;optional uint32 precision = 7;optional uint32 scale = 8;optional uint32 nullable = 9; }
项目根目录配置build.gradle
dependencies { classpath 'com.android.tools.build:gradle:2.3.3' // protobuf支持版本 classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0' }
modle根目录配置build.gradle
//依赖支持apply plugin: 'com.google.protobuf'
//指定文件目录 sourceSets { main { proto { //main目录新建proto目录 srcDir 'src/main/proto' include '**/*.proto' } java { srcDir 'src/main/java' } } }
//依赖库compile 'com.google.protobuf:protobuf-java:2.5.0'compile 'com.google.protobuf:protoc:2.5.0'
//构建taskprotobuf { protoc { artifact = 'com.google.protobuf:protoc:2.5.0' } generateProtoTasks { all().each { task -> task.builtins { remove java } task.builtins { java {} // Add cpp output without any option. // DO NOT omit the braces if you want this builtin to be added. cpp {} } } } //生成目录 generatedFilesBaseDir = "$projectDir/src/generated"}
我使用依赖库是 2.5 ,如果是3.0以上依赖库的需要定义下协议头
3.0 头部开始加上syntax = "proto3";2.0 头部加上syntax = "proto2";
下面是完整的gradle文件
apply plugin: 'com.android.application'apply plugin: 'com.google.protobuf'android { compileSdkVersion 25 buildToolsVersion "25.0.3" defaultConfig { applicationId "com.daycodeday.myapplication" minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } sourceSets { main { proto { srcDir 'src/main/proto' include '**/*.proto' } java { srcDir 'src/main/java' } } }}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile 'com.google.protobuf:protobuf-java:3.1.0' compile 'com.google.protobuf:protoc:3.1.0'}protobuf { protoc { artifact = 'com.google.protobuf:protoc:3.1.0' } generateProtoTasks { all().each { task -> task.builtins { remove java } task.builtins { java {} // Add cpp output without any option. // DO NOT omit the braces if you want this builtin to be added. cpp {} } } } //生成目录 generatedFilesBaseDir = "$projectDir/src/generated"}
阅读全文
0 0
- 使用AndroidStudio处理Google Protobuf文件转Java文件
- protobuf转java文件
- Google protobuf使用之proto文件编写规则(二)
- 【Proto文件】Google开源技术 Protobuf 简介与使用
- Protobuf详解(.Java文件)
- google protobuf java 简单使用
- [转]Google Protobuf 使用介绍
- android使用wire方式生成protobuf的Java文件
- 【JAVA】google protobuf 3.0 安装使用
- androidstudio使用Svn忽略文件
- AndroidStudio使用properties资源文件
- AndroidStudio R.java文件问题
- 解决:AndroidStudio 下使用AIDL不能生成对应java文件
- AndroidStudio 下使用AIDL不能生成对应java文件
- 在lua环境中使用protobuf ,编译protobuf文件
- 使用Java处理大文件
- 使用Java处理大文件
- 使用Java处理大文件
- 机器学习
- 国产xilinx Jtag VS 原装xilinx小红盒
- 猫奴大数据_你中枪了吗?
- 链表的实现
- 搭建Sunny ngrok实现内外网通讯
- 使用AndroidStudio处理Google Protobuf文件转Java文件
- python学习笔记(21)——匿名函数
- 关于canvas画布的操作,看这一篇就足够了!
- HDU6034Balala Power!(大数进制转换)
- python 爬虫学习
- POJ
- 《Python自然语言处理》学习笔记-第二章
- nested exception is org.apache.ibatis.binding.BindingException: Invalid (not found)
- 滑动html