您的位置:首页 > 移动开发 > Android开发

protobuf使用NDK编译Android的静态库(工作记录)

2015-01-10 23:50 465 查看

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/lib
usr/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文件夹

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: