1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

コマンドラインでWireのProtocol Buffersをコンパイルする

Posted at

はじめに

コマンドラインでWireのProtocol Buffersをコンパイルする手順をまとめました。

環境

  • MacBook Pro (13-inch, 2020)
  • macOS Catalina 10.15.7

手順

1. protoファイルを用意

syntax = "proto3";
package com.test;

message Person {
  string name = 1;
  int32 id = 2;
  string email = 3;
}

2. Javaが使えることを確認する

$ java -version
openjdk version "1.8.0_242-release"
OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
OpenJDK 64-Bit Server VM (build 25.242-b3-6915495, mixed mode)

3. Wireコンパイラを用意

jar-with-dependencies.jar を選択してダウンロードします。
https://search.maven.org/search?q=g:com.squareup.wire%20AND%20a:wire-compiler
スクリーンショット 2021-01-19 16.44.41.png

4. protoファイルをコンパイルする

$ java -jar wire-compiler-3.5.0-jar-with-dependencies.jar \
     --proto_path=src/main/proto \
     --java_out=src/main/java
Writing com.test.Person to src/main/java

AndroidでIntentのパラメータにProtocol Buffersを使う場合は、--android をつけて上げると Parcelable を自動でつけてくれます。

5. できあがり

java_outに指定したディレクトリにコードが生成されます。

// Code generated by Wire protocol buffer compiler, do not edit.
// Source file: test.proto at 4:1
package com.test;

import com.squareup.wire.FieldEncoding;
import com.squareup.wire.Message;
import com.squareup.wire.ProtoAdapter;
import com.squareup.wire.ProtoReader;
import com.squareup.wire.ProtoWriter;
import com.squareup.wire.WireField;
import com.squareup.wire.internal.Internal;
import java.io.IOException;
import java.lang.Integer;
import java.lang.Object;
import java.lang.Override;
import java.lang.String;
import java.lang.StringBuilder;
import okio.ByteString;

public final class Person extends Message<Person, Person.Builder> {
  public static final ProtoAdapter<Person> ADAPTER = new ProtoAdapter_Person();
  // 以下省略
}

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?