參考文章:
1.NDK環境配置 http://blog.csdn.net/smfwuxiao/article/details/6587709
2.libcurl的配置修改 http://piggyq.com/pen/compile-libcurl-and-openssl-on-android-using-ndk.htmlhtml
假設已經編譯好了openssl,而且安裝目錄(頭文件和庫的所在的目錄)爲openssl-androidlinux
第一步:配置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。android
第二步:配置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中有定義,
不註釋的話在編譯過程當中會有不少警告(可是能正確編譯經過)c++
第三步:編譯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)curl
#using libcrypto.so
include $(CLEAR_VARS)
LOCAL_MODULE := libcrypto
LOCAL_SRC_FILES :=/home/administrator/openssl-android/libs/armeabi/libcrypto.so
include $(PREBUILT_SHARED_LIBRARY)ide
# 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.hui
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)url
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 := x86TARGET_ARCH := x86