首页 > 代码库 > Ubuntu+NDK编译支持HTTPS的libcurl

Ubuntu+NDK编译支持HTTPS的libcurl

参考文章:
1.NDK环境配置 http://blog.csdn.net/smfwuxiao/article/details/6587709
2.libcurl的配置修改 http://piggyq.com/pen/compile-libcurl-and-openssl-on-android-using-ndk.html

假设已经编译好了openssl,并且安装目录(头文件和库的所在的目录)为openssl-android

第一步:配置NDK环境(包括系统环境SYSROOT和编译器CC)
android ndk 提供脚本,允许自己定制一套工具链。例如:
$NDK/build/tools/make-standalone-toolchain.sh --platform=android-5 --install-dir=/tmp/my-android-toolchain [ --arch=x86 ]
将会在/tmp/my-android-toolchain 中创建 sysroot 环境和 工具链。--arch 选项选择目标程序的指令架构,默认是为 arm。
如果不加 --install-dir 选项,则会创建 /tmp/ndk/<toolchain-name>.tar.bz2。
(执行 make-standalone-toolchain.sh --help 查看帮助。)
运行之后,这样使用:
$ export PATH=/tmp/my-android-toolchain/bin:$PATH
$ export CC=arm-linux-androideabi-gcc
$ export CXX=arm-linux-androideabi-g++
$ export CXXFLAGS="-lstdc++"
执行完以上设置环境变量的命令之后,就可以直接编译了(例如,执行 ./configure 然后 make 得到的就是 arm 程序了)。
不用再设定 sysroot, CC 了。而且,可以使用 STL,异常,RTTI。

第二步:配置libcurl
进入curl根目录,运行./configure --host=arm-linux-androideabi --with-ssl=/openssl-android
为了保证生成的配置文件正确,需要手动确认一下,打开lib/curl_config.h,
打开宏定义HAVE_LIBSSL、 HAVE_OPENSSL_CRYPTO_H、HAVE_OPENSSL_ERR_H、HAVE_OPENSSL_PEM_H、 HAVE_OPENSSL_PKCS12_H、
HAVE_OPENSSL_RSA_H、HAVE_OPENSSL_SSL_H、 HAVE_OPENSSL_X509_H、USE_OPENSSL、USE_SSLEAY
(但是注意android没有编译openssl中的 engine,因此不要打开宏定义HAVE_OPENSSL_ENGINE_H),
同时注释掉宏定义HAVE_MALLOC_H和HAVE_IOCTL, 这两个宏定义在Android_config.h中有定义,
不注释的话在编译过程中会有很多警告(但是能正确编译通过)

第三步:编译Libcurl
修改Android.mk,删除我们不需要的curl模块,在LOCAL_C_INCLUDES变量的值中增加openssl-android/include和openssl-android/crypto.
再按照以下几种方式修改Android.mk.修改完后,将所在目录package/Android改为package/jni,使用终端进入package,然后执行ndk-build即可。
1.编译ARM版的(依赖openssl的共享库)
先在libcurl前增加动态库模块,使libssl.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

#using libssl.so
include $(CLEAR_VARS)
LOCAL_MODULE := libssl
LOCAL_SRC_FILES := /home/administrator/openssl-android/libs/armeabi/libssl.so
include $(PREBUILT_SHARED_LIBRARY)

#using libcrypto.so
include $(CLEAR_VARS)
LOCAL_MODULE := libcrypto
LOCAL_SRC_FILES :=/home/administrator/openssl-android/libs/armeabi/libcrypto.so
include $(PREBUILT_SHARED_LIBRARY)

# 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_PATH)/lib/\
$(LOCAL_PATH)/../openssl-android/include/\
$(LOCAL_PATH)/../openssl-android/crypto/
LOCAL_SYSTEM_SHARED_LIBRARIES+= libssl libcrypto libz
LOCAL_CFLAGS += $(common_CFLAGS)

LOCAL_COPY_HEADERS_TO := libcurl/curl
LOCAL_COPY_HEADERS := $(addprefix include/curl/,$(CURL_HEADERS))

LOCAL_MODULE:= libcurl
LOCAL_MODULE_TAGS := optional

ALL_PREBUILT += $(LOCAL_PATH)/NOTICE
$(LOCAL_PATH)/NOTICE: $(LOCAL_PATH)/COPYING | $(ACP)
$(copy-file-to-target)

include $(BUILD_STATIC_LIBRARY)

2.编译ARM版的(依赖Openssl的静态库)
Android.mk修改与3类似,稍微改一下静态库路径即可


3.编译x86版的(依赖openssl静态库)

修改Android.mk
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

include $(CLEAR_VARS)
LOCAL_MODULE := libssl
LOCAL_SRC_FILES := /home/administrator/openssl-android/obj/local/x86/libssl.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libcrypto
LOCAL_SRC_FILES :=/home/administrator/openssl-android/obj/local/x86/libcrypto.a
include $(PREBUILT_STATIC_LIBRARY)
#########################
# 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_PATH)/lib/\
$(LOCAL_PATH)/../openssl-android/include/\
$(LOCAL_PATH)/../openssl-android/crypto/
LOCAL_STATIC_LIBRARIES += libssl libcrypto
LOCAL_SYSTEM_SHARED_LIBRARIES += libz
LOCAL_CFLAGS += $(common_CFLAGS)

LOCAL_COPY_HEADERS_TO := libcurl/curl
LOCAL_COPY_HEADERS := $(addprefix include/curl/,$(CURL_HEADERS))

LOCAL_MODULE:= libcurl
LOCAL_MODULE_TAGS := optional

ALL_PREBUILT += $(LOCAL_PATH)/NOTICE
$(LOCAL_PATH)/NOTICE: $(LOCAL_PATH)/COPYING | $(ACP)
$(copy-file-to-target)

include $(BUILD_STATIC_LIBRARY)

在Android.mk的同目录中增加Application.mk,并增加以下内容:
APP_ABI := x86
TARGET_ARCH := x86

 

Ubuntu+NDK编译支持HTTPS的libcurl