Android 找不到参数的方法 protobuf()

问题描述

我正在为 protobuf 添加插件和依赖项,但在 gradle 同步时出现此错误

注意:Protobuf 类路径添加在项目级 build.gradle 文件

找不到参数的方法 protobuf() [build_8w4hvjkk8z81pgbpl03zqpq9b$_run_closure2@373b3e24] 在项目中 ':grpc'

模块级别 -> build.gradle

apply plugin: 'com.android.dynamic-feature'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.protobuf'

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    compileSdkVersion project.ext.compile_sdk_version

    defaultConfig {
        minSdkVersion project.ext.min_sdk_version
        targetSdkVersion project.ext.target_sdk_version
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            debuggable false
            minifyEnabled false
            useProguard false
            proguardFile 'proguard-rules.pro'
        }
    }

    configurations {
        implementation.exclude module:'protobuf-lite'
    }
}

protobuf {
    protoc { artifact = 'com.google.protobuf:protoc:3.12.0' }
    plugins {
        grpc { artifact = 'io.grpc:protoc-gen-grpc-java:1.36.0'
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java { option 'lite' }
            }
            task.plugins {
                grpc {
                    option 'lite' }
            }
        }
    }
}

dependencies {
    implementation project(":app")
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    androidTestImplementation 'androidx.annotation:annotation:1.1.0'

    implementation 'io.grpc:grpc-okhttp:1.36.0'
    implementation 'io.grpc:grpc-protobuf-lite:1.36.0'
    implementation 'io.grpc:grpc-stub:1.36.0'
}

解决方法

您只显示您的应用 build.gradle。在您的项目 build.gradle 中,您必须添加

依赖项{ .... 类路径“com.google.protobuf:protobuf-gradle-plugin:” }

参考:https://grpc.io/blog/kotlin-gradle-projects