kotlin plugins { id("com.google.protobuf") version "0.8.17" } dependencies { implementation("com.google.protobuf:protobuf-java:3.17.3") implementation("com.google.protobuf:protobuf-java-util:3.17.3") implementation("com.google.protobuf:protobuf-gradle-plugin:0.8.17") } protobuf syntax = "proto3"; package com.example.protobuf; message Person { string name = 1; int32 age = 2; repeated string hobbies = 3; } shell kotlin val person = Person.newBuilder() .setName("John Doe") .setAge(25) .addHobbies("Reading") .addHobbies("Gaming") .build() val serializedBytes = person.toByteArray() kotlin val deserializedPerson = Person.parseFrom(serializedBytes) kotlin val outputStream = FileOutputStream("person.pb") person.writeTo(outputStream) outputStream.close() kotlin val inputStream = FileInputStream("person.pb") val deserializedPerson = Person.parseFrom(inputStream) inputStream.close()


上一篇:
下一篇:
切换中文