这里有一个用于电线的 gradle 插件:https ://github.com/square/wire-gradle-plugin 。但是,它似乎还没有为黄金时段做好准备。我在让它工作时遇到了一些麻烦。
但是,这里有一种方法可以直接使用线编译器和一个简单的 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