简体   繁体   English

gRPC protobuf无法使用kotlin构建进行编译(Android Studio 3.0)

[英]gRPC protobuf not compiling with kotlin builds (Android Studio 3.0)

I created a gRPC project very similar to the example gRPC for android project at https://github.com/grpc/grpc-java/tree/master/examples/android/helloworld 我在https://github.com/grpc/grpc-java/tree/master/examples/android/helloworld创建了一个与Android项目示例gRPC非常相似的gRPC项目

The first time I somehow got my own project with my own simple .proto file compiled without any errors. 第一次我以某种方式将自己的简单.proto文件编译成自己的项目而没有任何错误。 I just copy-and-pasted all important build.gradle parts. 我只是复制并粘贴了所有重要的build.gradle部分。 The following builds in which I changed my MainActivity Kotlin code worked as well. 我在其中更改了MainActivity Kotlin代码的以下内部版本也能正常工作。

But now, that I have added a bit more to my .proto file, Gradle fails to build my project. 但是现在,我在.proto文件中添加了更多内容,因此Gradle无法构建我的项目。 The .proto file is correct and I already tried adding it as a dependency to compileDebugKotlin{} and compileReleaseKotlin{} but nothing worked. .proto文件是正确的,我已经尝试将其添加为compileDebugKotlin {}和compileReleaseKotlin {}的依赖项,但没有任何效果。 I get the following error message that the task :generateDebugProto is not defined. 我收到以下错误消息,未定义任务:generateDebugProto

But I am confused because when I add a line break to my build.gradle and click Sync now in Android Studio the Gradle sync finishes successfully and in the gradle log I see the following lines: 但我很困惑,因为当我换行添加到我的build.gradle并点击Sync now在Android Studio中的摇篮同步成功完成,并在日志的gradle我看到下面几行:

:app:extractDebugProto
:app:extractIncludeDebugProto
:app:extractProto
:app:generateDebugProto

Am I doing anything wrong? 我做错什么了吗?


My project build.gradle 我的项目build.gradle

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

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

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

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

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

My module-level build.gradle 我的模块级build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'com.google.protobuf'

android {
    compileSdkVersion 26
    ext.buildType =  ""
    defaultConfig {
        applicationId "com.my.appr"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            buildType = "Release"
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            buildType = "Debug"
        }
    }
    sourceSets {
        debug.java.srcDirs += 'build/generated/source/proto/debug/java'
        debug.java.srcDirs += 'build/generated/source/proto/debug/grpc'
        debug.java.srcDirs += 'build/generated/source/proto/debug/javalite'
        release.java.srcDirs += 'build/generated/source/proto/release/java'
        release.java.srcDirs += 'build/generated/source/proto/release/grpc'
        release.java.srcDirs += 'build/generated/source/proto/release/javalite'
        main.proto.srcDirs += 'src/main/proto'
    }
    //preBuild.dependsOn(":generateDebugProto")
}


protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.5.0'
    }
    plugins {
        javalite {
            artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
        }
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.8.0' // CURRENT_GRPC_VERSION
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.plugins {
                javalite {}
                grpc {
                    // Options added to --grpc_out
                    option 'lite'
                }
            }
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'io.grpc:grpc-okhttp:1.8.0' // CURRENT_GRPC_VERSION
    implementation 'io.grpc:grpc-protobuf-lite:1.8.0' // CURRENT_GRPC_VERSION
    implementation 'io.grpc:grpc-stub:1.8.0' // CURRENT_GRPC_VERSION
    implementation 'javax.annotation:javax.annotation-api:1.2'
}


//preBuild.dependsOn ":generate"+buildType+"proto"

After 4 hours I could solve the problem! 4小时后,我可以解决问题!

For some weird reason I had to add this codeblock: 出于某些奇怪的原因,我不得不添加以下代码块:

task.builtins {
    remove java
}

to my module-level build.gradle 到我的模块级build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'com.google.protobuf'

android {
    compileSdkVersion 26
    ext.buildType =  ""
    defaultConfig {
        applicationId "com.my.appr"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            buildType = "Release"
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            buildType = "Debug"
        }
    }
    sourceSets {
        debug.java.srcDirs += 'build/generated/source/proto/debug/java'
        debug.java.srcDirs += 'build/generated/source/proto/debug/grpc'
        debug.java.srcDirs += 'build/generated/source/proto/debug/javalite'
        release.java.srcDirs += 'build/generated/source/proto/release/java'
        release.java.srcDirs += 'build/generated/source/proto/release/grpc'
        release.java.srcDirs += 'build/generated/source/proto/release/javalite'
        main.proto.srcDirs += 'src/main/proto'
    }
    //preBuild.dependsOn(":generateDebugProto")
}


protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.5.0'
    }
    plugins {
        javalite {
            artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
        }
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.8.0' // CURRENT_GRPC_VERSION
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {    ///////////////////////////////7
                remove java    // THIS IS THE SOLUTION      //
            }                  ///////////////////////////////
            task.plugins {
                javalite {}
                grpc {
                    // Options added to --grpc_out
                    option 'lite'
                }
            }
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'io.grpc:grpc-okhttp:1.8.0' // CURRENT_GRPC_VERSION
    implementation 'io.grpc:grpc-protobuf-lite:1.8.0' // CURRENT_GRPC_VERSION
    implementation 'io.grpc:grpc-stub:1.8.0' // CURRENT_GRPC_VERSION
    implementation 'javax.annotation:javax.annotation-api:1.2'
}


//preBuild.dependsOn ":generate"+buildType+"proto"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM