1.
apply plugin: 'com.google.protobuf'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.1'
}
}
android {
sourceSets {
main {
proto {
srcDir 'src/main/proto'
include '**/*.proto'
}
java {
srcDir 'src/main/java'
}
}
}
}
protobuf {
plugins {
javalite {
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
}
}
generateProtoTasks {
all().each { task ->
task.plugins {
javalite { }
}
}
}
generatedFilesBaseDir = "$projectDir/src/generated"
}
2.
compile 'com.google.protobuf:protobuf-lite:3.0.0'
syntax = "proto2";
option java_package = "com.xxx.xxx.bean";
option java_outer_classname = "PersonEntity";//生成的数据访问类的类名
message Person {
required int32 id = 1;
required string name = 2;
optional string email = 3;
}
mAsyncHandler.post(()->{
PersonEntity.Person person = PersonEntity.Person.newBuilder()
.setId(11)
.setName("tom")
.setEmail("979713144@qq.com")
.build();
File dir = Environment.getExternalStorageDirectory();
File file = new File(dir, "book");
try {
FileOutputStream outputStream = new FileOutputStream(file);
person.writeTo(outputStream);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
});
mAsyncHandler.post(()->{
File dir2 = Environment.getExternalStorageDirectory();
File file2 = new File(dir2, "book");
try {
FileInputStream inputStream = new FileInputStream(file2);
PersonEntity.Person person2 = PersonEntity.Person.parseFrom(inputStream);
log("person = " + person2.toString());
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
});