libprotobuf.a: X compile with Android NDK using Bazel
To build libprotobuf.a
using Bazel and the Android NDK, follow these steps:
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 Bazel:
- Follow the instructions from Bazel’s official site to install Bazel for our Linux distribution. We can also install it using:
sudo apt install apt-transport-https curl gnupg -y
curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor >bazel-archive-keyring.gpg
sudo mv bazel-archive-keyring.gpg /usr/share/keyrings
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/bazel-archive-keyring.gpg] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
sudo apt update && sudo apt-get install bazel
4. Download Protobuf Source:
- Clone the protobuf repository and check out version 5.27.1:
git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf
git checkout v5.27.1
5. Install Python (if needed):
- Bazel will need Python to configure the protobuf build.
Step-by-Step Process
1. Setup Bazel Configuration for Android NDK
We need to configure Bazel to use the Android NDK. To do that, we need to set up an ndk
in our Bazel WORKSPACE
file.
- Open or create the
WORKSPACE
file in the root of our protobuf directory:
vim WORKSPACE
- Add the Android NDK and SDK configurations to our
WORKSPACE
file:
android_ndk_repository(
name = "androidndk",
path = "/path/to/our/android-ndk",
api_level = 21, # Set to the desired Android API level.
)
- Replace
/path/to/our/android-ndk
with the actual paths to your Android NDK installations.
2. Configure Android NDK in .bazelrc
Bazel uses .bazelrc
to manage the build configuration. We need to specify the Android NDK and the desired architecture in this file.
- Create or open the
.bazelrc
file:
vim .bazelrc
- Add the following configuration to build for Android:
build --config=android
build:android --cpu=arm64-v8a
build:android --fat_apk_cpu=arm64-v8a
- We can change the CPU architecture (
arm64-v8a
,armeabi-v7a
,x86_64
) according to the architecture we need.
3. Build libprotobuf.a
using Bazel
Bazel allows we to directly target and build specific libraries. In this case, we’ll be building the static library (libprotobuf.a
).
- Run the following Bazel command to build
libprotobuf.a
:
bazel build //src/google/protobuf:protobuf --config=android --cpu=arm64-v8a
4. Locate the Output
After the build is successful, Bazel will output the compiled static library (libprotobuf.a
). We can find it under the bazel-bin
directory inside our project:
bazel-bin/src/google/protobuf/libprotobuf.a
bazel-bin/src/google/protobuf/libprotobuf.so
- Multiple Architectures: To build for multiple architectures, we can pass different
--cpu
values likearm64-v8a
,armeabi-v7a
,x86_64
, etc. We can build for multiple architectures in one go by passing--fat_apk_cpu=arm64-v8a,armeabi-v7a,x86_64
. - Building Other Protobuf Components: We can replace
//:protobuf
with other targets, such as//:protoc_lib
or//:protobuf_lite
, depending on our needs.
This process cross-compiles Protobuf for Android and outputs the libprotobuf.a
static library using Bazel and the NDK.