当前位置: 移动技术网 > IT编程>移动开发>Android > 在android中增加curl的解决方法

在android中增加curl的解决方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

悲情城市快播,香格里拉 胡歌,阳元醇

curl是一个著名的开源文件传输协议实现软件,其中包括了http、https、ftp等多种常用不常用协议的实现。在curl最新版本的官方源代码中其实已经包括了android的编译文件(android.mk),不过要想编译通过还需要做一些工作。

我所使用的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.mk文件会将libcurl编译为静态库,要想编译为动态库还需要修改一下android.mk文件。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网