Android Studio中Grpc的配置

Android Studio中Grpc的配置

Grpc:是一個高性能、開源和通用的 RPC 框架,通過官方提供各種語言的實現包,我們可以快速接入到項目中。Android中接入的是grpc-java
爲了方便接入,在Android Studio內配置如下,即可自動編譯proto文件生成相關的代碼,然後直接調用即可。

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

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"
    defaultConfig {
        applicationId "com.yuchenfw.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'
        }
    }
    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
    }
    protobuf {
        protoc {
            artifact = 'com.google.protobuf:protoc:3.0.0'
        }
        plugins {
            javalite {
                artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" //javalite版本
            }
            grpc {
                artifact = 'io.grpc:protoc-gen-grpc-java:1.0.0' //grpc-java版本
            }
        }
        generateProtoTasks {
            all().each { task ->
                task.plugins {
                    javalite {}
                    grpc {
                        // Options added to --grpc_out
                        option 'lite' //生成類型
                    }
                }
            }
        }
    }
}

buildscript {
    repositories {
        mavenCentral()
//        maven {
////            mavenCentral()
//            url 'https://repo1.maven.org/maven2'
//        }
    }
    dependencies {
        classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.1"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.+'
    testCompile 'junit:junit:4.12'
    //grpc need
    compile 'io.grpc:grpc-okhttp:1.0.0'
    compile 'io.grpc:grpc-protobuf-lite:1.0.0'
    compile 'io.grpc:grpc-stub:1.0.0'

    compile 'com.google.code.findbugs:jsr305:3.0.0'
    compile 'com.google.guava:guava:20.0'
    compile 'javax.annotation:javax.annotation-api:1.2'
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章