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

标签 android android-studio gradle protocol-buffers

我想在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 同步消息输出是 错误:(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,提示“Error:(2, 0) Cause: org/gradle/api/internal/file/collections/DefaultDirectoryFileTreeFactory ”。

最佳答案

你的 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 {

   }

关于Android Studio 和 protobuf 错误 :(7, 0) 找不到 id 为 'com.google.protobuf' 的插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39284797/

相关文章:

android - 无法停止媒体播放器

java - Android 应用因侵犯用户隐私而从 Play 商店下架

android - 无法安装 Google Play 服务包,修订版。 11

java - Eclipse Spring Boot 构建路径包含重复条目

android - 无法检测文件 : java. lang.NoClassDefFoundError android/content/ComponentCallbacks

android - 在 Android 设备上查找 IPv6 全局范围地址

android - 自定义 View 日历中的 Google 日历等事件

android - Unity,在 120hz 设备上构建 android 的问题

java - 运行后应用程序在模拟器中关闭

android - 错误:任务 ':Click:dexDebug'的执行失败