android generated java files,Android protobuf-javalite 实践

环境

windows 10

AndroidStudio 3.0.1

protobuf-javalite 版本

初始配置

1. project.gradle

dependencies {

classpath 'com.android.tools.build:gradle:3.0.1'

// protobuf支持版本,AS3.0必须用0.8.2以上

classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.8'

// NOTE: Do not place your application dependencies here; they belong

// in the individual module build.gradle files

}

2. modle.gradel

apply plugin: 'com.google.protobuf'

android {

sourceSets {

main {

proto {

srcDir 'src/main/proto' //proto文件所在路径

include '**/*.proto'

}

java {

srcDir 'src/main/java'

}

}

}

}

protobuf {

protoc {

// You still need protoc like in the non-Android case

artifact = 'com.google.protobuf:protoc:3.0.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 { }

}

}

}

//将会在 "$projectDir/src/generated"这个目录中自动生成对应的java文件

generatedFilesBaseDir = "$projectDir/src/generated"

}

dependencies {

compile 'com.google.protobuf:protobuf-lite:3.0.0'

}

3. AndroidStudio中安装插件:

0da79242babc?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

4. 编写.proto文件

因为在gradle中设置了proto文件的可编译目录,所以需要在这个目录中编写.proto文件

(参考网上教程写了proto文件,但具体网址不记得了,不好意思,如果需要我会备注)

syntax = "proto3";

package tutorial;

option java_package = "com.je.pro.test";

option java_outer_classname = "ResponsePB";

message Tab {

int32 type = 1;

string f = 2;

}

message ItemData {

string sname = 1;

string packageid = 2;

repeated Tab tabs = 45;

}

message DataItem {

int32 datatype = 1;

ItemData itemdata = 2;

}

message Response {

repeated DataItem data = 1;

bool hasNextPage = 2;

string dirtag = 3;

}

初次使用,proto的语法都是参考网上教程,若有失误,谢谢指正。

写完后,点击

0da79242babc?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

即可自动生成java文件。

生成目录结构:

0da79242babc?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

数据生成和解析

1. 数据生成

代码:

public byte[] testGetBytes(){

ResponsePB.Tab.Builder tabBuilder = ResponsePB.Tab.newBuilder().setF("sss").setType(2);

ResponsePB.ItemData.Builder itemData = ResponsePB.ItemData.newBuilder();

itemData.setPackageid("222222");

itemData.setSname("eiiii");

itemData.addTabs(tabBuilder);

ResponsePB.Response.Builder responseBuilder = ResponsePB.Response.newBuilder();

responseBuilder.setHasNextPage(true);

responseBuilder.setDirtag("soft");

ResponsePB.DataItem.Builder dataItem = ResponsePB.DataItem.newBuilder().setDatatype(1).setItemdata(itemData);

ResponsePB.Response response = responseBuilder.addData(dataItem).build();

System.out.println(response.toString());

byte[] out = response.toByteArray();

return out;

}

打印出

data {

datatype: 1

itemdata {

packageid: "222222"

sname: "eiiii"

tabs {

f: "sss"

type: 2

}

}

}

dirtag: "soft"

has_next_page: true

1. 解析

代码:

public void testDeBytes(){

byte[] out = testGetBytes();

try {

ResponsePB.Response test = ResponsePB.Response.parseFrom(out);

System.out.println(test.getData(0));

} catch (InvalidProtocolBufferException e) {

e.printStackTrace();

}

}

打印出:

datatype: 1

itemdata {

packageid: "222222"

sname: "eiiii"

tabs {

f: "sss"

type: 2

}

}

这样简单的应用就完成了。

项目中遇到问题与解决:

1. 需要重利用数据:option allow_alias = true;

enum Test{

option allow_alias = true;

test_value=2;

duplicate_test_value=2;

}

原本在enum中不能定义相同的值,但加入option allow_alias = true; 就可以了

2. repeated变量使用

正确方式

ResponsePB.Tab.Builder tabBuilder = ResponsePB.Tab.newBuilder().setF("sss").setType(2);

ResponsePB.ItemData.Builder itemData = ResponsePB.ItemData.newBuilder();

itemData.addTabs(tabBuilder);

错误方式

ResponsePB.Tab.Builder tabBuilder = ResponsePB.Tab.newBuilder().setF("sss").setType(2);

ResponsePB.ItemData.Builder itemData = ResponsePB.ItemData.newBuilder();

itemData.setTabs(0,tabBuilder);

报异常:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

at java.util.ArrayList.rangeCheck(ArrayList.java:653)

at java.util.ArrayList.set(ArrayList.java:444)

at com.google.protobuf.ProtobufArrayList.set(ProtobufArrayList.java:96)

at com.je.pro.test.ResponsePB$ItemData.setTabs(ResponsePB.java:590)

at com.je.pro.test.ResponsePB$ItemData.access$1500(ResponsePB.java:429)

at com.je.pro.test.ResponsePB$ItemData$Builder.setTabs(ResponsePB.java:881)

at com.je.pro.ExampleUnitTest.testByte(ExampleUnitTest.java:91)

这个异常可以参考ArrayList.set() 方法,不要怀疑,我真的直接set了

附上ArrayList add() set() 源码

/**

* Replaces the element at the specified position in this list with

* the specified element.

*

* @param index index of the element to replace

* @param element element to be stored at the specified position

* @return the element previously at the specified position

* @throws IndexOutOfBoundsException {@inheritDoc}

*/

public E set(int index, E element) {

if (index >= size)

throw new IndexOutOfBoundsException(outOfBoundsMsg(index));

E oldValue = (E) elementData[index];

elementData[index] = element;

return oldValue;

}

/**

* Appends the specified element to the end of this list.

*

* @param e element to be appended to this list

* @return true (as specified by {@link Collection#add})

*/

public boolean add(E e) {

ensureCapacityInternal(size + 1); // Increments modCount!!

elementData[size++] = e;

return true;

}

参考网站

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
protobuf-maven-plugin 是一个 Maven 插件,用于在 Maven 项目中自动生成和编译 Protocol Buffers(简称为 protobuf)文件。Protocol Buffers 是一种语言无关、平台无关的数据序列化格式,可用于结构化数据的存储和交换。 使用 protobuf-maven-plugin,您可以在 Maven 构建过程中自动执行以下任务: - 根据 protobuf 文件生成对应的 Java 类 - 编译生成的 Java 类 - 将生成的 Java 类打包到 JAR 文件中 为了使用 protobuf-maven-plugin,您需要在 Maven 项目的 pom.xml 文件中添加相应的配置。以下是一个示例配置: ```xml <build> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> <goal>test-compile</goal> <goal>test-compile-custom</goal> </goals> </execution> </executions> <configuration> <protocArtifact>com.google.protobuf:protoc:3.15.8:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.38.0:exe:${os.detected.classifier}</pluginArtifact> <protoSourceRoot>${basedir}/src/main/proto</protoSourceRoot> <generatedSourcesDirectory>${project.build.directory}/generated-sources/protobuf</generatedSourcesDirectory> <clearOutputDirectory>false</clearOutputDirectory> </configuration> </plugin> </plugins> </build> ``` 上述配置中,配置了 protobuf-maven-plugin 的版本、执行目标、protobuf 和 grpc-java 的依赖,以及源文件和生成文件的目录等。 在项目中的 src/main/proto 目录下放置 protobuf 文件(扩展名为 .proto),执行 Maven 构建时,protobuf-maven-plugin 将会自动生成对应的 Java 类,并将生成的 Java 类编译成字节码文件。 请注意,protobuf-maven-plugin 需要依赖于 Protocol Buffers 和 grpc-java 的工具,因此需要确保这些工具可用并在配置中正确指定。 希望以上信息能解答您的问题。如果您还有其他问题,请随时提问!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值