Add an curl solution to android

  • 2020-05-10 18:50:06
  • OfStack

curl is a famous open source file transfer protocol implementation software, which includes the implementation of HTTP, HTTPS, FTP and other commonly used and uncommon protocols. The official source code for the latest version of curl actually includes the android compiled file (Android.mk), but there is still some work to be done before it can be compiled.

The curl version I used was 7.20.0, and the android source code version was eclair 2.1.
First, unzip curl into the external directory and change the directory name to curl (you can do without it, but android doesn't have a version number, so when in Rome, do as the Romans do).

(1) first create a header file curl_config.h. The creation method is explained in the comments at the head of the curl/ Android.mk file. Follow this instruction to run configure. The command line I used is as follows (I compiled the x86 version, but we only want to generate curl_config.h, so x86 or arm is actually 1) :


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 cannot find openssl, so the generated configuration file does not use openssl. To use ssl, you need to manually modify lib/ curl_config.h and src/ curl_config.h, Open the macro definitions 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 (but note that android does not compile engine in openssl, so do not open the macro definition HAVE_OPENSSL_ENGINE_H) and comment out the macro definition HAVE_MALLOC_H and HAVE_IOCTL, These two macro definitions are defined in Android_config.h, and there will be a lot of warnings during compilation without comments (but they will compile correctly)

(3) modify the curl/ Android.mk file, add external/openssl/include in the LOCAL_C_INCLUDES variable value, and add libssl libz libcrypto in the LOCAL_SYSTEM_SHARED_LIBRARIES variable value.

Now you can compile:
make curl ONE_SHOT_MAKEFILE=external/curl/Android.mk TARGET_PRODUCT= < ... >
 
However, the above Android.mk file will compile libcurl to a static library. To compile to a dynamic library, you need to modify the Android.mk file.


Related articles: