前面发了一片windows下编译protobuf的文章

后来把生成的.a文件加到android工程后发现不可用

所以只好自己使用NDK编译protobuf了(虽然这样,生成的Inclule的头文件还是很有用的,可以直接用到jni的代码中)


Android NDK编译protobuf


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


好了,现在开始吧

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

二、下载protobuf代码(http://code.google.com/p/protobuf/downloads/list 

三、建立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即可


更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. 一款常用的 Squid 日志分析工具
  3. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  4. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  5. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  6. android apk反编译到java源码的实现方法
  7. Android(安卓)xml文件中ID重名问题
  8. 创建应用快捷方式到桌面
  9. Android(安卓)Studio开发(一)模拟微信页面

随机推荐

  1. 常用 RecyclerView LayoutManager ItemDe
  2. android:ListView:convertView.setTag()来
  3. Android文字跑马灯
  4. 解析xml数据
  5. Android中泛型使用实例
  6. ScrollView和EditText 滑动冲突的解决办
  7. listView点击事件
  8. android 端生成随机验证码 实现
  9. android下截屏
  10. Android(安卓)EditText 银行卡四位空一格