5

How to compile C++ gRPC code for Android? I have seen several tutorials on how Protobuf itself can be compiled using the Android Native Development Kit, such as in the answer from Swapnil: How to integrate/install latest c++ protobuf (3.2) with Android NDK?

Or Google protobuf and Android NDK

But how to How to compile C++ gRPC code that is using Protobuf as well for Android? Taking into account that there's 20K lines of gRPC Makefile.

Taier
  • 2,109
  • 12
  • 22
  • 1
    gRPC has a CMakeLists.txt. Glancing at it, I see nothing that should prevent it to be used to generate NDK targets, –  Jan 03 '18 at 20:49
  • Did you eventually make it? – Michael Litvin Sep 06 '18 at 08:41
  • @David how my question can be a duplicate, if it was asked more than a year ago and this one just a few days ago? Also my question is not only about compiling gRPC, but protobuf as well. – Taier Jan 18 '19 at 07:15
  • @Taier Ok I mark this other question as duplicate of yours. And the other question has answers addressing grpc and protobuf compilation as grpc depends on protobuf package : building grpc leads to building protobuf – david Jan 18 '19 at 13:32

2 Answers2

1

I do it with dockcross (which does not do anything fancy, it just sets up the toolchain in a convenient way).

Say for android-arm (but it works for android-arm64, android-x86 and android-x86_64 just the same):

$ git clone https://github.com/grpc/grpc --recursive
$ cd grpc
$ docker run --rm dockcross/android-arm > ./dockcross-android-arm
$ chmod +x dockcross-android-arm
$ ./dockcross-android-arm cmake -DgRPC_BUILD_CODEGEN=OFF -Bbuild -S.
$ ./dockcross-android-arm cmake --build build

It will build all the dependencies in the gRPC submodules. It should work in your project if you add gRPC as a submodule (well, up to some point depending on your transitive dependencies situation, I guess).

This said, I am not a big fan of using git submodules for dependencies with CMake. Instead I like to build the dependencies separately and have my project find them with find_package. I explain that in more details here, and more importantly I have an example doing exactly that for gRPC here.

JonasVautherin
  • 7,297
  • 6
  • 49
  • 95
0

I've done it here.

In brief: git submodule gRPC C++, add "CMakeLists.txt" file to your project on how to build C++ gRPC (and your code that uses it), add a externalNativeBuild/cmake it to your "build.gradle" - voila!

4ntoine
  • 19,816
  • 21
  • 96
  • 220