前言
正文
直入正题。编译protobuf的android版本我们使用的环境如下
CMake: 3.6
NDK: r16b
Protobuf: v3.5.1
OS : Mac os
首先我们下载protobuf的代码https://github.com/google/protobuf/releases 直接下载cpp平台的就可以了。
1 首先进入到cmake目录下,创建一个文件夹 build-armeabi-v7a
2 然后我们在根目录下(protobuf的顶层目录)添加一个android.sh,代码如下
cd cmake && cd build-armeabi-v7a
# Step 3. Generate Makefile with CMake.
# -Dprotobuf_BUILD_SHARED_LIBS=ON By default is static library (*.a file). I want a
# shared library (*.so file).
# -DCMAKE_INSTALL_PREFIX Because it's a cross-compiled library. You probably
# want to install the header files and shared library
# in specific folder rather than default /usr/local
# directory.
# -DANDROID_STL=c++_shared For the library using C++11, link to C++11 runtime.
# -DANDROID_LINKER_FLAGS="-landroid -llog" For the library using functions of libandroid.so
# and liblog.so.
# -DANDROID_CPP_FEATURES="rtti exceptions" Most ppl use exception and runtime-type-information
# features in their C++ projects.
#
# Debug Tips:
# Add -LAH to see variables.
#/Users/yxwang/Library/Android/sdk/cmake/3.6.4111459/bin/cmake \
<your-path-of>/cmake \
-Dprotobuf_BUILD_SHARED_LIBS=OFF \
-Dprotobuf_BUILD_STATIC_LIBS=ON \
-DCMAKE_VERBOSE_MAKEFILE=ON \
-Dprotobuf_BUILD_TEST=OFF \
-Dprotobuf_BUILD_EXAMPLES=OFF \
-DCMAKE_TOOLCHAIN_FILE=/Users/yxwang/Library/Android/sdk/ndk-bundle/build/cmake/android.toolchain.cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=<install-path> \
-DANDROID_NDK=<ndk-path> \
-DANDROID_TOOLCHAIN=clang \
-DANDROID_ABI=armeabi-v7a \
-DANDROID_NATIVE_API_LEVEL=16 \
-DANDROID_STL=c++_shared \
-DANDROID_LINKER_FLAGS="-landroid -llog" \
-DANDROID_CPP_FEATURES="rtti exceptions" \
..
# Step 4. Run Make with generated Makefile.
<your-path-of>/cmake --build .
# Step 5 (optional). Install the generated header files and shared library
# to specific folder.
make install
PS:注意这里的android.toolchain.cmake的地址,并不是直接放在sdk/cmake/version/下的那个文件……使用这个文件会报错。可以在android studio中创建一个拥有c++的项目,然后编译一次,在.externalNativeBuild/cmake_build_command.txt中能够找到位置,直接拿来用就行。
另外这里生成的是.a的静态库,如果需要.so的动态库,可以吧上面protobuf_build_shared_libs这个编译选项设置为on
3. 在运行之前还需要修改原来的 cmake文件
我这里只留下了 protobuf-lite的库,所以最后也只会生成这个库。如果需要libprotobuf.so库,可以把这部分也放出来。
不过libprotoc和protoc是无法使用ndk编译的,所以一定需要注释掉。
在项目中使用的时候可能会出现这样的提示
Error:FAILURE: Build failed with an exception.
* What went wrong:
.....
FAILED: : && /Users/yxwang/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=aarch64-none-linux-android --gcc-toolchain=/Users/yxwang/Library/Android/sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/yxwang/Library/Android/sdk/ndk-bundle/sysroot -fPIC -isystem /Users/yxwang/Library/Android/sdk/ndk-bundle/sysroot/usr/include/aarch64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -frtti -fexceptions --std=c++1z -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a --sysroot /Users/yxwang/Library/Android/sdk/ndk-bundle/platforms/android-21/arch-arm64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -L/Users/yxwang/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/arm64-v8a -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libTKATPROTOBUF.so -o ../../../../build/intermediates/cmake/debug/obj/arm64-v8a/libTKATPROTOBUF.so src/main/cpp/shared/tkat.protobuf/CMakeFiles/TKATPROTOBUF.dir/src/protobuf-handler.cpp.o src/main/cpp/shared/tkat.protobuf/CMakeFiles/TKATPROTOBUF.dir/src/protobuf-message-decorator.cpp.o src/main/cpp/shared/tkat.protobuf/CMakeFiles/TKATPROTOBUF.dir/src/tkat.dbf.sjs.pb.cc.o src/main/cpp/shared/tkat.protobuf/CMakeFiles/TKATPROTOBUF.dir/src/tkat.login.pb.cc.o src/main/cpp/shared/tkat.protobuf/CMakeFiles/TKATPROTOBUF.dir/src/tkat.pb.cc.o src/main/cpp/shared/tkat.protobuf/CMakeFiles/TKATPROTOBUF.dir/src/tkat.quote.pb.cc.o ../../../../src/main/cpp/protobuf/arm64-v8a/lib/libprotobuf.a -latomic -lm "/Users/yxwang/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++.a" && :
../../../../src/main/cpp/protobuf/arm64-v8a/lib/libprotobuf.a(common.cc.o): In function `google::protobuf::internal::DefaultLogHandler(google::protobuf::LogLevel, char const*, int, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&)':
/Users/yxwang/Desktop/protobuf-3.5.1/src/google/protobuf/stubs/common.cc:142: undefined reference to `__android_log_write'
/Users/yxwang/Desktop/protobuf-3.5.1/src/google/protobuf/stubs/common.cc:150: undefined reference to `__android_log_write'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
BUILD FAILED in 22s
表示_android_log_write未定义,而是是报在静态库中的。
解决方案
find_library( # Defines the name of the path variable that stores the
# location of the NDK library.
log-lib
# Specifies the name of the NDK library that
# CMake needs to locate.
log)
target_link_libraries(TKATPROTOBUF PUBLIC ${PROTOBUF_LIBRARY} ${log-lib})
在使用Protobuf库的时候,同时添加log-lib库的链接。