windows 10 AndroidStudio 3.0.1 protobuf-javalite 版本java
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
}
複製代碼
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' } 複製代碼
由於在gradle中設置了proto文件的可編譯目錄,因此須要在這個目錄中編寫.proto文件 (參考網上教程寫了proto文件,但具體網址不記得了,很差意思,若是須要我會備註)android
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的語法都是參考網上教程,如有失誤,謝謝指正。 寫完後,點擊 git
便可自動生成java文件。代碼:github
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;
}
複製代碼
打印出windows
data {
datatype: 1
itemdata {
packageid: "222222"
sname: "eiiii"
tabs {
f: "sss"
type: 2
}
}
}
dirtag: "soft"
has_next_page: true
複製代碼
代碼:bash
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();
}
}
複製代碼
打印出:app
datatype: 1
itemdata {
packageid: "222222"
sname: "eiiii"
tabs {
f: "sss"
type: 2
}
}
複製代碼
這樣簡單的應用就完成了。gradle
項目中遇到問題與解決:網站
enum Test{
option allow_alias = true;
test_value=2;
duplicate_test_value=2;
}
複製代碼
本來在enum中不能定義相同的值,但加入option allow_alias = true;
就能夠了ui
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 <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
複製代碼
developers.google.com/protocol-bu… github.com/google/prot… github.com/protocolbuf…