Android Studio和protobuf错误:(7,0)找不到id为“com.google.protobuf”的插件。

4
我想在Android Studio中使用gRPC,但在gradle设置中失败了。
我的gradle插件版本是2.1.2。我已经安装了gradle 2.14。
在app\build.gradle文件中:
    apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    apply plugin: "com.google.protobuf"

    defaultConfig {
        applicationId "com.example.test.test_20"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable true
            jniDebuggable true
        }
    }

    buildscript {
        repositories {
            maven {
                mavenCentral()
            }
        }
        dependencies {
            classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.0"
        }
    }


    protobuf {
        protoc {
            // The version of protoc must match protobuf-java. If you don't depend on
            // protobuf-java directly, you will be transitively depending on the
            // protobuf-java version that grpc depends on.
            artifact = "com.google.protobuf:protoc:3.0.0"
        }
        plugins {
            grpc {
                artifact = 'io.grpc:protoc-gen-grpc-java:1.0.0'
            }
        }
        generateProtoTasks {
            all()*.plugins {
                grpc {}
            }
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    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'
}

Gradle同步消息输出为

Error:(7, 0) 找不到id为'com.google.protobuf'的插件。

我的设置哪里出了问题?

我创建了一个新的空项目和app/build.gradle文件:

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

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

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.test.testprotobuf"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
}

而 gradle-wrapper.properties 文件:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-all.zip

我成功执行了"gradlew build"命令。并同步Gradle时,它提示“错误:(2, 0) 原因:org/gradle/api/internal/file/collections/DefaultDirectoryFileTreeFactory”。

2个回答

2

您的build.gradle文件中有些错误。您需要移动一些部分:

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

   buildscript {
        repositories {
            maven {
                mavenCentral()
            }
        }
        dependencies {
            classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.0"
        }
    }

   android {

   }

非常感谢您的回复。我创建了一个新项目,但它再次失败了... 它提示“错误:(2,0)原因:org/gradle/api/internal/file/collections/DefaultDirectoryFileTreeFactory”。 - Something

1

您需要在build.gradle中添加以下代码(项目的build.gradle,而不是应用程序的build.gradle):

buildscript {
repositories {
    jcenter()
    mavenCentral()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.2.3'
    // for gRPC
    classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.0" 
}}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接