2011-08-22 142 views
13

我想构建一个本地版本的Google协议缓冲区库。
我该怎么做?如何通过Android NDK构建协议缓冲区

+0

只是一个建议。首先使用Java构建它,然后如果您需要进一步优化移动到NDK。至少在Java中,您将获得基本协议。 – arunkumar

+0

您是否试过简单地使用google提供的C++实现? –

+0

这里是我的回答:https://stackoverflow.com/a/44773137/529442 –

回答

7

我用这个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) 

+0

这是略过时。对于protobuf 2.4.1,hash.cc不存在,所以它应该被删除。如果上升到protobug 2.5.0,同样的交易加上你需要添加src/google/protobuf/stubs/stringprintf.cc – gkanwar

+1

@gkanwar当我编辑你建议的时候,lib编译但是当我尝试调用我的方法,这取决于protobuf库: 引起:java.lang。UnsatisfiedLinkError:无法加载库:reloc_library [1306]:37找不到'strtof'... 使用“nm”我注意到strtof方法已列出,但其旁边列出了一个“U”。有没有我不包括的图书馆,这是protobuf工作所必需的? –

+0

@MichaelNguyen strtof应该住在stl库中。 libprotobuf使用stlport;答案中描述的构建文件将其链接为共享库而非静态文件,因此您应该从Java加载libstlport。你可以找到一个适用于我在这里工作的项目的例子:https://github.com/IDKJava/the-elements/blob/master/TheElements-dev/src/com/idkjava/thelements/MainActivity.java #L605 – gkanwar

4

Android.mk上述工作得很好,非常感谢。

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

  • 配置android的源代码。 使用下面的脚本(有在这里:http://habrahabr.ru/post/119693/):

    预建= /用户/用户/ Android的NDK,经R 7b /工具链/ ARM-Linux的androideabi-4.4.3

    PLATFORM = /用户/ user/android-ndk-r7b/platforms/android-3/arch-arm/

    export CC =“/ Users/user/android-ndk-r7b/toolchains/arm-linux-androideabi- 4.4.3/prebuilt /达尔文86/bin中/ ARM-Linux的androideabi-GCC”

    出口CFLAGS = “ - 子卡-DANDROID -nostdlib”

    export ANDROID_ROOT =“/ Users/user/android-ndk-r7b”

    export LDFLAGS =“ - Wl,-rpath-link = $ ANDROID_ROOT/platforms/android-3/arch-arm/usr/lib/-L $ ANDROID_ROOT /平台/ Android的3 /弓臂/ usr/lib中/”

    出口CPPFLAGS = “ - I $ ANDROID_ROOT /平台/ Android的3 /弓臂/ usr/include目录/”

    出口LIBS = “ - LC”

    的./configure主机=臂-EABI

  • 删除以下行FR om上面提到的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 
    

    这是需要建立精简版的库。 不幸的是,完整版本的lib仅适用于Android 2.3。 我测试了Android 1.6,1.1,2.3的精简版。

  • 使用ndk-build构建库。
  • 用于为需要包括的库文件库生成C++文件(使用protoc编译器):option optimize_for = LITE_RUNTIME;在.proto文件中。
+0

当我删除提到的线我得到一个编译错误。你确定需要删除这些行吗? –

+0

你似乎已经忘了在CPPFLAGS设置SYSROOT: '出口SYSROOT = $ NDK_ROOT /平台/ Android的9 /拱arm' '出口CPPFLAGS = “ - SYSROOT = $ SYSROOT”'' 的./configure options' –