Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Build Google Protobuf library for android development. With android ndk stand alone tool chains.
PREFIX=`pwd`/protobuf/android
rm -rf ${PREFIX}
mkdir ${PREFIX}
export NDK=YOUR_NDK_ROOT
# 1. Use the tools from the Standalone Toolchain
export PATH=YOUR_NDK_STAND_ALONE_TOOL_PATH/bin:$PATH
export SYSROOT=YOUR_NDK_STAND_ALONE_TOOL_PATH/sysroot
export CC="arm-linux-androideabi-gcc --sysroot $SYSROOT"
export CXX="arm-linux-androideabi-g++ --sysroot $SYSROOT"
export CXXSTL=$NDK/sources/cxx-stl/gnu-libstdc++/4.6
##########################################
# Fetch Protobuf 2.5.0 from source.
##########################################
(
cd /tmp
curl http://protobuf.googlecode.com/files/protobuf-2.5.0.tar.gz --output /tmp/protobuf-2.5.0.tar.gz
if [ -d /tmp/protobuf-2.5.0 ]
then
rm -rf /tmp/protobuf-2.5.0
fi
tar xvf /tmp/protobuf-2.5.0.tar.gz
)
cd /tmp/protobuf-2.5.0
mkdir build
# 3. Run the configure to target a static library for the ARMv7 ABI
# for using protoc, you need to install protoc to your OS first, or use another protoc by path
./configure --prefix=$(pwd)/build \
--host=arm-linux-androideabi \
--with-sysroot=$SYSROOT \
--disable-shared \
--enable-cross-compile \
--with-protoc=protoc \
CFLAGS="-march=armv7-a" \
CXXFLAGS="-march=armv7-a -I$CXXSTL/include -I$CXXSTL/libs/armeabi-v7a/include"
# 4. Build
make && make install
# 5. Inspect the library architecture specific information
arm-linux-androideabi-readelf -A build/lib/libprotobuf-lite.a
cp build/lib/libprotobuf.a $PREFIX/libprotobuf.a
cp build/lib/libprotobuf-lite.a $PREFIX/libprotobuf-lite.a
@keithkml

This comment has been minimized.

Copy link

keithkml commented Apr 17, 2015

Thanks this was very helpful!

@aso123

This comment has been minimized.

Copy link

aso123 commented Apr 12, 2017

Could you please specify a sample environment variable path for sysroot as I don't find such a folder in my ndk of version r10e?
I am working on Centos 7 and try to cross compile protobuf for android. I use protobuf in a Qt console project. ABIs of my phone are arm64-v8a, armeabi-v7a and armeabi.
How/Do I have to modify your script to build for my project? Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.