flutter进阶(1)ndk编译protobuf&作为预编译库使用

大家查资料还是得去官网,官网上还是挺全的,不要浪费时间看别人的教程。官网链接在最底下参考链接里

今天要讲的这个过程是一个我大概花了一周时间才解决的问题:在windows10系统当中如何把protobuf库使用C++的写法在Android Studio项目当中进行调用

网上大部分的教程都是关于如何用ndk编译protobuf的,或者只是单独如何将一个普通的c++编译成.so文件链接到Android Studio项目上的(如果这是linux系统就简单地多了...)。

在这里首先有一个坑要注意:ndk编译跟cmake编译是不兼容的

其实android studio可以直接支持protobuf的java编译,在plugin里有protobuf-support下载然后gradle里配置一下就可以了,但是我不会写java,而且这个插件下载要翻墙,所以就没有选择这个方案。

ndk编译protobuf总共分为以下几步:
1.通过cmake_gui生成vs2019所需文件
2.vs2019编译protobuf生成protoc
3.使用ndk编译protobuf生成.so文件
4.自己写一个proto文件,然后调用protobuf包当中的protoc来生成文件

1.通过cmake_gui生成protoc

首先下载cmake和protobuf的源码,下载地址:

cmake:https://cmake.org/download/选binary distribution里面的msi文件,可以直接运行的。

protobuf https://github.com/protocolbuffers/protobuf下载下来解压

安装好cmake之后,打开cmake_gui这个可执行文件,然后把要源码的地址和要输出文件的地址选好(最好就放在protobuf这个层级底下)。

这个时候点configure,它会让你选择哪个编译版本,这个时候依据你选择的编译器进行更改,比如通用的vs,我下载的是2019版本,那就选择vs2019,然后点击确定。

这个时候一般有可能会出现一个问题,因为我们在下载源码的时候是不带test项目的,所以会缺一些文件,但是对本体protobuf没有影响,可以改变产生cmake文件的选项来略过这个问题。

这样就生成好了vs2019编译所需的cmake文件和sln文件了。

2.vs2019编译protobuf

首先,下载vs2019:vs2019(不太清楚需不需要翻墙,选择community社区版下载安装。

按照默认配置安装就可以了,安装好了之后运行,选择打开项目或解决方案,点击到之前生成好的文件夹当中,选择后缀为.sln的文件进行打开(这代表是vs2019的一个项目)。

然后再点击这个按钮就可以进行编译了。

在编译好了之后进到那个文件夹当中,进入Debug就可以看到protoc这个文件了,这个文件我们之后会用到。

3.使用ndk编译protobuf生成.so文件

有些人可能会觉得很奇怪,之前不是编译过了,怎么还要编译。在vs2019里面编译出
来的是dll库,也是动态链接库,在安卓上用不了,所以我们需要使用在android studio上的ndk插件来进行编译生成.so的库。

首先我们要新建一个flutter项目,如果有疑问可以看flutter中文网

我们将protobuf/src/下面的google复制,放在android-app-src-main-jni目录下。

首先更改android-app-build.gradle,在android的层级下加入下面的程序。

    externalNativeBuild {
        ndkBuild {
            // Tells Gradle to put outputs from external native
            // builds in the path specified below.
            path './src/main/jni/Android.mk'
            buildStagingDirectory "./outputs/ndk-build"
        }
    }

这个代表使用原生的编译方式,调用这个Android.mk,调用这个的同时也会调用跟他同一级目录的Application.mk。

接下来编写这个Android.mk和Application.mk,这些文件的详情参考链接里有。
Andorid.mk


LOCAL_PATH := $(call my-dir)
 
include $(CLEAR_VARS)
 
LOCAL_MODULE := protobuf
LOCAL_CFLAGS := -std=c++11 -fexceptions -frtti
LOCAL_MODULE_FILENAME := libprotobuf
LOCAL_SRC_FILES :=  google/protobuf/stubs/bytestream.cc                          \
                     google/protobuf/stubs/bytestream.h                           \
                     google/protobuf/stubs/common.cc                              \
                     google/protobuf/stubs/hash.h                                 \
                     google/protobuf/stubs/int128.cc                              \
                     google/protobuf/stubs/int128.h                               \
                     google/protobuf/io/io_win32.cc                               \
                     google/protobuf/stubs/map_util.h                             \
                     google/protobuf/stubs/mathutil.h                             \
                     google/protobuf/stubs/status.cc                              \
                     google/protobuf/stubs/status.h                               \
                     google/protobuf/stubs/status_macros.h                        \
                     google/protobuf/stubs/statusor.cc                            \
                     google/protobuf/stubs/statusor.h                             \
                     google/protobuf/stubs/stringpiece.cc                         \
                     google/protobuf/stubs/stringpiece.h                          \
                     google/protobuf/stubs/stringprintf.cc                        \
                     google/protobuf/stubs/stringprintf.h                         \
                     google/protobuf/stubs/structurally_valid.cc                  \
                     google/protobuf/stubs/strutil.cc                             \
                     google/protobuf/stubs/time.cc                                \
                     google/protobuf/stubs/time.h                                 \
                     google/protobuf/any_lite.cc                                  \
                     google/protobuf/arena.cc                                     \
                     google/protobuf/extension_set.cc                             \
                     google/protobuf/generated_enum_util.cc                       \
                     google/protobuf/generated_message_util.cc                    \
                     google/protobuf/generated_message_table_driven_lite.h        \
                     google/protobuf/generated_message_table_driven_lite.cc       \
                     google/protobuf/implicit_weak_message.cc                     \
                     google/protobuf/message_lite.cc                              \
                     google/protobuf/parse_context.cc                             \
                     google/protobuf/repeated_field.cc                            \
                     google/protobuf/wire_format_lite.cc                          \
                     google/protobuf/io/coded_stream.cc                           \
                     google/protobuf/io/strtod.cc                                 \
                     google/protobuf/io/zero_copy_stream.cc                       \
                     google/protobuf/io/zero_copy_stream_impl.cc                  \
                     google/protobuf/io/zero_copy_stream_impl_lite.cc             \
                      google/protobuf/any.pb.cc                                    \
                       google/protobuf/api.pb.cc                                    \
                       google/protobuf/any.cc                                       \
                       google/protobuf/descriptor.cc                                \
                       google/protobuf/descriptor_database.cc                       \
                       google/protobuf/descriptor.pb.cc                             \
                       google/protobuf/duration.pb.cc                               \
                       google/protobuf/dynamic_message.cc                           \
                       google/protobuf/empty.pb.cc                                  \
                       google/protobuf/extension_set_heavy.cc                       \
                       google/protobuf/field_mask.pb.cc                             \
                       google/protobuf/generated_message_reflection.cc              \
                       google/protobuf/generated_message_table_driven_lite.h        \
                       google/protobuf/generated_message_table_driven.cc            \
                       google/protobuf/map_field.cc                                 \
                       google/protobuf/message.cc                                   \
                       google/protobuf/reflection_internal.h                        \
                       google/protobuf/reflection_ops.cc                            \
                       google/protobuf/service.cc                                   \
                       google/protobuf/source_context.pb.cc                         \
                       google/protobuf/struct.pb.cc                                 \
                       google/protobuf/stubs/substitute.cc                          \
                       google/protobuf/stubs/substitute.h                           \
                       google/protobuf/text_format.cc                               \
                       google/protobuf/timestamp.pb.cc                              \
                       google/protobuf/type.pb.cc                                   \
                       google/protobuf/unknown_field_set.cc                         \
                       google/protobuf/wire_format.cc                               \
                       google/protobuf/wrappers.pb.cc                               \
                       google/protobuf/io/gzip_stream.cc                            \
                       google/protobuf/io/printer.cc                                \
                       google/protobuf/io/tokenizer.cc                              \
                       google/protobuf/compiler/importer.cc                         \
                       google/protobuf/compiler/parser.cc                           \
                       google/protobuf/util/delimited_message_util.cc               \
                       google/protobuf/util/field_comparator.cc                     \
                       google/protobuf/util/field_mask_util.cc                      \
                       google/protobuf/util/internal/constants.h                    \
                       google/protobuf/util/internal/datapiece.cc                   \
                       google/protobuf/util/internal/datapiece.h                    \
                       google/protobuf/util/internal/default_value_objectwriter.cc  \
                       google/protobuf/util/internal/default_value_objectwriter.h   \
                       google/protobuf/util/internal/error_listener.cc              \
                       google/protobuf/util/internal/error_listener.h               \
                       google/protobuf/util/internal/expecting_objectwriter.h       \
                       google/protobuf/util/internal/field_mask_utility.cc          \
                       google/protobuf/util/internal/field_mask_utility.h           \
                       google/protobuf/util/internal/json_escaping.cc               \
                       google/protobuf/util/internal/json_escaping.h                \
                       google/protobuf/util/internal/json_objectwriter.cc           \
                       google/protobuf/util/internal/json_objectwriter.h            \
                       google/protobuf/util/internal/json_stream_parser.cc          \
                       google/protobuf/util/internal/json_stream_parser.h           \
                       google/protobuf/util/internal/location_tracker.h             \
                       google/protobuf/util/internal/mock_error_listener.h          \
                       google/protobuf/util/internal/object_location_tracker.h      \
                       google/protobuf/util/internal/object_source.h                \
                       google/protobuf/util/internal/object_writer.cc               \
                       google/protobuf/util/internal/object_writer.h                \
                       google/protobuf/util/internal/protostream_objectsource.cc    \
                       google/protobuf/util/internal/protostream_objectsource.h     \
                       google/protobuf/util/internal/protostream_objectwriter.cc    \
                       google/protobuf/util/internal/protostream_objectwriter.h     \
                       google/protobuf/util/internal/proto_writer.cc                \
                       google/protobuf/util/internal/proto_writer.h                 \
                       google/protobuf/util/internal/structured_objectwriter.h      \
                       google/protobuf/util/internal/type_info.cc                   \
                       google/protobuf/util/internal/type_info.h                    \
                       google/protobuf/util/internal/type_info_test_helper.cc       \
                       google/protobuf/util/internal/type_info_test_helper.h        \
                       google/protobuf/util/internal/utility.cc                     \
                       google/protobuf/util/internal/utility.h                      \
                       google/protobuf/util/json_util.cc                            \
                       google/protobuf/util/message_differencer.cc                  \
                       google/protobuf/util/time_util.cc                            \
                       google/protobuf/util/type_resolver_util.cc
 
 
LOCAL_EXPORT_C_INCLUDES :=
LOCAL_EXPORT_LDLIBS :=
 
LOCAL_C_INCLUDES := $(LOCAL_PATH) \
$(LOCAL_PATH)/src
 
LOCAL_LDLIBS := -llog -lz
 
include $(BUILD_SHARED_LIBRARY)
#include $(BUILD_STATIC_LIBRARY)

Application.mk

APP_MODULES      := protobuf
APP_PLATFORM     := android-26
APP_ABI          := arm64-v8a
APP_STL := c++_static
APP_OPTIM        := debug

这里面主要就是编译这个包所需要包含的源文件,我编译的是整个protobuf,所以比较多。

具体要编译的这些文件在protobuf/src/Makefile.am当中,你可以根据自己源码版本进行更改,如果你想编译protobuf-lite,就去找libprotobuf_lite_la_SOURCES的变量定义复制过来就可以了。

然后生成一个AVD的模拟器或者是外接一个android设备,就可以编译了。

编译之后,我们点击停止(不点击停止,build不会刷新),然后在build(注意是根目录下的build)/app/intermediates/ndkbuild点到底就可以找到libprotobuf.so就说明编译成功了。

到这里protobuf的ndk编译就完成了,如果想用其他方式调用这个protobuf.so也是可以直接调用的了。

结语

flutter其实还是一个应用起来有一些难度的大前端框架,它的学习曲线比较陡峭,需要懂的东西也比较多。由于这个框架不是非常的稳定,经常在更新,网上很多可以搜出来的解决方法有可能已经过时了,这个时候就需要通过一些经验来自己解决问题。

在编写一个app的时候,往往需要前后端结合,dart虽然是一个比较通用的语言,但是还是不如C++在后端上的应用广泛,所以如何将写好的C++库链接到flutter的项目上确实是一个比较重要的过程。

参考资料

flutter调用C++ https://blog.csdn.net/guawazi123321/article/details/105099443
这篇文章有一些问题,但是具有借鉴意义,它生成出来的.so文件不能被dart的dynamicLibrary调用,这个我们后面会讲到。

深入解读Cmake https://www.jianshu.com/p/089b458ab8d5 我们其实没有用到cmake,有兴趣的可以自己看一下。

Android studio ndk编译protobuf生成C++的.so链接库https://blog.csdn.net/niuben127/article/details/78738671

Android 官网关于android当中安装ndk的介绍 https://developer.android.com/studio/projects/install-ndk

Android官网关于使用ndk及预编译库配置的介绍&Android.mk和Application.mk详解
https://developer.android.google.cn/ndk/guides/android_mk

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,440评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,814评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,427评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,710评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,625评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,014评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,511评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,162评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,311评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,262评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,278评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,989评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,583评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,664评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,904评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,274评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,856评论 2 339