我正在尝试从Android工作室中存储在我的SRC文件夹下的.proto文件生成.java文件。我将下面的代码放在我的gradle文件中,它似乎不起作用
apply plugin: 'com.squareup.wire'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.squareup.wire:wire-maven-plugin:2.1.1'
}
}
答案 0 :(得分:4)
这里有一个gradle插件:https://github.com/square/wire-gradle-plugin。但是,黄金时段好像是not quite ready。我在使用它时遇到了一些麻烦。
但是,这是一种方法,可以直接使用线编译器和简单的gradle任务自动生成* .proto文件中的java代码。我在下面提供了一个片段,其中包含对build.gradle的修改。根据源布局更改protoPath和wireGeneratedPath。
def protoPath = 'src/proto'
def wireGeneratedPath = 'build/generated/source/wire'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.squareup.wire:wire-compiler:2.2.0'
}
}
android {
sourceSets {
main {
java {
include wireGeneratedPath
}
}
}
}
dependencies {
compile 'com.squareup.wire:wire-runtime:2.2.0'
// Leave this out if you're not doing integration testing...
androidTestCompile 'com.squareup.wire:wire-runtime:2.2.0'
}
// This handles the protocol buffer generation with wire
task generateWireClasses {
description = 'Generate Java classes from protocol buffer (.proto) schema files for use with squareup\'s wire library'
delete(wireGeneratedPath)
fileTree(dir: protoPath, include: '**/*.proto').each { File file ->
doLast {
javaexec {
main = 'com.squareup.wire.WireCompiler'
classpath = buildscript.configurations.classpath
args = ["--proto_path=${protoPath}", "--java_out=${wireGeneratedPath}", "${file}"]
}
}
}
}
preBuild.dependsOn generateWireClasses
答案 1 :(得分:2)
因此,我没有使用gradle插件,而是最终使用了方形线编译器jar。以下是步骤。
转到目录并粘贴此命令
java -jar wire-compiler-2.1.1-jar-with-dependencies.jar --proto_path=directory-of-protofile --java_out=app/src/main/java/ name-of-file.proto
应该有效。请确保将directory-of-protofile
和name-of-file
替换为您拥有的任何内容。