Protobuf + gRPC Android Studio接入指南

摘要:
0.8.6 '} 2. 添加“com.android”。应用程序“//”或“com.android”。模块下build.gradle中的library'applyplugin:3.0.1'api“javax.a”nnotation://github.com/google/protobuf-gradle-plugin2)https:
  一.添加protobuf-gradle-plugin插件

1.项目根目录build.gradle里添加:

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.6'
  }
}

2.Module下build.gradle里添加:

Android插件必须在Protobuf插件之前:

apply plugin: 'com.android.application'  // or 'com.android.library'
apply plugin: 'com.google.protobuf'

设置proto文件位置(src/main/proto):

android {
  sourceSets {
    main {
      proto {
        srcDir 'src/main/proto'
        ...
      }
    }
  }
}   

自定义Protobuf编译项:

android {
    protobuf {
        // Configure the protoc executable
        protoc {
            // Download from repositories
            artifact = 'com.google.protobuf:protoc:3.6.1' // 这里可以手工指定 path = '/usr/local/bin/protoc'
        }
        plugins {
            // Optional: an artifact spec for a protoc plugin, with "grpc" as
            // the identifier, which can be referred to in the "plugins"
            // container of the "generateProtoTasks" closure.
            javalite {
                artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
            }
        }
        generateProtoTasks {
            all()*.plugins {
                javalite {}

            }
        }
    }

}

加入依赖包:

dependencies {
  // You need to depend on the lite runtime library, not protobuf-java
  api 'com.google.protobuf:protobuf-lite:3.0.1'
api "javax.annotation:javax.annotation-api:1.2" //这个依赖是解决注释报错的问题
}

同步.rebuild 至此build/generated/source/proto/目录下生成相对应的XxxOuterClass文件

引用:

1)https://github.com/google/protobuf-gradle-plugin

2)https://search.maven.org/search?q=g:com.google.protobuf

3)https://blog.csdn.net/YongYu_IT/article/details/80430318

:

引用1)中有句话Android projects: no default output will be added. Since Protobuf 3.0.0, protobuf-lite is the recommended Protobuf library for Android, and you will need to add it as a codegen plugin.大概意思是Android的项目是没有默认的输出类型这个不同于GO,PHP,PYTHON的项目,从Protobuf 3.0.0后,在Android项目中推荐使用protobuf-lite为做为引用库.这一点尤为重要,不然在后面grpc的生成时很容易出错,该库对应的版本号通过引用2)进行查看

二.grpc代码生成

1.在protobuf选项增加相应代码

android {
    protobuf {
        // Configure the protoc executable
        protoc {
            // Download from repositories
            artifact = 'com.google.protobuf:protoc:3.6.1'
        }
        plugins {
            // Optional: an artifact spec for a protoc plugin, with "grpc" as
            // the identifier, which can be referred to in the "plugins"
            // container of the "generateProtoTasks" closure.
            javalite {
                artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
            }
            grpc {
                artifact = "io.grpc:protoc-gen-grpc-java:1.14.0"
            }
        }
        generateProtoTasks {
            all()*.plugins {
                javalite {}
                grpc {
                    option 'lite'
                }
            }
        }
    }

}

2.加入依赖包:

api 'io.grpc:grpc-okhttp:1.14.0'
api 'io.grpc:grpc-protobuf-lite:1.14.0'
api'io.grpc:grpc-stub:1.14.0'

引用:

1)https://github.com/grpc/grpc-java

免责声明:文章转载自《Protobuf + gRPC Android Studio接入指南》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇关于flume中涉及到时间戳的错误解决,Expected timestamp in the Flume even为什么说 LINQ 要胜过 SQL下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

三、文件的操作、函数、类和对象

一、文件 文件的打开与关闭 在python,使用open函数,可以打开一个已经存在的文件,或者创建一个新文件 open(文件名,访问模式) 示例如下: f = open('test.txt', 'w') 说明: 访问模式 说明 r 以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。 w 打开一个文件只用于写入。如果该文件已存...

Excel.Application手册

----转载:http://blog.csdn.net/xxfigo/article/details/6618129 定制模块行为(1) Option Explicit '强制对模块内所有变量进行声明    Option Private Module '标记模块为私有,仅对同一工程中其它模块有用,在宏对话框中不显示    Option Compare T...

关于箭头函数

一、为什么存在 const Person = { 'name': 'little bear', 'age': 18, 'sayHello': function () { setInterval(function () { console.log('我叫' + this.name + '我今年' + thi...

Jenkins 部署 .NET MVC 项目

一、什么是Jenkins   Jenkins是一款开源 CI&CD 软件,用于自动化各种任务,包括构建、测试和部署软件。Jenkins 支持各种运行方式,可通过系统包、Docker 或者通过一个独立的 Java 程序。 二、如何安装Jenkins   1、安装JDK:https://www.oracle.com/java/technologies/...

Proxy详解

一.Proxy基础 1. 什么是Proxy? Proxy是一个构造函数,可以通过它生成一个Proxy实例。 const proxy = new Proxy(target, handler); // target: 目标对象,待操作对象(可以是普通对象,数组,函数,proxy实例对象) // handler: 一个对象,最多可包含13个操作方法,也可以是空对...

Android Studio安装及其使用

为了不与Java开发混用,所以不使用IDEA开发安卓应用而选择Android Studio。 官网下载安装 Android Studio(https://developer.android.google.cn/studio/) 启动时会提示 Unable to access Android SDK add-on list,点 cancel,然后就会弹出An...