├── .gitignore ├── README.md ├── app ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── bqt │ │ └── test │ │ ├── MainActivity.java │ │ └── protomodel │ │ ├── Person.java │ │ └── PersonEntity.java │ ├── proto │ ├── User.proto │ └── test.proto │ └── res │ ├── drawable │ └── icon.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | .idea/ 5 | 6 | # Files for the ART/Dalvik VM 7 | *.dex 8 | 9 | # Java class files 10 | *.class 11 | 12 | # Generated files 13 | bin/ 14 | gen/ 15 | out/ 16 | 17 | # Gradle files 18 | .gradle/ 19 | build/ 20 | 21 | # Local configuration file (sdk path, etc) 22 | local.properties 23 | 24 | # Proguard folder generated by Eclipse 25 | proguard/ 26 | 27 | # Log Files 28 | *.log 29 | 30 | # Android Studio Navigation editor temp files 31 | .navigation/ 32 | 33 | # Android Studio captures folder 34 | captures/ 35 | 36 | # Intellij 37 | *.iml 38 | .idea/workspace.xml 39 | .idea/tasks.xml 40 | .idea/gradle.xml 41 | .idea/dictionaries 42 | .idea/libraries 43 | 44 | # Keystore files 45 | *.jks 46 | 47 | # External native build folder generated in Android Studio 2.2 and later 48 | .externalNativeBuild 49 | 50 | # Google Services (e.g. APIs or Firebase) 51 | google-services.json 52 | 53 | # Freeline 54 | freeline.py 55 | freeline/ 56 | freeline_project_description.json 57 | /gradlew 58 | /gradlew.bat 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ProtocolBufferTest 2 | Protocol Buffer 使用演示 3 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | defaultConfig { 6 | applicationId "com.bqt.test.pb" 7 | minSdkVersion 17 8 | targetSdkVersion 27 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:27.1.1' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.2' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | 29 | implementation 'com.google.code.gson:gson:2.8.1' 30 | //implementation 'com.google.protobuf:protobuf-java:3.6.1'//protobuf 31 | implementation 'com.google.protobuf:protobuf-lite:3.0.1'//protobuf lite 32 | } 33 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/bqt/test/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.bqt.test; 2 | 3 | import android.app.ListActivity; 4 | import android.os.Bundle; 5 | import android.os.Environment; 6 | import android.util.Log; 7 | import android.view.View; 8 | import android.widget.ArrayAdapter; 9 | import android.widget.ListView; 10 | 11 | import com.bqt.test.protomodel.Person; 12 | import com.bqt.test.protomodel.PersonEntity; 13 | import com.google.gson.Gson; 14 | import com.google.protobuf.InvalidProtocolBufferException; 15 | 16 | import java.io.File; 17 | import java.io.FileInputStream; 18 | import java.io.FileOutputStream; 19 | import java.io.IOException; 20 | import java.util.Arrays; 21 | 22 | public class MainActivity extends ListActivity { 23 | 24 | private static String protoFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test_proto"; 25 | private static String jsonfilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test_json"; 26 | 27 | @Override 28 | protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | String[] array = {"以proto格式保存对象", 31 | "解析proto方式保存的对象内容", 32 | "以Json格式保存对象", 33 | "解析son方式保存的对象内容", 34 | "",}; 35 | setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Arrays.asList(array))); 36 | } 37 | 38 | @Override 39 | protected void onListItemClick(ListView l, View v, int position, long id) { 40 | switch (position) { 41 | case 0: 42 | PersonEntity.Person protoPerson = PersonEntity.Person.newBuilder() 43 | .setId(3) 44 | .setName("zhangsan") 45 | .setEmail("test@qq.com") 46 | .build(); 47 | Log.i("bqt", "原始的对象内容:" + protoPerson.toString()); 48 | writeToFile(protoPerson.toByteArray(), protoFilePath);//以proto格式保存对象,保存的长度=25 49 | break; 50 | case 1: 51 | byte[] bytes = readFromFile(protoFilePath); 52 | try { 53 | PersonEntity.Person protoPerson2 = PersonEntity.Person.parseFrom(bytes);//解析proto方式保存的对象内容 54 | Log.i("bqt", protoPerson2.toString()); 55 | } catch (InvalidProtocolBufferException e) { 56 | e.printStackTrace(); 57 | } 58 | break; 59 | case 2: 60 | Person jsonPerson = Person.newBuilder().id(3).name("zhangsan").email("test@qq.com").build(); 61 | Log.i("bqt", "原始的对象内容:" + jsonPerson.toString()); 62 | writeToFile(new Gson().toJson(jsonPerson).getBytes(), jsonfilePath);//以Json格式保存对象,保存的长度=48 63 | break; 64 | case 3: 65 | byte[] bytes2 = readFromFile(jsonfilePath); 66 | String json = new String(bytes2); 67 | Person jsonPerson2 = new Gson().fromJson(json, Person.class); 68 | Log.i("bqt", jsonPerson2.toString()); 69 | break; 70 | } 71 | } 72 | 73 | private byte[] readFromFile(String path) { 74 | try { 75 | FileInputStream input = new FileInputStream(path); 76 | byte[] personBytes = new byte[(int) new File(path).length()]; 77 | input.read(personBytes); 78 | input.close(); 79 | Log.i("bqt", "解析的长度=" + personBytes.length); 80 | return personBytes; 81 | } catch (IOException e) { 82 | e.printStackTrace(); 83 | return new byte[0]; 84 | } 85 | } 86 | 87 | public static void writeToFile(byte[] content, String path) { 88 | Log.i("bqt", "保存的长度=" + content.length); 89 | try { 90 | FileOutputStream out = new FileOutputStream(path,false); 91 | out.write(content); 92 | out.close(); 93 | } catch (IOException e) { 94 | e.printStackTrace(); 95 | } 96 | } 97 | } -------------------------------------------------------------------------------- /app/src/main/java/com/bqt/test/protomodel/Person.java: -------------------------------------------------------------------------------- 1 | package com.bqt.test.protomodel; 2 | 3 | public class Person { 4 | 5 | private String email; 6 | private int id; 7 | private String name; 8 | 9 | private Person(Builder builder) { 10 | email = builder.email; 11 | id = builder.id; 12 | name = builder.name; 13 | } 14 | 15 | public static Builder newBuilder() { 16 | return new Builder(); 17 | } 18 | 19 | public static final class Builder { 20 | private String email; 21 | private int id; 22 | private String name; 23 | 24 | private Builder() { 25 | } 26 | 27 | public Builder email(String val) { 28 | email = val; 29 | return this; 30 | } 31 | 32 | public Builder id(int val) { 33 | id = val; 34 | return this; 35 | } 36 | 37 | public Builder name(String val) { 38 | name = val; 39 | return this; 40 | } 41 | 42 | public Person build() { 43 | return new Person(this); 44 | } 45 | } 46 | 47 | @Override 48 | public String toString() { 49 | return "Person{" + 50 | "email='" + email + '\'' + 51 | ", id=" + id + 52 | ", name='" + name + '\'' + 53 | '}'; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /app/src/main/java/com/bqt/test/protomodel/PersonEntity.java: -------------------------------------------------------------------------------- 1 | // Generated by the protocol buffer compiler. DO NOT EDIT! 2 | // source: test.proto 3 | 4 | package com.bqt.test.protomodel; 5 | 6 | public final class PersonEntity { 7 | private PersonEntity() {} 8 | public static void registerAllExtensions( 9 | com.google.protobuf.ExtensionRegistryLite registry) { 10 | } 11 | public interface PersonOrBuilder extends 12 | // @@protoc_insertion_point(interface_extends:Person) 13 | com.google.protobuf.MessageLiteOrBuilder { 14 | 15 | /** 16 | * optional int32 id = 1; 17 | */ 18 | int getId(); 19 | 20 | /** 21 | * optional string name = 2; 22 | */ 23 | String getName(); 24 | /** 25 | * optional string name = 2; 26 | */ 27 | com.google.protobuf.ByteString 28 | getNameBytes(); 29 | 30 | /** 31 | * optional string email = 3; 32 | */ 33 | String getEmail(); 34 | /** 35 | * optional string email = 3; 36 | */ 37 | com.google.protobuf.ByteString 38 | getEmailBytes(); 39 | } 40 | /** 41 | * Protobuf type {@code Person} 42 | */ 43 | public static final class Person extends 44 | com.google.protobuf.GeneratedMessageLite< 45 | Person, Person.Builder> implements 46 | // @@protoc_insertion_point(message_implements:Person) 47 | PersonOrBuilder { 48 | private Person() { 49 | name_ = ""; 50 | email_ = ""; 51 | } 52 | public static final int ID_FIELD_NUMBER = 1; 53 | private int id_; 54 | /** 55 | * optional int32 id = 1; 56 | */ 57 | public int getId() { 58 | return id_; 59 | } 60 | /** 61 | * optional int32 id = 1; 62 | */ 63 | private void setId(int value) { 64 | 65 | id_ = value; 66 | } 67 | /** 68 | * optional int32 id = 1; 69 | */ 70 | private void clearId() { 71 | 72 | id_ = 0; 73 | } 74 | 75 | public static final int NAME_FIELD_NUMBER = 2; 76 | private String name_; 77 | /** 78 | * optional string name = 2; 79 | */ 80 | public String getName() { 81 | return name_; 82 | } 83 | /** 84 | * optional string name = 2; 85 | */ 86 | public com.google.protobuf.ByteString 87 | getNameBytes() { 88 | return com.google.protobuf.ByteString.copyFromUtf8(name_); 89 | } 90 | /** 91 | * optional string name = 2; 92 | */ 93 | private void setName( 94 | String value) { 95 | if (value == null) { 96 | throw new NullPointerException(); 97 | } 98 | 99 | name_ = value; 100 | } 101 | /** 102 | * optional string name = 2; 103 | */ 104 | private void clearName() { 105 | 106 | name_ = getDefaultInstance().getName(); 107 | } 108 | /** 109 | * optional string name = 2; 110 | */ 111 | private void setNameBytes( 112 | com.google.protobuf.ByteString value) { 113 | if (value == null) { 114 | throw new NullPointerException(); 115 | } 116 | checkByteStringIsUtf8(value); 117 | 118 | name_ = value.toStringUtf8(); 119 | } 120 | 121 | public static final int EMAIL_FIELD_NUMBER = 3; 122 | private String email_; 123 | /** 124 | * optional string email = 3; 125 | */ 126 | public String getEmail() { 127 | return email_; 128 | } 129 | /** 130 | * optional string email = 3; 131 | */ 132 | public com.google.protobuf.ByteString 133 | getEmailBytes() { 134 | return com.google.protobuf.ByteString.copyFromUtf8(email_); 135 | } 136 | /** 137 | * optional string email = 3; 138 | */ 139 | private void setEmail( 140 | String value) { 141 | if (value == null) { 142 | throw new NullPointerException(); 143 | } 144 | 145 | email_ = value; 146 | } 147 | /** 148 | * optional string email = 3; 149 | */ 150 | private void clearEmail() { 151 | 152 | email_ = getDefaultInstance().getEmail(); 153 | } 154 | /** 155 | * optional string email = 3; 156 | */ 157 | private void setEmailBytes( 158 | com.google.protobuf.ByteString value) { 159 | if (value == null) { 160 | throw new NullPointerException(); 161 | } 162 | checkByteStringIsUtf8(value); 163 | 164 | email_ = value.toStringUtf8(); 165 | } 166 | 167 | public void writeTo(com.google.protobuf.CodedOutputStream output) 168 | throws java.io.IOException { 169 | if (id_ != 0) { 170 | output.writeInt32(1, id_); 171 | } 172 | if (!name_.isEmpty()) { 173 | output.writeString(2, getName()); 174 | } 175 | if (!email_.isEmpty()) { 176 | output.writeString(3, getEmail()); 177 | } 178 | } 179 | 180 | public int getSerializedSize() { 181 | int size = memoizedSerializedSize; 182 | if (size != -1) return size; 183 | 184 | size = 0; 185 | if (id_ != 0) { 186 | size += com.google.protobuf.CodedOutputStream 187 | .computeInt32Size(1, id_); 188 | } 189 | if (!name_.isEmpty()) { 190 | size += com.google.protobuf.CodedOutputStream 191 | .computeStringSize(2, getName()); 192 | } 193 | if (!email_.isEmpty()) { 194 | size += com.google.protobuf.CodedOutputStream 195 | .computeStringSize(3, getEmail()); 196 | } 197 | memoizedSerializedSize = size; 198 | return size; 199 | } 200 | 201 | public static Person parseFrom( 202 | com.google.protobuf.ByteString data) 203 | throws com.google.protobuf.InvalidProtocolBufferException { 204 | return com.google.protobuf.GeneratedMessageLite.parseFrom( 205 | DEFAULT_INSTANCE, data); 206 | } 207 | public static Person parseFrom( 208 | com.google.protobuf.ByteString data, 209 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 210 | throws com.google.protobuf.InvalidProtocolBufferException { 211 | return com.google.protobuf.GeneratedMessageLite.parseFrom( 212 | DEFAULT_INSTANCE, data, extensionRegistry); 213 | } 214 | public static Person parseFrom(byte[] data) 215 | throws com.google.protobuf.InvalidProtocolBufferException { 216 | return com.google.protobuf.GeneratedMessageLite.parseFrom( 217 | DEFAULT_INSTANCE, data); 218 | } 219 | public static Person parseFrom( 220 | byte[] data, 221 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 222 | throws com.google.protobuf.InvalidProtocolBufferException { 223 | return com.google.protobuf.GeneratedMessageLite.parseFrom( 224 | DEFAULT_INSTANCE, data, extensionRegistry); 225 | } 226 | public static Person parseFrom(java.io.InputStream input) 227 | throws java.io.IOException { 228 | return com.google.protobuf.GeneratedMessageLite.parseFrom( 229 | DEFAULT_INSTANCE, input); 230 | } 231 | public static Person parseFrom( 232 | java.io.InputStream input, 233 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 234 | throws java.io.IOException { 235 | return com.google.protobuf.GeneratedMessageLite.parseFrom( 236 | DEFAULT_INSTANCE, input, extensionRegistry); 237 | } 238 | public static Person parseDelimitedFrom(java.io.InputStream input) 239 | throws java.io.IOException { 240 | return parseDelimitedFrom(DEFAULT_INSTANCE, input); 241 | } 242 | public static Person parseDelimitedFrom( 243 | java.io.InputStream input, 244 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 245 | throws java.io.IOException { 246 | return parseDelimitedFrom(DEFAULT_INSTANCE, input, extensionRegistry); 247 | } 248 | public static Person parseFrom( 249 | com.google.protobuf.CodedInputStream input) 250 | throws java.io.IOException { 251 | return com.google.protobuf.GeneratedMessageLite.parseFrom( 252 | DEFAULT_INSTANCE, input); 253 | } 254 | public static Person parseFrom( 255 | com.google.protobuf.CodedInputStream input, 256 | com.google.protobuf.ExtensionRegistryLite extensionRegistry) 257 | throws java.io.IOException { 258 | return com.google.protobuf.GeneratedMessageLite.parseFrom( 259 | DEFAULT_INSTANCE, input, extensionRegistry); 260 | } 261 | 262 | public static Builder newBuilder() { 263 | return DEFAULT_INSTANCE.toBuilder(); 264 | } 265 | public static Builder newBuilder(Person prototype) { 266 | return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); 267 | } 268 | 269 | /** 270 | * Protobuf type {@code Person} 271 | */ 272 | public static final class Builder extends 273 | com.google.protobuf.GeneratedMessageLite.Builder< 274 | Person, Builder> implements 275 | // @@protoc_insertion_point(builder_implements:Person) 276 | PersonOrBuilder { 277 | // Construct using com.bqt.test.protomodel.PersonEntity.Person.newBuilder() 278 | private Builder() { 279 | super(DEFAULT_INSTANCE); 280 | } 281 | 282 | 283 | /** 284 | * optional int32 id = 1; 285 | */ 286 | public int getId() { 287 | return instance.getId(); 288 | } 289 | /** 290 | * optional int32 id = 1; 291 | */ 292 | public Builder setId(int value) { 293 | copyOnWrite(); 294 | instance.setId(value); 295 | return this; 296 | } 297 | /** 298 | * optional int32 id = 1; 299 | */ 300 | public Builder clearId() { 301 | copyOnWrite(); 302 | instance.clearId(); 303 | return this; 304 | } 305 | 306 | /** 307 | * optional string name = 2; 308 | */ 309 | public String getName() { 310 | return instance.getName(); 311 | } 312 | /** 313 | * optional string name = 2; 314 | */ 315 | public com.google.protobuf.ByteString 316 | getNameBytes() { 317 | return instance.getNameBytes(); 318 | } 319 | /** 320 | * optional string name = 2; 321 | */ 322 | public Builder setName( 323 | String value) { 324 | copyOnWrite(); 325 | instance.setName(value); 326 | return this; 327 | } 328 | /** 329 | * optional string name = 2; 330 | */ 331 | public Builder clearName() { 332 | copyOnWrite(); 333 | instance.clearName(); 334 | return this; 335 | } 336 | /** 337 | * optional string name = 2; 338 | */ 339 | public Builder setNameBytes( 340 | com.google.protobuf.ByteString value) { 341 | copyOnWrite(); 342 | instance.setNameBytes(value); 343 | return this; 344 | } 345 | 346 | /** 347 | * optional string email = 3; 348 | */ 349 | public String getEmail() { 350 | return instance.getEmail(); 351 | } 352 | /** 353 | * optional string email = 3; 354 | */ 355 | public com.google.protobuf.ByteString 356 | getEmailBytes() { 357 | return instance.getEmailBytes(); 358 | } 359 | /** 360 | * optional string email = 3; 361 | */ 362 | public Builder setEmail( 363 | String value) { 364 | copyOnWrite(); 365 | instance.setEmail(value); 366 | return this; 367 | } 368 | /** 369 | * optional string email = 3; 370 | */ 371 | public Builder clearEmail() { 372 | copyOnWrite(); 373 | instance.clearEmail(); 374 | return this; 375 | } 376 | /** 377 | * optional string email = 3; 378 | */ 379 | public Builder setEmailBytes( 380 | com.google.protobuf.ByteString value) { 381 | copyOnWrite(); 382 | instance.setEmailBytes(value); 383 | return this; 384 | } 385 | 386 | // @@protoc_insertion_point(builder_scope:Person) 387 | } 388 | protected final Object dynamicMethod( 389 | MethodToInvoke method, 390 | Object arg0, Object arg1) { 391 | switch (method) { 392 | case NEW_MUTABLE_INSTANCE: { 393 | return new Person(); 394 | } 395 | case IS_INITIALIZED: { 396 | return DEFAULT_INSTANCE; 397 | } 398 | case MAKE_IMMUTABLE: { 399 | return null; 400 | } 401 | case NEW_BUILDER: { 402 | return new Builder(); 403 | } 404 | case VISIT: { 405 | Visitor visitor = (Visitor) arg0; 406 | Person other = (Person) arg1; 407 | id_ = visitor.visitInt(id_ != 0, id_, 408 | other.id_ != 0, other.id_); 409 | name_ = visitor.visitString(!name_.isEmpty(), name_, 410 | !other.name_.isEmpty(), other.name_); 411 | email_ = visitor.visitString(!email_.isEmpty(), email_, 412 | !other.email_.isEmpty(), other.email_); 413 | if (visitor == MergeFromVisitor 414 | .INSTANCE) { 415 | } 416 | return this; 417 | } 418 | case MERGE_FROM_STREAM: { 419 | com.google.protobuf.CodedInputStream input = 420 | (com.google.protobuf.CodedInputStream) arg0; 421 | com.google.protobuf.ExtensionRegistryLite extensionRegistry = 422 | (com.google.protobuf.ExtensionRegistryLite) arg1; 423 | try { 424 | boolean done = false; 425 | while (!done) { 426 | int tag = input.readTag(); 427 | switch (tag) { 428 | case 0: 429 | done = true; 430 | break; 431 | default: { 432 | if (!input.skipField(tag)) { 433 | done = true; 434 | } 435 | break; 436 | } 437 | case 8: { 438 | 439 | id_ = input.readInt32(); 440 | break; 441 | } 442 | case 18: { 443 | String s = input.readStringRequireUtf8(); 444 | 445 | name_ = s; 446 | break; 447 | } 448 | case 26: { 449 | String s = input.readStringRequireUtf8(); 450 | 451 | email_ = s; 452 | break; 453 | } 454 | } 455 | } 456 | } catch (com.google.protobuf.InvalidProtocolBufferException e) { 457 | throw new RuntimeException(e.setUnfinishedMessage(this)); 458 | } catch (java.io.IOException e) { 459 | throw new RuntimeException( 460 | new com.google.protobuf.InvalidProtocolBufferException( 461 | e.getMessage()).setUnfinishedMessage(this)); 462 | } finally { 463 | } 464 | } 465 | case GET_DEFAULT_INSTANCE: { 466 | return DEFAULT_INSTANCE; 467 | } 468 | case GET_PARSER: { 469 | if (PARSER == null) { synchronized (Person.class) { 470 | if (PARSER == null) { 471 | PARSER = new DefaultInstanceBasedParser(DEFAULT_INSTANCE); 472 | } 473 | } 474 | } 475 | return PARSER; 476 | } 477 | } 478 | throw new UnsupportedOperationException(); 479 | } 480 | 481 | 482 | // @@protoc_insertion_point(class_scope:Person) 483 | private static final Person DEFAULT_INSTANCE; 484 | static { 485 | DEFAULT_INSTANCE = new Person(); 486 | DEFAULT_INSTANCE.makeImmutable(); 487 | } 488 | 489 | public static Person getDefaultInstance() { 490 | return DEFAULT_INSTANCE; 491 | } 492 | 493 | private static volatile com.google.protobuf.Parser PARSER; 494 | 495 | public static com.google.protobuf.Parser parser() { 496 | return DEFAULT_INSTANCE.getParserForType(); 497 | } 498 | } 499 | 500 | 501 | static { 502 | } 503 | 504 | // @@protoc_insertion_point(outer_class_scope) 505 | } 506 | -------------------------------------------------------------------------------- /app/src/main/proto/User.proto: -------------------------------------------------------------------------------- 1 | package com.bqt.test; 2 | 3 | option java_package = "com.bqt.test.model"; 4 | option java_outer_classname = "UserProtos"; 5 | 6 | message User { 7 | required int32 id = 1; 8 | required string firstName = 2; 9 | required string lastName = 3; 10 | 11 | optional int32 level = 4 [default = 0]; 12 | 13 | message Monster { 14 | required int32 id = 1; 15 | required string name = 2; 16 | optional string type = 3 [default = "n/a"]; 17 | } 18 | 19 | repeated Monster monsters = 5; 20 | } -------------------------------------------------------------------------------- /app/src/main/proto/test.proto: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baiqiantao/ProtocolBufferTest/d9cb298697249eeffca5a7f9bf48af8a731c7550/app/src/main/proto/test.proto -------------------------------------------------------------------------------- /app/src/main/res/drawable/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baiqiantao/ProtocolBufferTest/d9cb298697249eeffca5a7f9bf48af8a731c7550/app/src/main/res/drawable/icon.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Test 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | 5 | repositories { 6 | google() 7 | jcenter() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.1.4' 11 | 12 | // NOTE: Do not place your application dependencies here; they belong in the individual module build.gradle files 13 | } 14 | } 15 | 16 | allprojects { 17 | repositories { 18 | google() 19 | jcenter() 20 | } 21 | } 22 | 23 | task clean(type: Delete) { 24 | delete rootProject.buildDir 25 | } 26 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/baiqiantao/ProtocolBufferTest/d9cb298697249eeffca5a7f9bf48af8a731c7550/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon May 07 21:41:50 CST 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip 7 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------