Android Studio 配置并使用Protocol Buffer生成java文件

一、环境配置

(一)Project的build.gradle
  • 添加Protobuf插件版本信息和maven仓库
mavenCentral()
 classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.13'

具体位置如下:

buildscript {
    repositories {
        google()
        mavenCentral()

        //choice 1:
        //jcenter()

        //choice 2:
        //change mirror
        maven{ url'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven{ url'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.13'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
(二)AppModule的build.gradle
1. 声明依赖的protobuf插件
  • plugins闭包或apply写法二选一
//apply plugin:'com.android.application'
//apply plugin: 'com.google.protobuf'

plugins {
    id 'com.android.application'
    id 'com.google.protobuf'//gradle脚本头,声明依赖的插件
}

2. 指定protobuf所在路径(在android闭包中)
  • 不指定时,默认与java同级的目录,否则不能被识别编译
  • 如下,指定了在java指定目录下为源目录
android{
	......//省略其他配置项
    sourceSets {
        main {
            java {
                srcDirs 'src/main/java'
            }
            proto {
                srcDirs 'src/main/java/com/demo/protocolbuffer'
            }
        }
    }
}
3. 添加protobuf依赖
dependencies {
	//....
    implementation 'com.google.protobuf:protobuf-java:4.0.0-rc-2'
    implementation 'com.google.protobuf:protoc:4.0.0-rc-2'
	//....
}
4. 设置protobuf编译任务
//编写编译任务,调用plugin编译生成java文件
protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.6.1'
    }

    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.builtins {
                java {}
            }
        }
    }
}

二、定义proto文件

  • 参考[Google]Protocol Buffer Basics: Java:https://developers.google.cn/protocol-buffers/docs/javatutorial
syntax = "proto2";

package com.demo.protocolbuffer;

option java_package = "com.demo.protocolbuffer";
option java_outer_classname = "AddressBookProtos";

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

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    optional string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phones = 4;
}

message AddressBook {
  repeated Person people = 1;
}

三、结构目录与效果

Android Studio 配置并使用Protocol Buffer生成java文件_第1张图片

一些问题(插件版本)

  • 目前采用4.0.0-rc-2版本(maven最新的),如果把procobuf中protoc也换成这个版本会出错,修改成3.6.1不会出错
  • 全部使用3.6.1也不会错
    在这里插入图片描述

Execution failed for task ‘:protocolbufferdemo:generateDebugProto’.
java.io.IOException: Cannot run program “C:\Users\xxxx.gradle\caches\modules-2\files-2.1\com.google.protobuf\protoc\4.0.0-rc-2\474b3604dbd7dff7aa733a745056dfa2dc946eeb\protoc-4.0.0-rc-2-windows-x86_64.exe”:
CreateProcess error=1392, The file or directory is corrupted and
unreadable

你可能感兴趣的:(Android,gradle,android,studio,protobuf,protocol,buffer,java)