Android ProtoBuf

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'

1778289-45704bd5180d4733.png

image.png

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();
                    }
                });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值