Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

kotlin - How to share a proto definition file between an android module and a non-android module (without using another module)?

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.

question from:https://stackoverflow.com/questions/65841346/how-to-share-a-proto-definition-file-between-an-android-module-and-a-non-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...