Skip to content

Instantly share code, notes, and snippets.

@ZhangMenghe
Last active April 2, 2023 14:53
Show Gist options
  • Select an option

  • Save ZhangMenghe/9210e1396a97a230e4d2a78b59bb97c1 to your computer and use it in GitHub Desktop.

Select an option

Save ZhangMenghe/9210e1396a97a230e4d2a78b59bb97c1 to your computer and use it in GitHub Desktop.
Protobuf for Android and Android NDK

Using

  • Ubuntu 18.02
  • Android NDK 20.0.5594570
  • Protobuf v3.12.2
  • Language C++ / Java

Use clang:

S1: Generate standalone toolchain

${ANDROID_NDK_HOME}/build/tools/make-standalone-toolchain.sh \
--arch=arm \
--platform=android-28 \
--toolchain=arm-linux-android-clang5.0 \
--install-dir=$HOME/Android/arm-28-toolchain-clang \
--use-llvm \
--stl=libc++

This generates an arm-28 toolchain under Android

S2: Build Protobuf

git clone --recurse--submodules https://github.com/protocolbuffers/protobuf.git

cd protobuf

git checkout v3.12.2

./autogen.sh

mkdir build && cd build

vi build-protobuf-android.sh

wherebuild-protobuf-android.sh

export PREFIX=$HOME/Android/andrc/protobuf-3.12.2/

export PATH=$HOME/Android/arm-28-toolchain-clang/bin:$PATH

export SYSROOT=$HOME/Android/arm-28-toolchain-clang/sysroot

export CC="arm-linux-androideabi-clang --sysroot $SYSROOT"

export CXX="arm-linux-androideabi-clang++ --sysroot $SYSROOT"

  

../configure \

--prefix=$PREFIX \

--host=arm-linux-androideabi \

--with-sysroot="${SYSROOT}" \

--enable-shared \

--enable-cross-compile \

--with-protoc=protoc \

CFLAGS="-march=armv7-a -D__ANDROID_API__=29" \

CXXFLAGS="-frtti -fexceptions -fPIC -march=armv7-a -D__ANDROID_API__=29" \

LIBS="-llog -lz -lc++_static"

  

make -j 8

  

make install

Notice!# (I) Need to manually add ./google/protobuf/.libs/descriptor.pb.o to get protoc to link

/bin/bash ../libtool --tag=CXX --mode=link clang++ -pthread -DHAVE_PTHREAD=1 -DHAVE_ZLIB=1 -Wall -Wno-sign-compare -O2 -g -std=c++11 -DNDEBUG -pthread -o protoc google/protobuf/compiler/main.o libprotobuf.la libprotoc.la ./google/protobuf/.libs/descriptor.pb.o -lz

S3: In NDK

  • copy .a/.so and include to third_party
  • write and generate .proto for cpp use

Android protobuf lite

  • In project's build.gradle add
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'
  • In app's build.gradle on the top, add apply plugin: 'com.google.protobuf' on the dependencies, add
implementation 'com.google.protobuf:protobuf-lite:3.0.0'

then

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.0.0'
    }
    plugins {
        javalite {
            artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.plugins {
                javalite { }
            }
        }
    }
}
@mmdxm
Copy link

mmdxm commented Nov 24, 2022

I do as the article say.
In S2:Build Protobuf ,I generate the libprotobuf.so file. When I add it into my android project and compile, it gets an error :
"/libs/protobuf/libprotobuf.so: error adding symbols: File in wrong format", which means the so file's format is wrong.
But when I try to print the format of libprotobuf.so,
image
I think the format is correct, but why I meet such a error?
thanks for answer

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