Android studio和Protobuf集成

时间:2016-05-29 07:26:01

标签: android maven gradle protocol-buffers

我有一个项目需要使用protobuf(从服务器获取对象并解析它们) 为此,我配置了以下内容:

项目级别gradle.build

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.0'
        classpath 'com.google.protobuf:protobuf-java:2.6.1'
    }
}

allprojects {
    repositories {
        jcenter()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

应用级gradle.build

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.protobuf'

android {
    ...
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:2.6.1'
    }
}

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'
    compile 'com.google.protobuf:protobuf-java:2.6.1'
    compile 'com.google.protobuf:protobuf-gradle-plugin:0.7.0'
}

我将proto文件放在.../app/src/main/proto/FILE.proto下 一旦我尝试构建项目,我收到以下错误:

Error:Execution failed for task ':app:generateDebugProto'.
> protoc: stdout: . stderr: /Users/XX/YY/app/build/extracted-protos/main: warning: directory does not exist.
  /Users/XX/YY/app/build/extracted-include-protos/main: warning: directory does not exist.
  ...
  protoc-gen-javanano: program not found or is not executable
  --javanano_out: protoc-gen-javanano: Plugin failed with status code 1.

知道为什么会这样吗?它应该使用repo中的protoc并且应该支持nano编译。

感谢。

1 个答案:

答案 0 :(得分:0)

最后我从GitHub下载了源码并进行了编译。它解决了这个问题,因为它包含了缺少的二进制文件。