這裏有一個電線的gradle插件:https://github.com/square/wire-gradle-plugin。但是,它似乎是黃金時段的not quite ready。讓它工作起來有些麻煩。
但是,這裏有一種方法可以使用線編譯器直接從* .proto文件自動生成java代碼,並且可以執行簡單的gradle任務。我在下面提供了一個代碼片段,其中對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
哪個目錄?你能簡單解釋一下嗎? – abh22ishek