Sunday, September 30, 2018

Register a package for the class generated from the proto file in the gradle by default , not in proto file

Leave a Comment

In my android app I use proto files. For example, I have proto file Stats.proto

syntax = "proto3"; package com.me.test;  message Stat {     string a = 1;     string b = 2;     string c = 3;     string d = 4; } 

I need to register package in the each proto file itself, and this is uncomfortable, because I have a lot of files. I want to register default package in gradle, for example, 'package com.me.test', which uses each file that I create. I found solution in javanano

nano {     proto {       // Selects between --java_out and --javanano_out, default is 'java'       mode 'javanano'       // Options added to --java_out or --javanano_out       option 'java_package=src/proto/simple-data.proto|my_package'       option 'java_outer_classname=src/proto/simple-data.proto|OuterName'       // Apply the 'grpc' plugin defined in protobufNativeCodeGenPluginDeps       plugin 'grpc' {         // Options added to --grpc_out         option 'nano=true'       }     } 

But I need to use javalite

protobuf {     protoc {         artifact = 'com.google.protobuf:protoc:3.0.0'     }     plugins {         javalite {             artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'         }     }     generateProtoTasks {         all().each { task ->             task.builtins {                 remove java             }             task.plugins {                 javalite { }             }         }     } } 

And I want to have analogous logic

option 'java_package=src/proto/simple-data.proto|my_package' 

like javanano in javalite

Can I implement that?

1 Answers

Answers 1

According to the doc, and assuming javalite supports the same features than java (it's a long shot as I did not found any trace of documentation on this plugin), just try that :

protobuf {     protoc {         artifact = 'com.google.protobuf:protoc:3.0.0'     }     plugins {         javalite {             artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'         }     }     generateProtoTasks {         all().each { task ->             task.builtins {                 remove java             }             task.plugins {                 javalite {                     option 'java_package=.......'                                  }             }         }     } } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment