首页 > 系统 > Android > 正文

利用Android Studio将Protobuf文件生成Java文件

2019-11-09 18:05:13
字体:
来源:转载
供稿:网友

配置Gradle

1、项目的build.gradle文件加入:

classpath 'com.google.PRotobuf:protobuf-gradle-plugin:0.8.0'

项目级

2、模块的build.gradle

顶部添加protobuf插件apply plugin: 'com.google.protobuf'android结点增加proto文件位置配置 sourceSets { main { proto { srcDir 'src/main/proto' include '**/*.proto' } java { srcDir 'src/main/java' } } }添加依赖 compile 'com.google.protobuf:protobuf-java:3.1.0' compile 'com.google.protobuf:protoc:3.1.0'增加protobuf结点protobuf { protoc { artifact = 'com.google.protobuf:protoc:3.1.0' } generateProtoTasks { all().each { task -> task.builtins { remove java } task.builtins { java {} // Add cpp output without any option. // DO NOT omit the braces if you want this builtin to be added. cpp {} } } } generatedFilesBaseDir = "$projectDir/src/generated"}

目录结构

目录结构

完整的模块的build.gradle

apply plugin: 'com.android.application'apply plugin: 'com.google.protobuf'android { compileSdkVersion 25 buildToolsVersion "25.0.0" defaultConfig { applicationId "com.ydtf.nbmobile.protobufdemo4" minSdkVersion 16 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } sourceSets { main { proto { srcDir 'src/main/proto' include '**/*.proto' } java { srcDir 'src/main/java' } } }}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.0.1' compile 'com.google.protobuf:protobuf-java:3.1.0' compile 'com.google.protobuf:protoc:3.1.0' testCompile 'junit:junit:4.12'}protobuf { protoc { artifact = 'com.google.protobuf:protoc:3.1.0' } generateProtoTasks { all().each { task -> task.builtins { remove java } task.builtins { java {} // Add cpp output without any option. // DO NOT omit the braces if you want this builtin to be added. cpp {} } } } generatedFilesBaseDir = "$projectDir/src/generated"}

参考文章 https://github.com/google/protobuf-gradle-plugin


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表