Configuration
gRPC integration is available in an experimental state. The artifacts are published separately in our Space repository.
Dependencies configuration
Below is an example of a project setup.
settings.gradle.kts
:
build.gradle.kts
:
Here <version>
comes from the badge above.
Working with proto-files
The minimum required configuration looks like this:
This enables code generation for your .proto files. Special source sets are created for them:
main
andtest
- source sets for Kotlin/JVM projects. Default source directories are src/main/proto and src/test/proto.jvmMain
andjvmTest
- source sets for Kotlin/Multiplatform projects. Default source directories are src/jsmMain/proto and src/jvmTest/proto.Android source set support is not available yet but will be included in a future release.
All source sets are generated automatically and available via the protoSourceSets
Gradle Project extension:
By default, the following source directories are generated:
java
- protobuf Java declarations, attached tojava
sources.grpc-java
- gRPC Java declarations, attached tojava
sources.grpc-kotlin
- gRPC Kotlin wrappers for Java, attached tokotlin
sources.kotlin-multiplatform
- wrappers for all of the above, attached tokotlin
sources.
Limitations
Source set hierarchy is not supported, and we have no plans to support it. That means, for example, that you can't have jsMain
and jsTest
source sets for proto files. The same applies for native and Wasm targets. Currently, .proto files are only supported in the jvmMain
and jvmTest
source sets. (Later to be replaced with commonMain
and commonTest
)
If you have a use case for other source sets and a hierarchy, please report it.
Protoc plugins
To generate code, we use protoc plugins. By default, we use the following plugins:
protobuf-java - Buf's version of the official Java plugin.
grpc-java - Buf's version of the official gRPC Java plugin.
grpc-kotlin - Buf's version of the official gRPC Kotlin plugin.
kotlin-multiplatform
- out own protoc plugin.
You can configure the plugins in the rpc
block:
protocPlugins
is a NamedDomainObjectContainer<ProtocPlugin>
, which allows you to add your own plugins.
Each plugin is represented by a ProtocPlugin
and can be configured like this:
Buf's plugin configuration options:
- strategy
strategy —
All
orDirectory
- includeImports
includeImports —
true
orfalse
- includeWkt
includeWkt —
true
orfalse
- types
types — a list of types to generate.
- excludeTypes
excludeTypes — a list of types to exclude from generation.
Once a plugin is added to the protocPlugins
container, it can be used in a source set:
Buf
As you may already notice, we use Buf — a tool for managing and building Protobuf schemas. We use its CLI to execute tasks like code generation.
Generated Workspace
To improve the developer experience, we introduce the concept of a generated workspace. It's a directory that contains all the necessary files for Buf to work:
Filtered .proto files
Generated buf.yaml file
Generated buf.gen.yaml file
Automatically managed imports from a
main
source set to atest
source set, making imports intuitive.
This workspace is created automatically for each source set and can be found in the build/protoBuild directory. You don't need to do anything with it, unless you want to customize it.
Tasks
If your project is configured to use gRPC, the following tasks will be generated:
generateBufYaml<sourceSet>
— generates buf.yamlType:
kotlinx.rpc.buf.tasks.GenerateBufYaml
generateBufGenYaml<sourceSet>
— generates buf.gen.yamlType:
kotlinx.rpc.buf.tasks.GenerateBufGenYaml
processProtoFiles<sourceSet>
— copies proto files to the workspace directoryType:
kotlinx.rpc.proto.ProcessProtoFiles
processProtoFilesImport<sourceSet>
— copies import proto files to the workspace directoryType:
kotlinx.rpc.proto.ProcessProtoFiles
bufGenerate<sourceSet>
— runsbuf generate
command.Type:
kotlinx.rpc.buf.tasks.BufGenerateTask
To configure Buf, use the buf
block:
General configuration options:
- logFormat
logFormat — either
Text
,Color
,Json
orDefault
- timeout
timeout — timeout for the
buf
commands, always converted to whole seconds- --debug
Running Gradle with
--debug
enables Buf's--debug
option as well.
buf generate
configuration options:
- includeImports
includeImports —
true
orfalse
- includeWkt
includeWkt —
true
orfalse
- errorFormat
errorFormat — either
Text
,Json
,Msvs
,Junit
,GithubActions
orDefault
Custom tasks
Currently, we only support generate
tasks. However, because of Buf's capabilities for managing .proto files, like linting and detection of breaking changes, we provide a way to create custom Buf tasks.
To create a custom task, extend the kotlinx.rpc.buf.tasks.BufExecTask
class. You don't need to define a @TaskAction
; just a set of arguments and a command to execute.
You can register the task in two ways:
A workspace task (recommended):
rpc.grpc.buf.tasks { val provider = registerWorkspaceTask<MyBufLintTask>("lint") { // configure task here } // bufLintMain, or bufLintJvmMain for KMP projects provider.mainTask // bufLintTest, or bufLintJvmTest for KMP projects provider.testTask }These tasks can be executed on a generated workspace for all proto source sets.
A regular task:
project.registerBufExecTask<MyBufLintTask>( name = "bufLint", workingDir = workingDirProvider, ) { // configure task here }It is a standard Gradle task that has access to the Buf's executable and some predefined properties.