Android——protobuf的简单使用

1. Kotlin

引入 protobuf

  • build.gradle(project):
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        gradlePluginPortal()
    }
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.19'
    }
}

plugins {
    id 'com.android.application' version '7.2.1' apply false
    id 'com.android.library' version '7.2.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
    id 'com.google.protobuf' version '0.8.18' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
  • build.gradle(app):
plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'com.google.protobuf'
}

android {
    compileSdk 32

    sourceSets {
        main {
            java {
                srcDirs += 'build/generated/source/proto/main/java'
            }
            kotlin {
                srcDirs += 'build/generated/source/proto/main/kotlin'
            }
            proto {
                srcDir 'src/main/java/com/example/gatttestapp/protobuf'
            }
        }
    }

    defaultConfig {
        applicationId "com.example.gatttestapp"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.8.0'
    implementation 'androidx.appcompat:appcompat:1.5.0'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'com.google.protobuf:protobuf-javalite:3.20.1'
    implementation("androidx.work:work-runtime:2.7.1")
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.8.0'
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java {
                    option "lite"
                }
            }
        }
    }
}

简单使用

  • 定义 personProto.proto文件:
//指定proto的版本为proto3,不写的话默认为proto2.
syntax = "proto3";
//包名
package proto;
//引入包
//import "";
//指定生成类所在的Java包名
option java_package = "com.example.demo";
//重命名,如果不写,默认为文件名的首字母大写转化生成,如本文件如果不写则是Person
option java_outer_classname = "PersonProto";

message Person {
  string name = 1;
  string id= 2;
  //repeated 相当于java 里的 List<Card>
  repeated Card cList = 6 ;
}
  • 基本调用
var person = personProto.Person .newBuilder().setName("123").setId("456").build()
val person2: personProto.Person = personProto.Person.parseFrom(person.toByteArray())
Log.e(TAG, "id : ${person2.id} , name: ${person2.name}")
var dataTemp = personProto.Person.parseFrom(person .toByteArray())
Log.e(TAG, "dataTemp : ${dataTemp.toString()}")

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yawn__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值