android - 如何将最新的 c++ protobuf (3.2) 与 Android NDK 集成/安装?

标签 android c++ compilation cross-compiling protobuf-c

早期的protobuf 3.0版本很容易集成。但是最新的3.2版本有问题。它还简单地编译和构建所有不相关的 Android .cc 文件组件。
如果我们按照 git for c++ proto 中最新分支的路径& 按照步骤构建它,然后它生成 15 MB 的 .SO 文件,这与之前的 ~1-2 MB 不同。

如何将最新的protobuf优化集成到Android?

最佳答案

使用跨平台Android Standalone Tool chain并且提供的构建脚本可能会导致更大的共享库文件 (.so),这是由于构建脚本中包含不需要的文件而生成的。您可以创建自己的 Android.mk 文件,该文件仅包含必需的核心部分图书馆的。生成的库文件可以用作其他 Android 项目中的预构建库。请检查以下步骤以了解创建自己的脚本文件的第二个选项。

  1. Git Source Repository 下载最新的 Android Google Protobuf 源代码.
  2. 创建新的 Android 应用程序项目。在应用程序项目中按名称 jni 创建目录。
  3. 从下载的源目录运行以下命令。

./autogen.sh

./configure

  1. 将下载的源代码放在jni目录下。
  2. 将下面的 Android.mk 和 Application.mk 文件放在 jni 目录中。

Android.mk

    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    c_includes := $(LOCAL_PATH)
    LOCAL_MODULE := libprotobuf      

    LOCAL_SRC_FILES := \
    google/protobuf/arena.cc \
    google/protobuf/arenastring.cc \
    google/protobuf/extension_set.cc \
    google/protobuf/generated_message_util.cc \
    google/protobuf/io/coded_stream.cc \
    google/protobuf/io/zero_copy_stream.cc \
    google/protobuf/io/zero_copy_stream_impl_lite.cc \
    google/protobuf/message_lite.cc \
    google/protobuf/repeated_field.cc \
    google/protobuf/stubs/atomicops_internals_x86_gcc.cc \
    google/protobuf/stubs/atomicops_internals_x86_msvc.cc \
    google/protobuf/stubs/bytestream.cc \
    google/protobuf/stubs/common.cc \
    google/protobuf/stubs/int128.cc \
    google/protobuf/stubs/once.cc \
    google/protobuf/stubs/status.cc \
    google/protobuf/stubs/statusor.cc \
    google/protobuf/stubs/stringpiece.cc \
    google/protobuf/stubs/stringprintf.cc \
    google/protobuf/stubs/structurally_valid.cc \
    google/protobuf/stubs/strutil.cc \
    google/protobuf/stubs/time.cc \
    google/protobuf/wire_format_lite.cc \
    google/protobuf/any.cc \
    google/protobuf/any.pb.cc \
    google/protobuf/api.pb.cc \
    google/protobuf/compiler/importer.cc \
    google/protobuf/compiler/parser.cc \
    google/protobuf/descriptor.cc \
    google/protobuf/descriptor.pb.cc \
    google/protobuf/descriptor_database.cc \
    google/protobuf/duration.pb.cc \
    google/protobuf/dynamic_message.cc \
    google/protobuf/empty.pb.cc \
    google/protobuf/extension_set_heavy.cc \
    google/protobuf/field_mask.pb.cc \
    google/protobuf/generated_message_reflection.cc \
    google/protobuf/io/gzip_stream.cc \
    google/protobuf/io/printer.cc \
    google/protobuf/io/strtod.cc \
    google/protobuf/io/tokenizer.cc \
    google/protobuf/io/zero_copy_stream_impl.cc \
    google/protobuf/map_field.cc \
    google/protobuf/message.cc \
    google/protobuf/reflection_ops.cc \
    google/protobuf/service.cc \
    google/protobuf/source_context.pb.cc \
    google/protobuf/struct.pb.cc \
    google/protobuf/stubs/mathlimits.cc \
    google/protobuf/stubs/substitute.cc \
    google/protobuf/text_format.cc \
    google/protobuf/timestamp.pb.cc \
    google/protobuf/type.pb.cc \
    google/protobuf/unknown_field_set.cc \
    google/protobuf/util/field_comparator.cc \
    google/protobuf/util/field_mask_util.cc \
    google/protobuf/util/internal/datapiece.cc \
    google/protobuf/util/internal/default_value_objectwriter.cc \
    google/protobuf/util/internal/error_listener.cc \
    google/protobuf/util/internal/field_mask_utility.cc \
    google/protobuf/util/internal/json_escaping.cc \
    google/protobuf/util/internal/json_objectwriter.cc \
    google/protobuf/util/internal/json_stream_parser.cc \
    google/protobuf/util/internal/object_writer.cc \
    google/protobuf/util/internal/proto_writer.cc \
    google/protobuf/util/internal/protostream_objectsource.cc \
    google/protobuf/util/internal/protostream_objectwriter.cc \
    google/protobuf/util/internal/type_info.cc \
    google/protobuf/util/internal/type_info_test_helper.cc \
    google/protobuf/util/internal/utility.cc \
    google/protobuf/util/json_util.cc \
    google/protobuf/util/message_differencer.cc \
    google/protobuf/util/time_util.cc \
    google/protobuf/util/type_resolver_util.cc \
    google/protobuf/wire_format.cc \
    google/protobuf/wrappers.pb.cc \
    google/protobuf/compiler/code_generator.cc      \
    google/protobuf/compiler/command_line_interface.cc      \
    google/protobuf/compiler/cpp/cpp_enum.cc      \
    google/protobuf/compiler/cpp/cpp_enum_field.cc      \
    google/protobuf/compiler/cpp/cpp_extension.cc      \
    google/protobuf/compiler/cpp/cpp_field.cc      \
    google/protobuf/compiler/cpp/cpp_file.cc      \
    google/protobuf/compiler/cpp/cpp_generator.cc      \
    google/protobuf/compiler/cpp/cpp_helpers.cc      \
    google/protobuf/compiler/cpp/cpp_map_field.cc      \
    google/protobuf/compiler/cpp/cpp_message.cc      \
    google/protobuf/compiler/cpp/cpp_message_field.cc      \
    google/protobuf/compiler/cpp/cpp_primitive_field.cc      \
    google/protobuf/compiler/cpp/cpp_service.cc      \
    google/protobuf/compiler/cpp/cpp_string_field.cc      \
    google/protobuf/compiler/plugin.cc      \
    google/protobuf/compiler/plugin.pb.cc      \
    google/protobuf/compiler/python/python_generator.cc      \
    google/protobuf/compiler/ruby/ruby_generator.cc      \
    google/protobuf/compiler/subprocess.cc      \
    google/protobuf/compiler/zip_writer.cc      \


    LOCAL_CFLAGS := -D GOOGLE_PROTOBUF_NO_RTTI=1
    LOCAL_CPPFLAGS := -std=c++11
    LOCAL_C_INCLUDES = $(LOCAL_PATH)/android 
    LOCAL_C_INCLUDES += ${ANDROID_NDK}/sources/cxx-stl/gnu-        libstdc++/4.8/include
    LOCAL_LDLIBS += -lz
    LOCAL_EXPORT_LDLIBS += -lz
    LOCAL_EXPORT_CFLAGS := $(LOCAL_CFLAGS)
    LOCAL_EXPORT_CPPFLAGS := $(LOCAL_CPPFLAGS)
    LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)


    include $(BUILD_SHARED_LIBRARY)

Application.mk

APP_STL :=gnustl_shared # gnustl_static
NDK_TOOLCHAIN_VERSION := 4.9
APP_ABI := all
LIBCXX_FORCE_REBUILD := true
APP_PLATFORM:=android-9
NDK_DEBUG:=1
  1. 运行ndk-build,所需的.so文件将在项目目录的libs文件夹中生成。

关于android - 如何将最新的 c++ protobuf (3.2) 与 Android NDK 集成/安装?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42458739/

相关文章:

java - 如何在Android(NDK r20)上修复(process_begin : CreateProcess(NULL, "",...)失败。)?

c++ - 将文本绘制到屏幕

c++ - 从 Makefile (Qt) 中删除标志 (-fpermissive)

android - 如何在 Glide/Picasso/Ion 等的占位符中加载 gif 图像

java - 无法解析构造函数 'ImageButton()'

c++ - std::abs 为 "unsigned long long int"数据类型

java - 使用类路径编译不寻常的 Java 类

haskell - 为什么 Haskell/GHC 可执行文件的文件大小如此之大?

c++ - 如何找到编译瓶颈?

javascript - 将回调/函数从 native 发送到 javascript