Android - клиент gRPC - от Java до Котлина - PullRequest
0 голосов
/ 22 декабря 2018

Я прихожу сегодня, потому что не могу найти способ использовать gRPC и protobuf с моим приложением Kotlin для обмена с моим API.

На самом деле, у меня есть Go API и Android (Java) клиент.Мой обмен работает, все хорошо, но теперь я хотел бы попробовать с Kotlin.Однако я не могу найти способ создания моей заглушки клиента gRPC ...

Я следовал различным учебным пособиям / блогам:

Однако, в конце концов, ничего не работает ... Либо у меня есть ошибки, либо мне нужны обновления, или даже он генерирует файл, нозатем я получаю сообщение об ошибке, как только файл сгенерирован ...

Со второй ссылкой (которая работает больше других), я получаю эту ошибку:

enter image description here

Мой файл protobuf выглядит следующим образом (я выбрал самый простой)

// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

Я также перешел по двум другим ссылкам и одному репозиторию github, но безуспешноЯ надеюсь, что любой, кто уже достиг этого, может помочь мне:)

Спасибо за вашу помощь!

Редактировать 1 - Основываясь на обмене комментариями

Модуль

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "eyesapp.co.eyes"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    implementation 'io.grpc:grpc-protobuf:1.13.2'
    implementation 'io.grpc:grpc-stub:1.13.2'
}

buildscript {
    ext {
        protobufPluginVersion = '0.8.5'
        grpcVersion = '1.12.0'
        protocVersion = '3.2.0'
    }

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "com.google.protobuf:protobuf-gradle-plugin:$protobufPluginVersion"
    }

}

apply plugin: 'com.google.protobuf'
repositories {
    mavenCentral()
}

dependencies {

    implementation "io.grpc:grpc-netty:$grpcVersion"
    implementation "io.grpc:grpc-protobuf:$grpcVersion"
    implementation "io.grpc:grpc-stub:$grpcVersion"
}

protobuf {

    generatedFilesBaseDir = "$projectDir/src"

    protoc {
        artifact = "com.google.protobuf:protoc:$protocVersion"
    }

    plugins {
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:$grpcVersion"
        }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {
                outputSubDir = 'java'
            }
        }
    }
}

Проект

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.3.11'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
...