Reputation: 345
Suppose we have a project in Android Studio (4.1) which is composed of two modules, an android app module and a module that contains a (Kotlin or Java) server application which is supposed to run on a non-android device (probably on localhost, in the first run). Both modules should use protocol buffers (protobuf). For the android app, this already works like a charme. Up to now, the corresponding protobuf definition (.proto) resides within the android module.
Now, I would like to reuse the protobuf definition in the server application module. I know how to make a module dependent on another, which is the way to go in case one would really want to reuse code from the other module. However, I just want to reuse the .proto and run protoc (generate the protobuf sources and headers) in both modules (protobuf-lite vs protobuf, maybe other language).
Maybe this is easy to answer, but I couldn't find a solution that allows sharing a single file between two modules. Can anyone give me hint?
I found this, Managing Shared Protocol Buffer library and using Gradle to Compile , but this solution assumes that the proto definition is somewhere on the net.
Upvotes: 1
Views: 863
Reputation: 345
After digging for a while, I found a solution myself. I don't know if it is the best, so please comment and let me know what you think.
I just moved my definition file out of the module subdirectory to the top level project directory, in src/main/proto
. From each module, I just add this directory to the proto srcSet. So for the android app:
android {
sourceSets {
main {
proto {
srcDir '../src/main/proto'
}
}
}
}
and for the Java/Kotlin non-android application
sourceSets {
main {
proto {
proto.srcDirs = ['../src/main/proto']
}
}
}
This works as aspected, and I don't have to maintain the .proto-file at different locations. However, are there any drawbacks to move code or code-like files to the top-level of a gradle project?
Upvotes: 1