Android native使用gRPC通信的一种实现方式

目标是在Android上使用gRPC进行通信,当然是在Android的Native层的需求,应用层要使用gRPC会方便的多,搜一下资源,build.gradle配置一下依赖就能用了。下面在Ubuntu系统中来编译在Android Native运行的gRPC的库,分为:
1 下载gRPC源码
2 使用Android源码自带的编译工具编译gRPC
3 使用gRPC

下载gRPC源码

在github下载gRPC项目,然后更新子模块:

git clone https://gitee.com/githubplus/grpc.git
cd grpc
git tag
git checkout v1.20.0

更新submodule:

git submodule update --init

直接更新速度特别慢,甚至n次的中断,不知道什么岁月才能更新完,因此请修改.gitmodules文件,替换其中的github源为gitee源

vim .gitmodules

// replace content in .gitmodules
[submodule "third_party/zlib"]
	path = third_party/zlib
	url = https://gitee.com/githubplus/zlib
	# When using CMake to build, the zlib submodule ends up with a
	# generated file that makes Git consider the submodule dirty. This
	# state can be ignored for day-to-day development on gRPC.
	ignore = dirty
[submodule "third_party/protobuf"]
	path = third_party/protobuf
	url = https://gitee.com/githubplus/protobuf.git
	branch = 3.0.x
[submodule "third_party/gflags"]
	path = third_party/gflags
	url = https://gitee.com/githubplus/gflags.git
[submodule "third_party/googletest"]
	path = third_party/googletest
	url = https://gitee.com/githubplus/googletest.git
[submodule "third_party/boringssl"]
	path = third_party/boringssl
	url = https://gitee.com/githubplus/boringssl.git
[submodule "third_party/benchmark"]
	path = third_party/benchmark
	url = https://gitee.com/githubplus/benchmark.git
[submodule "third_party/boringssl-with-bazel"]
	path = third_party/boringssl-with-bazel
	url = https://gitee.com/githubplus/boringssl.git
[submodule "third_party/cares/cares"]
	path = third_party/cares/cares
	url = https://gitee.com/githubplus/c-ares.git
	branch = cares-1_12_0
[submodule "third_party/bloaty"]
	path = third_party/bloaty
	url = https://gitee.com/githubplus/bloaty.git
[submodule "third_party/abseil-cpp"]
	path = third_party/abseil-cpp
	url = https://gitee.com/githubplus/abseil-cpp
[submodule "third_party/libcxxabi"]
	path = third_party/libcxxabi
	url = https://gitee.com/githubplus/libcxxabi.git
	branch = release_60
[submodule "third_party/libcxx"]
	path = third_party/libcxx
	url = https://gitee.com/githubplus/libcxx.git
	branch = release_60
[submodule "third_party/data-plane-api"]
	path = third_party/data-plane-api
	url = https://gitee.com/githubplus/data-plane-api.git
[submodule "third_party/googleapis"]
	path = third_party/googleapis
	url = https://gitee.com/githubplus/googleapis.git
[submodule "third_party/protoc-gen-validate"]
	path = third_party/protoc-gen-validate
	url = https://gitee.com/githubplus/protoc-gen-validate.git
[submodule "third_party/upb"]
	path = third_party/upb
	url = https://gitee.com/githubplus/upb.git

gRPC的submodule就更新完成了,然后进到protobuf,同样修改protobuf的.gitmodules,再更新其submodule:

cd third_party/protobuf
vim .gitmodules

// replace content in .gitmodules
[submodule "third_party/benchmark"]
	path = third_party/benchmark
	url = https://gitee.com/githubplus/benchmark.git
[submodule "third_party/googletest"]
	path = third_party/googletest
	url = https://gitee.com/githubplus/googletest.git
	ignore = dirty

All done for source code!

使用Android源码自带的编译工具编译gRPC

安装依赖:

sudo apt-get install pkg-config
sudo apt-get install autoconf automake libtool make g++ unzip
sudo apt-get install libgflags-dev libgtest-dev

先编译x86的gRPC,在Ubuntu系统中直接编译:

sudo make
sudo make install

安装protobuf

cd third_party/protobuf/
./autogen.sh
./configure
sudo make
sudo make install

测试gRPC,编译运行Hello world

cd examples/cpp/helloworld/
make

开启一个服务

./greeter_server 

在另一个terminal

./greeter_client 

然后编译arm64的gRPC,使用Android源码中的交叉编译工具:

sudo make GRPC_CROSS_COMPILE=true GRPC_CROSS_AROPTS="cr --target=elf64-little" HAS_PKG_CONFIG=false HAS_EMBEDDED_PROTOBUF=true CC=aosp/prebuilts/gcc/linux-x86/arm/toolchain/bin/arm-linux-androideabi-gcc CXX=aosp/prebuilts/gcc/linux-x86/arm/toolchain/bin/arm-linux-androideabi-g++ RANLIB=aosp/prebuilts/gcc/linux-x86/arm/toolchain/bin/aarch64-linux-gnu-gcc-ranlib LD=aosp/prebuilts/gcc/linux-x86/arm/toolchain/bin/arm-linux-androideabi-gcc LDXX=aosp/prebuilts/gcc/linux-x86/arm/toolchain/bin/arm-linux-androideabi-g++ AR=aosp/prebuilts/gcc/linux-x86/arm/toolchain/bin/arm-linux-androideabi-ar STRIP=aosp/prebuilts/gcc/linux-x86/arm/toolchain/bin/arm-linux-androideabi-strip PROTOBUF_CONFIG_OPTS="--host=arm-linux-androideabi --with-protoc=/usr/local/protobuf/bin/protoc"

在libs/opt目录下会有生成的静态库和动态库,可使用libgrpc.a&libgrpc++.a&libgrpc++_reflection.a&protobuf/libprotobuf.a这4个静态库。

使用gRPC

直接修改examples/cpp/helloworld/的MakeFile测试一下:

CXX = /aosp/prebuilts/gcc/linux-x86/arm/toolchain/bin/arm-linux-androideabi-g++
CXXFLAGS += -std=c++11 -I/usr/local/protobuf/include -I/usr/local/include -fPIC
LDFLAGS += -L. -lprotobuf -lgrpc++ -lgrpc -lglog\
           -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed\
           -ldl -lpthread -static

make一下,push到Android系统的/system/bin运行起来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值