4

AndroidスタジオのSRCフォルダーに保存した.protoファイルから.javaファイルを生成しようとしています。以下のコードをgradleファイルに入れましたが、うまくいかないようです

apply plugin: 'com.squareup.wire'

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath 'com.squareup.wire:wire-maven-plugin:2.1.1'
  }
}
4

2 に答える 2

5

ここにワイヤ用の 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
于 2016-09-28T16:51:32.003 に答える
2

そのため、gradle プラグインを使用する代わりに、square wire コンパイラ jar を使用することになりました。手順は次のとおりです。

  1. http://search.maven.org/#artifactdetails%7Ccom.squareup.wire%7Cwire-compiler%7C2.1.1%7Cjarから compiler-jar-with-dependencies をダウンロードします
  2. jarファイルをAndroidアプリのルートディレクトリに置く
  3. ディレクトリに移動して、このコマンドを貼り付けます

    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-protofileandは、お持ちのものに置き換えてくださいname-of-file

于 2016-03-05T01:09:09.527 に答える