无法应用插件 'com.google.protobuf'

Failed to apply plugin 'com.google.protobuf'

我正在尝试解决插件构建脚本中的错误。我可以从 IntelliJ IDEA 中 运行 执行此操作,但是当我尝试从命令行构建时出现异常。

我已经能够将构建脚本减少到最低限度,如下所示:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.6'
    }
}

plugins {
    id 'java'
    id 'org.jetbrains.intellij' version '0.3.12'
    id "maven"
    id "de.undercouch.download" version "3.2.0"
    id "com.google.protobuf" version "0.8.6"
    id "idea"
}

当我尝试使用此脚本执行构建时,出现以下错误:

$ gradle buildPlugin

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/erikengheim/Development/Java/experiment/build.gradle' line: 17

* What went wrong:
An exception occurred applying plugin request [id: 'com.google.protobuf', version: '0.8.6']
> Failed to apply plugin 'com.google.protobuf'.
   > Could not create an instance of type com.google.protobuf.gradle.ProtobufSourceDirectorySet.
      > 'void org.gradle.api.internal.file.DefaultSourceDirectorySet.<init>(java.lang.String, java.lang.String, org.gradle.api.internal.file.FileResolver, org.gradle.api.internal.file.collections.DirectoryFileTreeFactory)'

如果我只注释掉以下行,我就可以让所有这些工作:

id "com.google.protobuf" version "0.8.6"

我对Gradle或Java不是很熟悉,所以我不知道如何解释异常的堆栈回溯。

您应该使用 protobuf-gradle-plugin 的最新版本 0.8.13。它至少需要 Gradle 5.6 和 Java 8.
更新您的构建脚本:

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.13'
  }
}

和这一行:

id "com.google.protobuf" version "0.8.13"

root build.gradle.kts

buildscript {
    repositories {
        mavenLocal()
        google()
        mavenCentral()
        maven(uri("https://jitpack.io"))
    }

    dependencies {
        //...
        classpath("com.google.protobuf:protobuf-gradle-plugin:0.8.18")
    }
}

应用程序

plugins {
    id("com.android.library")
    id("kotlin-android")
    kotlin("kapt")
    id("kotlin-parcelize")
    kotlin("plugin.serialization")
    id("com.google.protobuf") //remove version
}

删除版本

id "com.google.protobuf"

或 Kotlin DSL

id("com.google.protobuf")