libprotobuf.a: X compile with Android NDK
Useful while working on Native library.
To cross-compile Protobuf for Android using the NDK toolchain and generate a static library (libprotobuf.a
), follow the detailed steps below.
Prerequisites
These prerequisites are what i tried with while building.
- Ubuntu: 20.4
- Install Android NDK: Download and install the Android NDK from the NDK official site. I tried with NDK version
v21e
- Install build tools: We will need
autoconf
,automake
,libtool
,cmake
, andninja
. We can install them via our package manager:
sudo apt-get install autoconf automake libtool cmake ninja-build
Step-by-Step Guide
Step 1: Download and Extract Protobuf
- Clone the latest release of Protobuf from GitHub, but fetch
v.3.5.1
because post this protobuf userbazel
for building. (This was a surprise for me, and took quite some to figure out)
git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf && git checkout v3.5.1
Step 2: Set Up the Android NDK Toolchain
- Set up environment variables for the Android target (replace
arm64-v8a
with our target architecture):
export ANDROID_NDK=<path-to-android-ndk>
export TOOLCHAIN=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64
export TARGET=aarch64-linux-android
export API=21 # We can change this depending on our minimum supported Android version
2. Set the cross-compiling environment variables for aarch64
:
export AR=$TOOLCHAIN/bin/$TARGET-ar
export AS=$TOOLCHAIN/bin/$TARGET-as
export CC=$TOOLCHAIN/bin/$TARGET$API-clang
export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
export LD=$TOOLCHAIN/bin/$TARGET-ld
export RANLIB=$TOOLCHAIN/bin/$TARGET-ranlib
export STRIP=$TOOLCHAIN/bin/$TARGET-strip
Step 3: Configure Protobuf for Cross-Compilation
- Install
protobuf
build dependencies: We need to installlibtool
,autoconf
, andautomake
:
sudo apt-get install libtool autoconf automake
2. Generate the configuration files for Protobuf:
./autogen.sh
mkdir build && cd build
3. Configure Protobuf with Android toolchain: Now configure the Protobuf library to use the Android NDK toolchain:
../configure --host=$TARGET --with-sysroot=$TOOLCHAIN/sysroot --enable-static --disable-shared --disable-tests CXXFLAGS="-fPIC" CC=$CC CXX=$CXX LDFLAGS="-llog -lz"
NOTE: We are disabling test, else you will see build error because the tests the target and build envornments are different.
Step 4: Compile Protobuf
- Build the static library:
make -j 2
This will build libprotobuf.a
for Android for above specified target type. Target different architectures by changing the TARGET
and API
values (e.g., armv7a-linux-androideabi
for ARM 32-bit, x86_64-linux-android
for x86_64).
NOTE: Make sure all the build tools have proper path, example: for armv7a-linux-androideabi
the AR/AS/LD/… tool has following path:
export AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar
Step 5: Verify the Output
- After the build completes, check for the generated
libprotobuf.a
file in thesrc/.libs/
directory.
ls src/.libs/libprotobuf.a
Final words, if configure
fails, mostly it is something to do with the export path,
double check them all.