Android ndk 如何用androidndk构建协议缓冲区

Android ndk 如何用androidndk构建协议缓冲区,android-ndk,protocol-buffers,porting,Android Ndk,Protocol Buffers,Porting,我想建立谷歌协议缓冲区库的本机版本。 我将如何做到这一点?我使用这个Android.mk并成功构建 # Copyright (C) 2009 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the Lic

我想建立谷歌协议缓冲区库的本机版本。

我将如何做到这一点?

我使用这个Android.mk并成功构建



    # Copyright (C) 2009 The Android Open Source Project
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #      http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    #
    #

    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    CC_LITE_SRC_FILES := \
        src/google/protobuf/stubs/common.cc                              \
        src/google/protobuf/stubs/once.cc                                \
        src/google/protobuf/stubs/hash.cc                                \
        src/google/protobuf/extension_set.cc                             \
        src/google/protobuf/generated_message_util.cc                    \
        src/google/protobuf/message_lite.cc                              \
        src/google/protobuf/repeated_field.cc                            \
        src/google/protobuf/wire_format_lite.cc                          \
        src/google/protobuf/io/coded_stream.cc                           \
        src/google/protobuf/io/zero_copy_stream.cc                       \
        src/google/protobuf/io/zero_copy_stream_impl_lite.cc


    COMPILER_SRC_FILES :=  \
        src/google/protobuf/descriptor.cc \
        src/google/protobuf/descriptor.pb.cc \
        src/google/protobuf/descriptor_database.cc \
        src/google/protobuf/dynamic_message.cc \
        src/google/protobuf/extension_set.cc \
        src/google/protobuf/extension_set_heavy.cc \
        src/google/protobuf/generated_message_reflection.cc \
        src/google/protobuf/generated_message_util.cc \
        src/google/protobuf/message.cc \
        src/google/protobuf/message_lite.cc \
        src/google/protobuf/reflection_ops.cc \
        src/google/protobuf/repeated_field.cc \
        src/google/protobuf/service.cc \
        src/google/protobuf/text_format.cc \
        src/google/protobuf/unknown_field_set.cc \
        src/google/protobuf/wire_format.cc \
        src/google/protobuf/wire_format_lite.cc \
        src/google/protobuf/compiler/code_generator.cc \
        src/google/protobuf/compiler/command_line_interface.cc \
        src/google/protobuf/compiler/importer.cc \
        src/google/protobuf/compiler/main.cc \
        src/google/protobuf/compiler/parser.cc \
        src/google/protobuf/compiler/plugin.cc \
        src/google/protobuf/compiler/plugin.pb.cc \
        src/google/protobuf/compiler/subprocess.cc \
        src/google/protobuf/compiler/zip_writer.cc \
        src/google/protobuf/compiler/cpp/cpp_enum.cc \
        src/google/protobuf/compiler/cpp/cpp_enum_field.cc \
        src/google/protobuf/compiler/cpp/cpp_extension.cc \
        src/google/protobuf/compiler/cpp/cpp_field.cc \
        src/google/protobuf/compiler/cpp/cpp_file.cc \
        src/google/protobuf/compiler/cpp/cpp_generator.cc \
        src/google/protobuf/compiler/cpp/cpp_helpers.cc \
        src/google/protobuf/compiler/cpp/cpp_message.cc \
        src/google/protobuf/compiler/cpp/cpp_message_field.cc \
        src/google/protobuf/compiler/cpp/cpp_primitive_field.cc \
        src/google/protobuf/compiler/cpp/cpp_service.cc \
        src/google/protobuf/compiler/cpp/cpp_string_field.cc \
        src/google/protobuf/compiler/java/java_enum.cc \
        src/google/protobuf/compiler/java/java_enum_field.cc \
        src/google/protobuf/compiler/java/java_extension.cc \
        src/google/protobuf/compiler/java/java_field.cc \
        src/google/protobuf/compiler/java/java_file.cc \
        src/google/protobuf/compiler/java/java_generator.cc \
        src/google/protobuf/compiler/java/java_helpers.cc \
        src/google/protobuf/compiler/java/java_message.cc \
        src/google/protobuf/compiler/java/java_message_field.cc \
        src/google/protobuf/compiler/java/java_primitive_field.cc \
        src/google/protobuf/compiler/java/java_service.cc \
        src/google/protobuf/compiler/javamicro/javamicro_enum.cc \
        src/google/protobuf/compiler/javamicro/javamicro_enum_field.cc \
        src/google/protobuf/compiler/javamicro/javamicro_field.cc \
        src/google/protobuf/compiler/javamicro/javamicro_file.cc \
        src/google/protobuf/compiler/javamicro/javamicro_generator.cc \
        src/google/protobuf/compiler/javamicro/javamicro_helpers.cc \
        src/google/protobuf/compiler/javamicro/javamicro_message.cc \
        src/google/protobuf/compiler/javamicro/javamicro_message_field.cc \
        src/google/protobuf/compiler/javamicro/javamicro_primitive_field.cc \
        src/google/protobuf/compiler/python/python_generator.cc \
        src/google/protobuf/io/coded_stream.cc \
        src/google/protobuf/io/gzip_stream.cc \
        src/google/protobuf/io/printer.cc \
        src/google/protobuf/io/tokenizer.cc \
        src/google/protobuf/io/zero_copy_stream.cc \
        src/google/protobuf/io/zero_copy_stream_impl.cc \
        src/google/protobuf/io/zero_copy_stream_impl_lite.cc \
        src/google/protobuf/stubs/common.cc \
        src/google/protobuf/stubs/hash.cc \
        src/google/protobuf/stubs/once.cc \
        src/google/protobuf/stubs/structurally_valid.cc \
        src/google/protobuf/stubs/strutil.cc \
        src/google/protobuf/stubs/substitute.cc

    # C++ full library
    # =======================================================
    #include $(CLEAR_VARS)

    LOCAL_MODULE := libprotobuf
    LOCAL_MODULE_TAGS := optional

    LOCAL_CPP_EXTENSION := .cc

    LOCAL_SRC_FILES := \
        $(CC_LITE_SRC_FILES)                                             \
        src/google/protobuf/stubs/strutil.cc                             \
        src/google/protobuf/stubs/substitute.cc                          \
        src/google/protobuf/stubs/structurally_valid.cc                  \
        src/google/protobuf/descriptor.cc                                \
        src/google/protobuf/descriptor.pb.cc                             \
        src/google/protobuf/descriptor_database.cc                       \
        src/google/protobuf/dynamic_message.cc                           \
        src/google/protobuf/extension_set_heavy.cc                       \
        src/google/protobuf/generated_message_reflection.cc              \
        src/google/protobuf/message.cc                                   \
        src/google/protobuf/reflection_ops.cc                            \
        src/google/protobuf/service.cc                                   \
        src/google/protobuf/text_format.cc                               \
        src/google/protobuf/unknown_field_set.cc                         \
        src/google/protobuf/wire_format.cc                               \
        src/google/protobuf/io/gzip_stream.cc                            \
        src/google/protobuf/io/printer.cc                                \
        src/google/protobuf/io/tokenizer.cc                              \
        src/google/protobuf/io/zero_copy_stream_impl.cc                  \
        src/google/protobuf/compiler/importer.cc                         \
        src/google/protobuf/compiler/parser.cc

    LOCAL_C_INCLUDES := \
        $(LOCAL_PATH)/src

    LOCAL_C_INCLUDES := \
        $(LOCAL_PATH)/android \
        bionic \
        $(LOCAL_PATH)/src \
        $(JNI_H_INCLUDE)

    LOCAL_SHARED_LIBRARIES := \
        libz libcutils libutils
    LOCAL_LDLIBS := -lz
    # stlport conflicts with the host stl library
    ifneq ($(TARGET_SIMULATOR),true)
    LOCAL_C_INCLUDES += external/stlport/stlport
    LOCAL_SHARED_LIBRARIES += libstlport
    endif

    # Define the header files to be copied
    #LOCAL_COPY_HEADERS := \
    #    src/google/protobuf/stubs/once.h \
    #    src/google/protobuf/stubs/common.h \
    #    src/google/protobuf/io/coded_stream.h \
    #    src/google/protobuf/generated_message_util.h \
    #    src/google/protobuf/repeated_field.h \
    #    src/google/protobuf/extension_set.h \
    #    src/google/protobuf/wire_format_lite_inl.h
    #
    #LOCAL_COPY_HEADERS_TO := $(LOCAL_MODULE)

    LOCAL_CFLAGS := -DGOOGLE_PROTOBUF_NO_RTTI

    include $(BUILD_SHARED_LIBRARY)


上面提到的Android.mk工作正常,非常感谢

我想描述一下为android构建protobuf的所有步骤

  • 为android配置源代码。 使用以下脚本(在此处找到:):

    预构建=/Users/user/android-ndk-r7b/toolschains/arm-linux-androideabi-4.4.3

    平台=/Users/user/android-ndk-r7b/platforms/android-3/arch-arm/

    export CC=“/Users/user/android-ndk-r7b/toolschains/arm-linux-androideabi-4.4.3/prebuild/darwin-x86/bin/arm-linux-androideabi-gcc”

    export CFLAGS=“-fPIC-DANDROID-nostdlib”

    导出ANDROID_ROOT=“/Users/user/ANDROID-ndk-r7b”

    export LDFLAGS=“-Wl,-rpath link=$ANDROID_ROOT/platforms/ANDROID-3/arch-arm/usr/lib/-L$ANDROID_ROOT/platforms/ANDROID-3/arch-arm/usr/lib/”

    export CPPFLAGS=“-I$ANDROID\u ROOT/platforms/ANDROID-3/arch arm/usr/include/”

    导出LIBS=“-lc”

    ./configure--host=arm eabi

  • 从上述Android.mk文件中删除以下行:

    src/google/protobuf/stubs/strutil.cc             \
    src/google/protobuf/stubs/substitute.cc                          \
    src/google/protobuf/stubs/structurally_valid.cc                  \
    src/google/protobuf/descriptor.cc                                \
    src/google/protobuf/descriptor.pb.cc                             \
    src/google/protobuf/descriptor_database.cc                       \
    src/google/protobuf/dynamic_message.cc                           \
    src/google/protobuf/extension_set_heavy.cc                       \
    src/google/protobuf/generated_message_reflection.cc              \
    src/google/protobuf/message.cc                                   \
    src/google/protobuf/reflection_ops.cc                            \
    src/google/protobuf/service.cc                                   \
    src/google/protobuf/text_format.cc                               \
    src/google/protobuf/unknown_field_set.cc                         \
    src/google/protobuf/wire_format.cc                               \
    src/google/protobuf/io/gzip_stream.cc                            \
    src/google/protobuf/io/printer.cc                                \
    src/google/protobuf/io/tokenizer.cc                              \
    src/google/protobuf/io/zero_copy_stream_impl.cc                  \
    src/google/protobuf/compiler/importer.cc                         \
    src/google/protobuf/compiler/parser.cc
    
    它是构建lite版本的库所必需的。 不幸的是,lib的完整版本只能在android2.3上运行。 我测试了Android 1.6、2.1、2.3的lite版本

  • 使用ndk build构建库
  • < LI>对于Lite库生成C++文件(使用PotoC编译器),需要包括:选项优化器= = LITEY运行时;在.proto文件中
只是一个建议。首先在Java中构建它,然后如果需要进一步的优化,就转到NDK。至少在爪哇,你会得到基本的协议。你是否尝试过简单地使用谷歌提供的C++实现?这里是我的答案:这有点过时了。对于protobuf 2.4.1,hash.cc不存在,因此应将其删除。如果升级到protobug 2.5.0,则需要添加src/google/protobuf/stubs/stringprintf。cc@gkanwar当我进行您建议的编辑时,lib会编译,但当我尝试调用依赖于protobuf libs的方法时,会出现此错误:由:java.lang.UnsatisfiedLinkError:无法加载库:reloc_library[1306]:37找不到“strof”。。。使用“nm”时,我注意到strof方法已列出,但在其旁边列出了一个“U”。protobuf是否需要一个我没有包含的库来工作?@MichaelNguyen strtof应该住在stl库中。libprotobuf使用stlport;答案中描述的构建文件将其链接为共享库而不是静态库,因此您也应该从Java加载libstlport。你可以在我正在做的一个项目中找到一个有效的例子:你是如何构建它的?我犯了很多错误。我正在试着摆脱标签2.6.1。我得到的错误像
/src/google/protobuf/stubs/hash.h:172:55:error:expected'{'before'好的,我终于构建了它。改用这个:你必须大量修改ANdroid.mk文件才能让它工作。当我删除上面提到的行时,我得到一个编译错误。你确定需要删除这些行吗?你似乎忘了在CPPFLAGS:
export sysroot=$NDK_ROOT/platforms/andr设置sysrootoid-9/arch arm
导出CPPFLAGS=“--sysroot=$sysroot”
/configure options