2011-08-22 140 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' –