因为有需求需要编译libcurl库在android工程中使用,所以首先就需要自己编译一个libcurl.a或者libcurl.so了,因为它用的很广泛嘛;
另外如果一个curl库要支持ssl,必须要在编译的时候和ssl组合编译,比如openssl;
了解了这些以后,就可以进行编译工作了;
环境:ubuntu12.04 64
首先下载NDK,android-ndk-r10c-linux-x86_64.bin
http://developer.android.com/tools/sdk/ndk/index.html
然后下载curl源码;在http://curl.haxx.se/download.html可以下载最新release的curl版本源码,当然https://github.com/bagder/curl这里更新;
再下载openssl源码,https://github.com/guardianproject/openssl-android
用到的工具代码准备好之后就是编译了;
1、设置编译环境变量
export LDFLAGS="-L/data/NDK/android-ndk-r10c/platforms/android-21/arch-arm/usr/lib"
export CPPFLAGS="-I/data/NDK/android-ndk-r10c/platforms/android-21/arch-arm/usr/include"
LDFLAGS是告诉编译器从哪里寻找需要的库文件;
CPPFLAGS可选的编译器选项,在编译 C/C++ 代码文件的时候使用。这可能是有用的,指定一个附加的包含路径(相对于NDK的顶层目录),宏定义,或者编译选项。
从NDK导出编译工具,然后将其添加环境变量;
在NDK根目录下执行:
./build/tools/make-standalone-toolchain.sh
会生成一个arm-linux-androideabi-4.6压缩文件,解压到自定义的目录下,然后将其添加进环境变量;
export PATH=$PATH:/opt/arm-linux-androideabi-4.6/bin”
2、进入到curl源码目录下,执行
./configure --host=arm-linux-androideabi \
--without-ssl \
--disable-ftp \
--disable-gopher \
--disable-file \
--disable-imap \
--disable-ldap \
--disable-ldaps \
--disable-pop3 \
--disable-proxy \
--disable-rtsp \
--disable-smtp \
--disable-telnet \
--disable-tftp \
--without-gnutls \
--without-libidn \
--without-librtmp \
--disable-dict
最后会输出类似如下的内容
configure: Configured to build curl/libcurl:
curl version: 7.39.0
Host setup: arm-unknown-linux-androideabi
Install prefix: /usr/local
Compiler: arm-linux-androideabi-gcc
SSL support: no (--with-{ssl,gnutls,nss,polarssl,cyassl,axtls,winssl,darwinssl} )
SSH support: no (--with-libssh2)
zlib support: enabled
GSS-API support: no (--with-gssapi)
TLS-SRP support: no (--enable-tls-srp)
resolver: default (--enable-ares / --enable-threaded-resolver)
ipv6 support: no (--enable-ipv6)
IDN support: no (--with-{libidn,winidn})
Build libcurl: Shared=yes, Static=yes
Built-in manual: enabled
--libcurl option: enabled (--disable-libcurl-option)
Verbose errors: enabled (--disable-verbose)
SSPI support: no (--enable-sspi)
ca cert bundle: no
ca cert path: no
LDAP support: no (--enable-ldap / --with-ldap-lib / --with-lber-lib)
LDAPS support: no (--enable-ldaps)
RTSP support: no (--enable-rtsp)
RTMP support: no (--with-librtmp)
metalink support: no (--with-libmetalink)
HTTP2 support: disabled (--with-nghttp2)
Protocols: HTTP
SONAME bump: yes - WARNING: this library will be built with the SONAME
number bumped due to (a detected) ABI breakage.
See lib/README.curl_off_t for details on this.
在这里要注意:Compiler: arm-linux-androideabi-gcc,如果是系统默认gcc,则需要指定使用ndk中的arm-gcc:
SYSROOT=$ANDROIDNDK/platforms/android-21/arch-arm
export CC="/opt/arm-linux-androideabi-4.6/bin/arm-linux-androideabi-gcc-4.6 --sysroot=$SYSROOT"
再执行
make
就会生成libcurl的库文件了,在lib/.libs下;
这个时候curl的库是不支持进行https请求的;通过下面一句代码可以查看你的库是否支持https;
curl_easy_setopt(easy_handle, CURLOPT_USE_SSL, CURLUSESSL_ALL);
如要查看curl的版本信息,代码如下;
curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
LOGE("data version= %s, ssl_version = %s", data->version, data->ssl_version);
首先进入到刚刚下载的openssl-android工程目录下,执行
ndk-build
命令即可,会生成libssl.so和libcrypto.so两个库文件;
再把两个文件放入到android-ndk-r10c\platforms\android-21\arch-arm\usr\lib目录下;这里的android-21对应上面指定的LDFLAGS;
然后把openssl-android/include/openssl目录拷贝到android-ndk-r10c/platforms/android-21/arch-arm/usr/include目录;
这个时候执行
./configure --host=arm-linux-androideabi \
--with-ssl \
--disable-ftp \
--disable-gopher \
--disable-file \
--disable-imap \
--disable-ldap \
--disable-ldaps \
--disable-pop3 \
--disable-proxy \
--disable-rtsp \
--disable-smtp \
--disable-telnet \
--disable-tftp \
--without-gnutls \
--without-libidn \
--without-librtmp \
--disable-dict
执行完成以后,还需要修改curl_config.h文件的一些宏定义;
打开:HAVE_LIBSSL(#define HAVE_LIBSSL 1)、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
注释掉宏 HAVE_GETPWUID_R 、HAVE_GETEUID、HAVE_GETPWUID,否则编译的时候会出现类似: error: undefined reference to 'getpwuid_r'之类的错误;
这个时候再执行
make
OK,带有https请求功能的libcurl出炉了;
再接下来,编译完成以后就是使用了,如何在android 工程中应用呢?
jni的makefile文件大体如下写也就可以了;需要将libcurl.a、libcrypto.so、libssl.so、libz.so拷贝到jni目录下;头文件拷贝到openssl下;
libz.so位置在,android NDK自带支持;
android-ndk-r10c/platforms/android-21/arch-arm/usr/lib
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)
#crypto
include $(CLEAR_VARS)
LOCAL_MODULE := crypto
LOCAL_SRC_FILES := ./openssl/libcrypto.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/openssl/
include $(PREBUILT_SHARED_LIBRARY)
#openssl
include $(CLEAR_VARS)
LOCAL_MODULE := ssl
LOCAL_SRC_FILES := ./openssl/libssl.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/openssl/
include $(PREBUILT_SHARED_LIBRARY)
#libz
include $(CLEAR_VARS)
LOCAL_MODULE := libz
LOCAL_SRC_FILES := ./openssl/libz.so
#LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/openssl/
include $(PREBUILT_SHARED_LIBRARY)
#libcurl
include $(CLEAR_VARS)
LOCAL_MODULE := curl
LOCAL_SRC_FILES := ./curllib/libcurl.a
include $(PREBUILT_STATIC_LIBRARY)
#ourLib
include $(CLEAR_VARS)
LOCAL_CFLAGS := -D_GNU_SOURCE
LOCAL_CPPFLAGS := -frtti
LOCAL_MODULE:= jni_curl
LOCAL_STATIC_LIBRARIES := libcurl
LOCAL_SHARED_LIBRARIES := crypto ssl libz
LOCAL_SRC_FILES := jni_main.cpp
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
64位环境设置:
export LDFLAGS="-L/data/NDK/android-ndk-r10c/platforms/android-21/arch-arm64/usr/lib"
export CPPFLAGS="-I/data/NDK/android-ndk-r10c/platforms/android-21/arch-arm64/usr/include"
SYSROOT=$ANDROIDNDK/platforms/android-21/arch-arm64
export CC="/data/NDK/android-ndk-r10c/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-gcc --sysroot=$SYSROOT"