Protobuf + gRPC Android Studio接入指南


  一.添加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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM