Search code examples
c++cmakeandroid-ndkgrpccross-compiling

GRPC cross compile for android using NDK toolchain


I am fairly new to this. So, I am trying to cross compile GRPC (https://github.com/grpc/grpc) for android using cmake and NDK toolchain to generate shared libraries, using the below:

cmake -DCMAKE_TOOLCHAIN_FILE=$And_PATH/android.toolchain.cmake -DCMAKE_CROSSCOMPILING=true -DANDROID_ABI=arm64-v8a -DCMAKE_INSTALL_PREFIX=/cross_grpc -DANDROID_PLATFORM=android-26 -DANDROID_STL=c++_shared CFLAGS="std=c11" CXXFLAGS="std=c++14" cmake -B . -S /grpc

With the above i am able to build and static libraries get generated, but if i add -DBUILD_SHARED_LIBS=ON , i am getting below errors

ld: error: undefined symbol: BIO_ctrl referenced by bio_ssl.cc:191 (/grpc/third_party/boringssl-with-bazel/src/ssl/bio_ssl.cc:191)

ld: error: undefined symbol: BIO_clear_retry_flags referenced by bio_ssl.cc:65 (/grpc/third_party/boringssl-with-bazel/src/ssl/bio_ssl.cc:65)

ld: error: undefined symbol: BIO_set_retry_write referenced by bio_ssl.cc:71 (/grpc/third_party/boringssl-with-bazel/src/ssl/bio_ssl.cc:71)

I have not pasted all the errors as it's a long list of undefined symbol errors.


Solution

  • So, I was able to cross compile it. Below cmake command I used. Make sure to play around the flag according to your situation.

    -> git clone https://github.com/grpc/grpc

    -> cd grpc

    -> git submodule update --init

    -> mkdir cross_grpc && cd cross_grpc

    cmake -DCMAKE_TOOLCHAIN_FILE=/Android/Sdk/ndk/25.1.8937393/build/cmake/android.toolchain.cmake -DCMAKE_CROSSCOMPILING=true -DANDROID_ABI=arm64-v8a -DCMAKE_INSTALL_PREFIX=/cross_grpc -DANDROID_PLATFORM=android-26 -DCMAKE_BUILD_TYPE=Release -DANDROID_STL=c++_shared CFLAGS="std=c11 -Wl -export-dynamic" CXXFLAGS="std=c++14 -Wl -export-dynamic" -DCMAKE_BUILD_TYPE=Release -DOPENSSL_ROOT_DIR=/etc/ssl/ -B . -S /grpc
    
    make "-j4" install