Skip to content

Instantly share code, notes, and snippets.

@SimonLeeGit
Created March 7, 2024 12:58
Show Gist options
  • Save SimonLeeGit/49190bdd4e75823aa2cad506ffd4a705 to your computer and use it in GitHub Desktop.
Save SimonLeeGit/49190bdd4e75823aa2cad506ffd4a705 to your computer and use it in GitHub Desktop.
How to build protobuf for android

How to build protobuf for android

For game development, protobuf is very useful for network message protocal. For mobile android platform, we need the libs for android. But we can not find the tutorial about the build step for android platform. Here I show a way about this.

Install Android NDK and SDK

You can download and install android studio. Then install the sdk and ndk for you need.

Create a Standalone Toolchain (optional)

It’s recommended to create a standalone toolchain for building protobuf. This toolchain will be specific to the Android API level and architecture you want to target.

python $NDK_ROOT/build/tools/make_standalone_toolchain.py --arch arm64 --api 21 --install-dir /path/to/standalone-toolchain

Replace /path/to/standalone-toolchain with the directory where you want to install the standalone toolchain.

Set Environment Variables for Standalone Toolchain (optional)

If you created a standalone toolchain, set the following environment variables:

export PATH=/path/to/standalone-toolchain/bin:$PATH
export CC=arm64-linux-android21-clang
export CXX=arm64-linux-android21-clang++

Adjust the paths and filenames based on your standalone toolchain and the Android API level you are targeting.

Download and prepare for your protobuf build

git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf
git submodule update --init --recursive
./autogen.sh

Build protobuf for android

./configure --host=arm-linux-androideabi --with-sysroot=/path/to/standalone-toolchain/sysroot
make

You can set host as you need.

  • arm: arm-linux-androideabi
  • arm64: aarch64-linux-android
  • x86: i686-linux-android
  • x86_64: x86_64-linux-android

After successfully building protobuf, you can install it to a specific location or directly copy the necessary files to your Android project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment