使用AndroidStudio处理Google Protobuf文件转Java文件

本文介绍了如何在Android Studio中使用protobuf v3和v2版本将Protobuf文件转换为Java文件。项目采用Protobuf作为数据传输协议,并详细说明了配置环境、protobuf文件、以及在build.gradle文件中的相关设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;            

}

项目根目录配置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'

    }

modle根目录配置build.gradle

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

            proto {
             //main目录新建proto目录
                srcDir 'src/main/proto'
                include '**/*.proto'
            }
            java {
                srcDir 'src/main/java'
            }
        }
    }
//依赖库
compile 'com.google.protobuf:protobuf-java:2.6.1'
compile 'com.google.protobuf:protoc:2.5.0'
//构建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"
}

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

3.0 头部开始加上
syntax = "proto3";
2.0 头部加上
syntax = "proto2";

下面是完整的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"
}

在 Android Studio 中编译 protobuf 文件的步骤如下: 1. 首先,在项目的 build.gradle 文件中添加以下依赖: ``` implementation 'com.google.protobuf:protobuf-java:x.x.x' ``` 其中,x.x.x 是版本号,可以根据自己的实际情况进行替换。 2. 在 app 目录下创建一个 proto 目录,用于存放 .proto 文件。 3. 在 build.gradle 文件中添加以下内容: ``` protobuf { protoc { artifact = 'com.google.protobuf:protoc:x.x.x' } plugins { javalite { artifact = 'com.google.protobuf:protoc-gen-javalite:x.x.x' } } generateProtoTasks { all().each { task -> task.builtins { java { option 'lite' } } task.plugins { javalite {} } } } } ``` 其中,x.x.x 是版本号,需要替换为实际的版本号。 4. 在 app 目录下的 build.gradle 文件中,添加以下内容: ``` sourceSets { main { java { srcDirs 'src/main/java', 'build/generated/source/proto/main/java' } } } protobuf { protoc { artifact = 'com.google.protobuf:protoc:x.x.x' } generateProtoTasks { all().each { task -> task.builtins { java { option 'lite' } } } generateProtoTasks { all().each { task -> task.plugins { javalite {} } } } } } ``` 其中,x.x.x 是版本号,需要替换为实际的版本号。 5. 在 Android Studio 中,点击菜单栏中的 Build -> Make Project,编译项目。编译成功后,会在 app/build/generated/source/proto/main/java 目录下生成对应的 .java 文件。 6. 最后,在需要使用 proto 文件的代码中,使用生成的 .java 文件即可。 以上就是在 Android Studio 中编译 protobuf 文件的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值