当前位置:   article > 正文

使用AndroidStudio处理Google Protobuf文件转Java文件_com.google.protobuf:protobuf-java

com.google.protobuf:protobuf-java

Google Protobuf文件转Java文件

Protobuf协议目前分两个版本 protobuf v3protobuf v2两个版本
项目中使用Protobuf作为数据传输,使用AndroidStudio工具批量编译protobuf文件比较简单。

这里写图片描述

开始配置环境
protobuf文件

option java_package = "com.daycodeday.test";
option java_outer_classname = "MtmColWrapper";

message MtmCol {
optional    uint32      colnamelength           = 1;
required    string      colname             = 2;

optional    uint32      coltype             = 3;

optional    uint32      coltypestringlength     = 4;
optional    string      coltypestring           = 5;

optional    uint32      length              = 6;

optional    uint32      precision           = 7;
optional    uint32      scale               = 8;
optional    uint32      nullable            = 9;            

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

项目根目录配置build.gradle

    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        // protobuf支持版本,AS3.0必须用0.8.2以上
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.2'

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

modle根目录配置build.gradle

//依赖支持
apply plugin: 'com.google.protobuf'
  • 1
  • 2
//指定文件目录
  sourceSets {
        main {

            proto {
             //main目录新建proto目录
                srcDir 'src/main/proto'
                include '**/*.proto'
            }
            java {
                srcDir 'src/main/java'
            }
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
//依赖库
compile 'com.google.protobuf:protobuf-java:2.6.1'
compile 'com.google.protobuf:protoc:2.5.0'
  • 1
  • 2
  • 3
//构建task
protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:2.5.0'
    }

    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.builtins {
                java {}
                // Add cpp output without any option.
                // DO NOT omit the braces if you want this builtin to be added.
                cpp {}
            }
        }
    }
    //生成目录
    generatedFilesBaseDir = "$projectDir/src/generated"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

我使用依赖库是 2.5 ,如果是3.0以上依赖库的需要定义下协议头

3.0 头部开始加上
syntax = "proto3";
2.0 头部加上
syntax = "proto2";
  • 1
  • 2
  • 3
  • 4

下面是完整的gradle文件

apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.daycodeday.myapplication"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {

            proto {
                srcDir 'src/main/proto'
                include '**/*.proto'
            }
            java {
                srcDir 'src/main/java'
            }
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.google.protobuf:protobuf-java:2.6.1'
    compile 'com.google.protobuf:protoc:2.5.0'
}
protobuf {
    protoc {
         artifact = 'com.google.protobuf:protoc:2.5.0'
    }

    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.builtins {
                java {}
                // Add cpp output without any option.
                // DO NOT omit the braces if you want this builtin to be added.
                cpp {}
            }
        }
    }
    //生成目录
    generatedFilesBaseDir = "$projectDir/src/generated"
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号