protobuf使用NDK編譯Android的靜態庫(工作記錄)


1.protobuf 編譯過程

  • 前提: 確保自己電腦上已經安裝了cygwin + ndk, 並且NDK能夠編譯hello-jni成功

1.1 把protobuf 壓縮包解壓到protobuf文件夾下

1.2 在protobuf文件夾下新建jni文件下

1.3 把protobuf文件夾下的src文件夾復制一份到jni文件夾下

1.4 在jni文件夾下新建Android.mk文件並且在里面添加如下內容

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := protobuf

LOCAL_MODULE_FILENAME := libprotobuf

LOCAL_SRC_FILES := src/google/protobuf/io/coded_stream.cc \
src/google/protobuf/stubs/common.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.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/io/gzip_stream.cc \
src/google/protobuf/compiler/importer.cc \
src/google/protobuf/message.cc \
src/google/protobuf/message_lite.cc \
src/google/protobuf/stubs/once.cc \
src/google/protobuf/compiler/parser.cc \
src/google/protobuf/io/printer.cc \
src/google/protobuf/reflection_ops.cc \
src/google/protobuf/repeated_field.cc \
src/google/protobuf/service.cc \
src/google/protobuf/stubs/structurally_valid.cc \
src/google/protobuf/stubs/strutil.cc \
src/google/protobuf/stubs/substitute.cc \
src/google/protobuf/text_format.cc \
src/google/protobuf/io/tokenizer.cc \
src/google/protobuf/unknown_field_set.cc \
src/google/protobuf/wire_format.cc \
src/google/protobuf/wire_format_lite.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/stringprintf.cc
  
LOCAL_EXPORT_C_INCLUDES :=  
LOCAL_EXPORT_LDLIBS :=  
  
LOCAL_C_INCLUDES := $(LOCAL_PATH) \
$(LOCAL_PATH)/src

LOCAL_LDLIBS := -llog -lz

include $(BUILD_STATIC_LIBRARY)

1.5 在jni文件夾下新建Application.mk文件並且在里面添加如下內容

APP_STL := gnustl_static  
APP_CPPFLAGS := -frtti 
APP_ABI := armeabi armeabi-v7a x86
APP_MODULES := protobuf
  • 這里我弄了很久,原因是在cygwin中用ndk-build一直沒有反應,但是hello-jni又是有效的,后來在stackoverflow中看到說如果使用NDK編譯靜態
    庫要在Application. :mk中添加 APP_MODULES := module_name否則無法編譯,動態庫可以不用這個,簡單的甚至不用Application.mk這個文件

1.6 使用cygwin 進入到protobuf文件夾,然后輸入 ./configure

1.7 把上一步生成的config.h文件拷貝到jni文件下

1.8 在protobuf文件夾下運行ndk-build命令生成靜態庫文件,並且把靜態庫文件拷貝到我們項目下的libs文件夾下,比如反正proto文件夾下

proto 文件夾結構:
根目錄:proto
一級子目錄和文件:include,libs文件夾已經Android.mk文件

1.9 在protobuf文件夾下運行make && make install

1.10 實際上在1.9中也會生成libprotobuf.a文件,但是我主要是用來提取頭文件,因為make install會把生成的lib文件和需要使用的頭文件分別安裝到cygwin的/usr/local/libusr/local/include其中include文件夾下的文件是我們需要的,把include文件夾放到1.8的proto文件夾下

1.11 在proto文件夾下創建一個Android.mk文件,內容如下

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := cocos_protobuf_static
LOCAL_MODULE_FILENAME := libprotobuf
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libprotobuf.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)

2.protobuf 使用過程

2.1 在自己工程的Android.mk文件中添加以下代碼

LOCAL_WHOLE_STATIC_LIBRARIES += cocos_protobuf_static
$(call import-add-path,$(LOCAL_PATH)/../../)
$(call import-module,proto)
  • cocos_protobuf_static這個是1.11的LOCAL_MODULE
  • $(LOCAL_PATH)/../../這個路徑是1.8的proto文件夾上一層路徑
  • proto就是1.8的proto文件夾


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM