curl是一个著名的开源文件,它包括http、https、ftp等常用协议,curl官方源代码的最新版本实际上包含了android的编译器文件,今天武林技术频道小编将为大家解析android中增加curl的解决方法。
我所使用的curl版本是7.20.0,android源代码版本是eclair 2.1。
首先将curl解压到external目录下,将目录名称改为curl(不改也可以,不过android的习惯是不带版本号,入乡随俗吧)。
(1) 首先要创建一个头文件curl_config.h。创建方法在curl/Android.mk文件头部的注释里有说明。根据这个说明运行configure。我所使用的命令行如下(我是编译成x86版本的,不过我们的目的只是要生成curl_config.h文件,所以x86还是arm其实是一样的):
ANDROID_ROOT=`realpath ../..` && PATH="$ANDROID_ROOT/prebuilt/linux-x86/toolchain/i686-unknown-linux-gnu-4.2.1/bin:$PATH" ./configure --host=x86-linux CC=i686-unknown-linux-gnu-gcc CPPFLAGS="-I $ANDROID_ROOT/external/curl/include/ -I $ANDROID_ROOT/external/curl -I $ANDROID_ROOT/out/target/product/eeepc/obj/STATIC_LIBRARIES/libcurl_intermediates -I $ANDROID_ROOT/system/core/include -I $ANDROID_ROOT/hardware/libhardware/include -I $ANDROID_ROOT/hardware/libhardware_legacy/include -I $ANDROID_ROOT/hardware/ril/include -I $ANDROID_ROOT/dalvik/libnativehelper/include -I $ANDROID_ROOT/frameworks/base/include -I $ANDROID_ROOT/frameworks/base/opengl/include -I $ANDROID_ROOT/external/skia/include -I $ANDROID_ROOT/out/target/product/eeepc/obj/include -I $ANDROID_ROOT/bionic/libc/arch-x86/include -I $ANDROID_ROOT/bionic/libc/include -I $ANDROID_ROOT/bionic/libstdc++/include -I $ANDROID_ROOT/bionic/libc/kernel/common -I $ANDROID_ROOT/bionic/libc/kernel/arch-x86 -I $ANDROID_ROOT/bionic/libm/include -I $ANDROID_ROOT/bionic/libm/include/i387 -I $ANDROID_ROOT/bionic/libthread_db/include -I $ANDROID_ROOT/external/openssl/include" CFLAGS="-fno-exceptions -Wno-multichar -march=i686 -m32 -fPIC -include $ANDROID_ROOT/system/core/include/arch/target_linux-x86/AndroidConfig.h -m32 -DANDROID -fmessage-length=0 -fno-strict-aliasing -Wno-unused -Winit-self -Wpointer-arith -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -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" LIB="$ANDROID_ROOT/prebuilt/linux-x86/toolchain/i686-unknown-linux-gnu-4.2.1/lib/gcc/i686-unknown-linux-gnu/4.2.1/libgcc.a $ANDROID_ROOT/prebuilt/linux-x86/toolchain/i686-unknown-linux-gnu-4.2.1/lib/gcc/i686-unknown-linux-gnu/4.2.1/crtbegin.o $ANDROID_ROOT/prebuilt/linux-x86/toolchain/i686-unknown-linux-gnu-4.2.1/lib/gcc/i686-unknown-linux-gnu/4.2.1/crtend.o"
(2) configure找不到openssl,所以所生成的配置文件是不使用openssl的。要想使用ssl,需要手动修改 lib/curl_config.h和src/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中有定义,不注释的话在编译过程中会有很多警告(但是能正确编译通过)
(3)修改curl/Android.mk文件,在LOCAL_C_INCLUDES变量的值中增加external/openssl/include,在 LOCAL_SYSTEM_SHARED_LIBRARIES变量的值中增加 libssl libz libcrypto。
好了现在可以编译了:
make curl ONE_SHOT_MAKEFILE=external/curl/Android.mk TARGET_PRODUCT=<...>
上述是武林技术频道小编带来的android中增加curl的解决方法,其实这个功能对于大多数人来说都是很实用的,希望对你有帮助!