大家都知道,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路径及编译工具链路径等变量
#!/bin/bash ANDROID_HOME_CURL=../../ && \ NDK_HOME_CURL=../../ndk && \ PATH="$ANDROID_HOME_CURL/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin:$PATH" \ ./configure --host=arm-linux CC=arm-eabi-gcc --with-random=/dev/urandom \ CPPFLAGS="-I$NDK_HOME_CURL/platforms/android-8/arch-arm/usr/include \ -I $ANDROID_HOME_CURL/external/curl/include/ \ -I $ANDROID_HOME_CURL/external/curl/3rd/include \ -I $ANDROID_HOME_CURL/external/curl \ -I $ANDROID_HOME_CURL/out/target/product/generic/obj/STATIC_LIBRARIES/libcurl_intermediates \ -I $ANDROID_HOME_CURL/dalvik/libnativehelper/include/nativehelper \ -I $ANDROID_HOME_CURL/system/core/include \ -I $ANDROID_HOME_CURL/hardware/libhardware/include \ -I $ANDROID_HOME_CURL/hardware/libhardware_legacy/include \ -I $ANDROID_HOME_CURL/hardware/ril/include \ -I $ANDROID_HOME_CURL/dalvik/libnativehelper/include \ -I $ANDROID_HOME_CURL/frameworks/base/include \ -I $ANDROID_HOME_CURL/frameworks/base/opengl/include \ -I $ANDROID_HOME_CURL/frameworks/base/native/include \ -I $ANDROID_HOME_CURL/external/skia/include \ -I $ANDROID_HOME_CURL/out/target/product/generic/obj/include \ -I $ANDROID_HOME_CURL/bionic/libc/arch-arm/include \ -I $ANDROID_HOME_CURL/bionic/libc/include \ -I $ANDROID_HOME_CURL/bionic/libstdc++/include \ -I $ANDROID_HOME_CURL/bionic/libc/kernel/common \ -I $ANDROID_HOME_CURL/bionic/libc/kernel/arch-arm \ -I $ANDROID_HOME_CURL/bionic/libm/include \ -I $ANDROID_HOME_CURL/bionic/libm/include/arch/arm \ -I $ANDROID_HOME_CURL/bionic/libthread_db/include \ -include $ANDROID_HOME_CURL/system/core/include/arch/linux-arm/AndroidConfig.h \ -I $ANDROID_HOME_CURL/system/core/include/arch/linux-arm/ \ -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -DANDROID -DNDEBUG -DNDEBUG -DHAVE_CONFIG_H" \ CFLAGS="-fno-exceptions -Wno-multichar -msoft-float -fpic -ffunction-sections \ -funwind-tables -fstack-protector -Wa,--noexecstack -Werror=format-security \ -fno-short-enums -march=armv5te -mtune=xscale -Wno-psabi -mthumb-interwork \ -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith \ -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point \ -g -Wstrict-aliasing=2 -finline-functions -fno-inline-functions-called-once \ -fgcse-after-reload -frerun-cse-after-loop -frename-registers -UDEBUG \ -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 \ -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" \ LIBS="-nostdlib -Bdynamic -Wl,-T,$ANDROID_HOME_CURL/build/core/armelf.x \ -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc \ -L$ANDROID_HOME_CURL/out/target/product/generic/obj/lib -Wl,-z,noexecstack \ -Wl,-rpath-link=$ANDROID_HOME_CURL/out/target/product/generic/obj/lib \ -lc -llog -lcutils -lstdc++ \ -Wl,--no-undefined $ANDROID_HOME_CURL/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/lib/gcc/arm-eabi/4.4.0/libgcc.a \ $ANDROID_HOME_CURL/out/target/product/generic/obj/lib/crtend_android.o \ -lm $ANDROID_HOME_CURL/out/target/product/generic/obj/lib/crtbegin_dynamic.o \ -L$ANDROID_HOME_CURL/external/curl/3rd/libs"
4、编写android.mk编译脚本
增加编译生成libcurl.so动态库
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 library include $(CLEAR_VARS) include $(LOCAL_PATH)/lib/Makefile.inc CURL_HEADERS := \ curlbuild.h \ curl.h \ curlrules.h \ curlver.h \ easy.h \ mprintf.h \ multi.h \ stdcheaders.h \ typecheck-gcc.h LOCAL_SRC_FILES := $(addprefix lib/,$(CSOURCES)) LOCAL_C_INCLUDES += $(LOCAL_PATH)/include/ LOCAL_CFLAGS += $(common_CFLAGS) LOCAL_COPY_HEADERS_TO := libcurl/curl LOCAL_COPY_HEADERS := $(addprefix include/curl/,$(CURL_HEADERS)) LOCAL_PRELINK_MODULE := false LOCAL_MODULE:= libcurl LOCAL_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.inc CURL_HEADERS := \ curlbuild.h \ curl.h \ curlrules.h \ curlver.h \ easy.h \ mprintf.h \ multi.h \ stdcheaders.h \ typecheck-gcc.h LOCAL_SRC_FILES := $(addprefix lib/,$(CSOURCES)) LOCAL_C_INCLUDES += $(LOCAL_PATH)/include/ LOCAL_CFLAGS += $(common_CFLAGS) LOCAL_COPY_HEADERS_TO := libcurl/curl LOCAL_COPY_HEADERS := $(addprefix include/curl/,$(CURL_HEADERS)) LOCAL_PRELINK_MODULE := false LOCAL_MODULE:= libcurl LOCAL_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 binary include $(CLEAR_VARS) include $(LOCAL_PATH)/src/Makefile.inc LOCAL_SRC_FILES := $(addprefix src/,$(CURL_CFILES)) LOCAL_MODULE := curl LOCAL_MODULE_TAGS := optional LOCAL_SHARED_LIBRARIES := libcurl LOCAL_SYSTEM_SHARED_LIBRARIES := libc LOCAL_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 library LOCAL_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 文件并重新执行即可,暂时没碰到其它的问题。