Android NDK下编译google protocol buffer
发布日期:2021-06-30 16:01:22 浏览次数:2 分类:技术文章

本文共 3112 字,大约阅读时间需要 10 分钟。

Android NDK编译protobuf

其实用NDK编译最大的难点就是make文件,把这个搞定就基本搞定了

好了,现在开始吧

一、安装java环境和NDK环境(不多说,自己准备)

二、下载protobuf代码( 

三、建立java的android工程,在根目录下简历jni目录,把protobuf代码解压到jni目录下

如图:

四、写application.mk文件,这个有个问题,protobuf需要使用gnustl_static,我替你写好了,直接复制过去吧,平台不如不一样,修改一下

APP_PLATFORM := android-9

#APP_STL := stlport_static 
APP_STL := gnustl_static

五、开始写android.mk文件

这个是个大难题了,打开文件结构一看,各种文件都有,也不知道要包含哪些,而且有些文件是编译不过的(可能是google的测试代码),所以这个就成了最大问题了

没关系,在各种痛苦的时候我突然想到了一个办法,并且最后成功的编译出来了

我想到了在windows下,用vs2008打开protobuf的代码时,下面有好几个工程,而对我们有用的其实只有一个,就是libprotobuf

所以,打开这个工程,看看里面都有什么.c和.cc或.cpp文件,把所有文件写进make文件去就好了,然后把头文件目录加进去,OK,打工告成

为了方便大家,我把我的make文件提出来给大家吧,我是把protobuf编译给其他程序使用,所以只生成了.a文件,用来做动态连接,需要生成.so的人需要稍微修改一下

android.mk:

LOCAL_PATH := $(call my-dir)

################## Build ProtoBuf ################
include $(CLEAR_VARS)
LOCAL_ARM_MODE := arm
LOCAL_CPPFLAGS += -frtti
LOCAL_MODULE_CLASS := STATIC_LIBRARIES
LOCAL_MODULE := libProtobuf
LOCAL_MODULE_TAGS := optional
LOCAL_CPP_EXTENSION := .cc
LOCAL_SRC_FILES := \
protobuf-2.4.1/src/google/protobuf/io/coded_stream.cc                \
protobuf-2.4.1/src/google/protobuf/stubs/common.cc                   \
protobuf-2.4.1/src/google/protobuf/descriptor.cc                     \
protobuf-2.4.1/src/google/protobuf/descriptor.pb.cc                  \
protobuf-2.4.1/src/google/protobuf/descriptor_database.cc            \
protobuf-2.4.1/src/google/protobuf/dynamic_message.cc                \
protobuf-2.4.1/src/google/protobuf/extension_set.cc                  \
protobuf-2.4.1/src/google/protobuf/extension_set_heavy.cc            \
protobuf-2.4.1/src/google/protobuf/generated_message_reflection.cc   \
protobuf-2.4.1/src/google/protobuf/generated_message_util.cc         \
protobuf-2.4.1/src/google/protobuf/io/gzip_stream.cc                 \
protobuf-2.4.1/src/google/protobuf/compiler/importer.cc              \
protobuf-2.4.1/src/google/protobuf/message.cc                        \
protobuf-2.4.1/src/google/protobuf/message_lite.cc                   \
protobuf-2.4.1/src/google/protobuf/stubs/once.cc                     \
protobuf-2.4.1/src/google/protobuf/compiler/parser.cc                \
protobuf-2.4.1/src/google/protobuf/io/printer.cc                     \
protobuf-2.4.1/src/google/protobuf/reflection_ops.cc                 \
protobuf-2.4.1/src/google/protobuf/repeated_field.cc                 \
protobuf-2.4.1/src/google/protobuf/service.cc                        \
protobuf-2.4.1/src/google/protobuf/stubs/structurally_valid.cc       \
protobuf-2.4.1/src/google/protobuf/stubs/strutil.cc                  \
protobuf-2.4.1/src/google/protobuf/stubs/substitute.cc               \
protobuf-2.4.1/src/google/protobuf/text_format.cc                    \
protobuf-2.4.1/src/google/protobuf/io/tokenizer.cc                   \
protobuf-2.4.1/src/google/protobuf/unknown_field_set.cc              \
protobuf-2.4.1/src/google/protobuf/wire_format.cc                    \
protobuf-2.4.1/src/google/protobuf/wire_format_lite.cc               \
protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream.cc            \
protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl.cc       \
protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl_lite.cc  \
LOCAL_C_INCLUDES += . \
$(LOCAL_PATH)/protobuf-2.4.1/src \
$(LOCAL_PATH)/protobuf-2.4.1 \
#$(LOCAL_PATH)/protobuf-2.4.1/vsprojects \
LOCAL_LDLIBS := -llog -lgcc -pthread \
      C:/android-ndk-r6/sources/cxx-stl/gnu-libstdc++/libs/armeabi/libstdc++.a \
ifndef NDK_ROOT
include external/stlport/libstlport.mk
endif
include $(BUILD_STATIC_LIBRARY)

六、cygwin到android工程目录,执行ndk-build即可

转载地址:https://kaitiren.blog.csdn.net/article/details/9000781 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:IOS-应用之间调用
下一篇:cocos2dx android.mk文件详解

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月13日 17时11分25秒