Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 966
Alherd Avatar asked Sep 21 '18 17:09

Alherd


People also ask

How do I create a Java proto file?

The protocol buffer compiler produces Java output when invoked with the --java_out= command-line flag. The parameter to the --java_out= option is the directory where you want the compiler to write your Java output. For each . proto file input, the compiler creates a wrapper .

What are proto files in gRPC?

gRPC uses the Protobuf . proto file format to define your messages, services and some aspects of the code generation.

What is Protobuf file?

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler.

What is .proto file in Java?

proto file are simple: you add a message for each data structure you want to serialize, then specify a name and a type for each field in the message. Here is the . proto file that defines your messages, addressbook. proto .


Video Answer


1 Answers

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=.......'                 
                }
            }
        }
    }
}

edit : try that in order to inspect the object :

task.plugins {
    javalite {
        it.properties.each { println "$it.key -> $it.value" }
    }
}
like image 104
ToYonos Avatar answered Sep 18 '22 13:09

ToYonos