protobuf gradle插件无法编译

时间:2017-11-16 14:48:45

标签: android-gradle protoc protobuf-java

我尝试使用gradle插件编译protobuf文件,但是我收到以下错误:

java.io.IOException: Can't write [/Users/elavi/dev/sdk3/android/showcaseapp/build/intermediates/multi-dex/debug/componentClasses.jar] 
(Can't read [/Users/elavi/.gradle/caches/modules-2/files-2.1/com.google.protobuf/protobuf-java/3.0.0/6d325aa7c921661d84577c0a93d82da4df9fa4c8/protobuf-java-3.0.0.jar(;;;;;;**.class)] 
(Duplicate zip entry [protobuf-java-3.0.0.jar:com/google/protobuf/ExperimentalApi.class]))

不确定为什么会发生这种情况...... 正如预期的那样正确生成了protobuf文件,但最后一步因为这个奇怪的错误而失败。

这是我的gradle文件:

apply plugin: 'com.android.library'
apply plugin: 'com.google.protobuf'
apply plugin: 'idea'

group = GROUP
version = VERSION_NAME

apply from: 'versioning.gradle'

buildscript {
  repositories {
    mavenCentral()
  }
}

android {
  compileSdkVersion 26
  buildToolsVersion '26.0.2'
  flavorDimensions "default"

  defaultConfig {
    minSdkVersion 15
    targetSdkVersion 26
    versionCode buildVersionCode()
    versionName VERSION_NAME
    consumerProguardFiles 'tangram-proguard-rules.txt'
  }

  // Add proto files location to be used with the protobuf plugin
  sourceSets {
    main {
      proto {
        srcDir '../../common/vendored/proto'
      }
    }
  }
}

dependencies {
  compile 'com.google.protobuf:protobuf-lite:3.0.0'
  compile 'io.grpc:grpc-stub:1.6.1'
  compile 'io.grpc:grpc-protobuf:1.0.0-pre2'
  compile 'javax.annotation:javax.annotation-api:1.2'

  compile 'com.squareup.okhttp3:okhttp:3.9.0'
  compile 'com.android.support:support-annotations:27.0.0'

  implementation project(':core')
}

// Protobuf configuration. Taken from the documentation: https://github.com/google/protobuf-gradle-plugin
protobuf {

  protoc {
    artifact = 'com.google.protobuf:protoc:3.0.0'   }   plugins {
    javalite {
      artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
    }
    grpc {
      artifact = 'io.grpc:protoc-gen-grpc-java:1.0.0-pre2'
    }   }   generateProtoTasks {
    all().each { task ->
      task.builtins {
        remove java
      }
      task.plugins {
        javalite { }
        grpc {
          option 'lite'
        }
      }
    }   }   generatedFilesBaseDir = "$projectDir/build/gen" }

clean {   delete protobuf.generatedFilesBaseDir }

idea {   module {
    sourceDirs += file("${protobuf.generatedFilesBaseDir}/main/java");
    sourceDirs += file("${protobuf.generatedFilesBaseDir}/main/grpc");   } }

//apply from: file('gradle-mvn-push.gradle')

我只是添加了原始文件自述文件中的内容(https://github.com/google/protobuf-gradle-plugin),没有做任何花哨的东西......

1 个答案:

答案 0 :(得分:3)

也许你应该删除依赖关系部分的compile 'com.google.protobuf:protobuf-lite:3.0.0'条目,你也有重复的条目,并且缺少最后版本的一些配置。对于另一方可能原始源的路径有问题,我的原型是src/main/proto但我只是单独声明proto。我的简要配置是下一个:

app build.gradle

apply plugin: 'com.google.protobuf'

android {
    ...
    sourceSets {
        main {
          proto {
          }
        }
    }
    ...
    configurations.all {
      resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'io.grpc:grpc-okhttp:1.10.0'
    implementation 'io.grpc:grpc-protobuf-lite:1.10.0'
    implementation 'io.grpc:grpc-stub:1.10.0'
    implementation 'javax.annotation:javax.annotation-api:1.2'
    // full protobuf (optional)
    // protobuf 'com.google.protobuf:protobuf-java:3.4.0'
    ...

}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.0.2"
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.1.2'
        }
        javalite {
            artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.plugins {
                javalite {}
                grpc {
                    // Options added to --grpc_out
                    option 'lite'
                }
            }
        }
    }
    generatedFilesBaseDir = "$projectDir/build/generated"
}

主要项目build.gradle

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.2"
        ...
    }
}