android -- libcurl移植、编译与测试
大家都知道,curl是利用URL语法在命令行方式下工作的文件传输工具
支持很多协议:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP。因此将其移植到android平台使用。
目前 http://curl.haxx.se/ 网站上最新的版本:curl and libcurl 7.29.0
网上也有人移植过curl,其实挺简单的,基本的编译脚本都的,我使用的是curl-7.28.1版本,下面简要说明一下如何编译及使用吧。
1、移植Curl工具到Android 2.3 环境步骤
1、直接到网站上下载 curl 源码
2、利用tar在android编译环境下,一般放在 external 目录下
3、由于Curl依赖一些一些头文件及定义需要先执行配置文件先,在external/curl 下面编译curl.sh
需要 cd external/curl/.curl.sh进行执行,或者放在android根目录下的*.sh脚本中执行:source external/curl/curl.sh
其中修改了相应的CURL路径及编译工具链路径等变量
LOCAL_PATH:= $(call my-dir)common_CFLAGS := -Wpointer-arith -Wwrite-strings -Wunused -Winline -Wnested-externs -Wmissing-declarations -Wmissing-prototypes -Wno-long-long -Wfloat-equal -Wno-multichar -Wsign-compare -Wno-format-nonliteral -Wendif-labels -Wstrict-prototypes -Wdeclaration-after-statement -Wno-system-headers -DHAVE_CONFIG_H########################## Build the libcurl libraryinclude $(CLEAR_VARS)include $(LOCAL_PATH)/lib/Makefile.incCURL_HEADERS := \curlbuild.h \curl.h \curlrules.h \curlver.h \easy.h \mprintf.h \multi.h \stdcheaders.h \typecheck-gcc.hLOCAL_SRC_FILES := $(addprefix lib/,$(CSOURCES))LOCAL_C_INCLUDES += $(LOCAL_PATH)/include/LOCAL_CFLAGS += $(common_CFLAGS)LOCAL_COPY_HEADERS_TO := libcurl/curlLOCAL_COPY_HEADERS := $(addprefix include/curl/,$(CURL_HEADERS))LOCAL_PRELINK_MODULE := falseLOCAL_MODULE:= libcurlLOCAL_MODULE_TAGS := optional# Copy the licence to a place where Android will find it.# Actually, this doesn't quite work because the build system searches# for NOTICE files before it gets to this point, so it will only be seen# on subsequent builds.ALL_PREBUILT += $(LOCAL_PATH)/NOTICE$(LOCAL_PATH)/NOTICE: $(LOCAL_PATH)/COPYING | $(ACP)$(copy-file-to-target)include $(BUILD_STATIC_LIBRARY)include $(CLEAR_VARS)include $(LOCAL_PATH)/lib/Makefile.incCURL_HEADERS := \curlbuild.h \curl.h \curlrules.h \curlver.h \easy.h \mprintf.h \multi.h \stdcheaders.h \typecheck-gcc.hLOCAL_SRC_FILES := $(addprefix lib/,$(CSOURCES))LOCAL_C_INCLUDES += $(LOCAL_PATH)/include/LOCAL_CFLAGS += $(common_CFLAGS)LOCAL_COPY_HEADERS_TO := libcurl/curlLOCAL_COPY_HEADERS := $(addprefix include/curl/,$(CURL_HEADERS))LOCAL_PRELINK_MODULE := falseLOCAL_MODULE:= libcurlLOCAL_MODULE_TAGS := optional# Copy the licence to a place where Android will find it.# Actually, this doesn't quite work because the build system searches# for NOTICE files before it gets to this point, so it will only be seen# on subsequent builds.ALL_PREBUILT += $(LOCAL_PATH)/NOTICE$(LOCAL_PATH)/NOTICE: $(LOCAL_PATH)/COPYING | $(ACP)$(copy-file-to-target)include $(BUILD_SHARED_LIBRARY)########################## Build the curl binaryinclude $(CLEAR_VARS)include $(LOCAL_PATH)/src/Makefile.incLOCAL_SRC_FILES := $(addprefix src/,$(CURL_CFILES))LOCAL_MODULE := curlLOCAL_MODULE_TAGS := optionalLOCAL_SHARED_LIBRARIES := libcurlLOCAL_SYSTEM_SHARED_LIBRARIES := libcLOCAL_C_INCLUDES += $(LOCAL_PATH)/include $(LOCAL_PATH)/lib# This may also need to include $(CURLX_ONES) in order to correctly link# if libcurl is changed to be built as a dynamic libraryLOCAL_CFLAGS += $(common_CFLAGS)include $(BUILD_EXECUTABLE)
编译过程如果没有任何错误的话将会在/system/lib下生成 libcurl.so 及 /system/bin下面 curl 可执行文件
2、测试接口功能
#include "curl/curl.h"
#include <stdio.h>;
int main() {
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.baidu.com/");
res = curl_easy_perform(curl);
if (0!=res) {
printf("curl error: %d\n", res);
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
编译一个相应的android.mk生成可执行文件:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += \
$(TOP)/external/curl/include/ \
LOCAL_SRC_FILES:= curl_test.cpp
LOCAL_SHARED_LIBRARIES := libcurl
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := curl_test
include $(BUILD_EXECUTABLE)
即可进行测试的,输出的内容就是打开 http://www.baidu.com/ 页面的源代码
3、编译问题
请根据具体android版本及路径修改 curl.sh 文件并重新执行即可,暂时没碰到其它的问题。
