Porting libcurl to android with ssl support

I'm trying to pass libCurl to android with SSL support, the first step was to port the curl without ssl support, I think, so I started to do this. but I ran into a problem.

as I read on the dev website and in the Android.mk file, the tricky part is setting up make first. so what i did:

  • Download the Android source code (and compile it, as some intermediate libraries are needed)

  • Download cURL

  • unzip twist under: {android_src} / external / curl

  • create a configuration script for curl by creating a sh file in an external / curl folder with this content.

`

export A=/home/user/Development/AOSP/2.3.3 export CC=$A/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc export NDK=/home/user/Development/Tools/sdk/android/ndk export NDKLIBS=$NDK/platforms/android-4/arch-arm/usr/include export SYSROOT=$A/ndk/build/platforms/android-4/arch-arm export CPPFLAGS="-I $A/system/core/include" export LDFLAGS="-L$A/out/target/product/generic/obj/lib/ -L$A/out/target/product/generic/system/lib/-L$SYSROOT/usr/lib -Wl,--gc-sections -nostdlib -lc -lm -ldl -llog -lgcc -Wl,--no-undefined,-z,nocopyreloc -Wl,-dynamic-linker,/system/bin/linker -L$NDK/out/target/product/generic/obj/lib/" export CFLAGS="-fno-exceptions -Wno-multichar -mthumb -mthumb-interwork -nostdlib -lc -ldl -lm -march=armv5te -mtune=xscale -msoft-float -mandroid -fPIC -mthumb-interwork -mthumb -mlong-calls -ffunction-sections -fstack-protector -fno-short-enums -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -DANDROID -DOS_ANDROID -D__NEW__ -D__SGI_STL_INTERNAL_PAIR_H -I$SYSROOT/usr/include -I $A/system/core/include -I $NDKLIBS" ./configure --host=arm-eabi --with-ssl=$A/external/openssl 

`And the final result is as follows:

configure: configured to create curl / libcurl:

  curl version: 7.26.0 Host setup: arm-unknown-eabi Install prefix: /usr/local Compiler: /home/tanco/Development/AOSP/2.3.3/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc SSL support: no (--with-{ssl,gnutls,nss,polarssl,cyassl,axtls} ) SSH support: no (--with-libssh2) zlib support: enabled krb4 support: no (--with-krb4*) GSSAPI support: no (--with-gssapi) SPNEGO support: no (--with-spnego) TLS-SRP support: no (--enable-tls-srp) resolver: default (--enable-ares / --enable-threaded-resolver) ipv6 support: no (--enable-ipv6) IDN support: no (--with-libidn) Build libcurl: Shared=no, 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: /etc/ssl/certs/ca-certificates.crt ca cert path: no LDAP support: no (--enable-ldap / --with-ldap-lib / --with-lber-lib) LDAPS support: no (--enable-ldaps) RTSP support: enabled RTMP support: no (--with-librtmp) Protocols: DICT FILE FTP GOPHER HTTP IMAP POP3 RTSP SMTP TELNET TFTP 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. 

The first strange thing that comes to mind is why SSL is not included in the configuration, since the linker shows intermediate libs and the ssl support flag, but after I use the same curl_config.h file in the jni project that I created for assemblies (since it has a stand-alone Android.mk file, it can be compiled by simply unpacking it into the jni folder of the Android project, copying the configuration file created in the AOSP source and calling ndk-build)

so I compile and get:

 $ ndk-build Compile thumb : curl <= url.c In file included from /Projects/temp/testNDK/jni/lib/url.c:32:0: /Tools/sdk/android/ndk/platforms/android-14/arch-arm/usr/include/unistd.h: In function 'getpagesize': /Tools/sdk/android/ndk/platforms/android-14/arch-arm/usr/include/unistd.h:171:3: warning: nested extern declaration of '__page_size' [-Wnested-externs] /Tools/sdk/android/ndk/platforms/android-14/arch-arm/usr/include/unistd.h: In function '__getpageshift': /Tools/sdk/android/ndk/platforms/android-14/arch-arm/usr/include/unistd.h:175:3: warning: nested extern declaration of '__page_shift' [-Wnested-externs] /Projects/temp/testNDK/jni/lib/url.c: At top level: /Projects/temp/testNDK/jni/lib/url.c:57:2: error: #error "We can't compile without socket() support!" make: *** [/Projects/temp/testNDK/obj/local/armeabi/objs/curl/lib/url.o] Error 1 
+6
source share
3 answers

Here is a solution updated to NDK8c

Zero step : download and fix Android NDK I donโ€™t know how, but ndk has a very interesting drawback that (in my oppinion) does not allow you to compile many things, so in order to compile OpenSSL, you need to make a small correction, extract ndk8c, your tools, and then edit the file: android-NDK-R8C / build / gmsl / __ gmsl line 512: line change

 int_encode = $(__gmsl_tr1)$(wordlist 1,$1,$(__gmsl_input_int)) 

with line

 int_encode = $(__gmsl_tr1)$(wordlist 1,$(words $1),$(__gmsl_input_int)) 

And you are fine!

step one . Download OpenSSL and compile for Android: either compile the migrated version here or Download the official version of OpenSSL version 1.0.0c and then compile it for Android using the guide provided in github, which I linked for the version compatible with Android.

So the next step is to get libssl.so and libcrypto.so and put them in the NDK folder for easy access, so copy them from

 openssl-folder/libs/armeabi/ 

to

 android-ndk-r8c/platforms/android-8/arch-arm/usr/lib 

so when compiling you can enable libs using the simple linker switch -lssl -lcrypto

Step Two : Get The Latest Curl Source For Here

Open the file in Docs / INSTALL and follow the steps necessary to create a stand-alone toolchain and place it in the desired folder, and then in the difficult part I will need the Android source code to continue the configuration, although I have a standalone compiled openssl, you can also include it header files, in any case, this is a more complicated version, so you choose what you do, I didnโ€™t decide to avoid them so that you can go to the Google AOSP Website and follow the steps to create and initialize the environment.

so it would be something like:

1.download,

  • go to the root of the source code and run:

    ~: build / envsetup.sh; lunch 1; to do;

So finally, we need to compile curl with SSL support, so

Step three

extract curl to the desired folder (I have a specific desire to disable everything except http / s so that the library is as low as ~ 300k if you want more libraries in your library, delete --disable-protocol for the desired protocol ) follow these steps:

 make clean export PATH=/opt/arm-linux-androideabi-4.4.3/bin:$PATH export LDFLAGS="\ -lssl \ -lcrypto \ -L/home/user/Development/Tools/sdk/android/ndk/platforms/android-8/arch-arm/usr/lib" export CFLAGS="\ -I/home/user/Development/AOSP/2.3.7/system/core/include \ -I/home/user/Development/Tools/sdk/android/ndk/platforms/android-8/arch-arm/usr/include" ./configure --host=arm-linux-androideabi \ --with-ssl=/home/user/Development/Projects/portingLibs/openssl-android-master \ --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 make Note that in the block above, if you don't want to use the AOSP source, you could switch -I/home/user/Development/AOSP/2.3.7/system/core/include \ with the include folder for your ssl distribution. 

So you have: static:

 curl-7.28.1/lib/.libs/libcurl.a 

and sharing:

 curl-7.28.1/lib/.libs/libcurl.so.5.3 

So that he ... takes the file and compiles :)

+13
source

I like to use Docker to create such static libraries. There is a full script here - you just run it and get the result. Example: https://gist.github.com/VictorLaskin/1c45245d4cdeab033956

Such a script would be:

  • setup compilation tools / utils download sdk / ndk
  • create custom cross-compilation toolchain download source code for libs (zlib, openssl, curl)
  • cross-compilation environment settings
  • configure and create libs
  • collect output in one folder and create a way to get compiled libs

This particular version uses the latest NDK toolkit 10e and clang.

There is a post with more detailed information: http://vitiy.info/dockerfile-example-to-compile-libcurl-for-android-inside-docker-container/

I hope this helps someone not to waste precious time.

+1
source

The steps for building libcurl.so with ssl support for android jni can be found here: http://ieroot.com/2015/03/29/1728.html

I struggled for a week to figure this out.

Ask if there are any questions. I will reply as soon as possible.

0
source

Source: https://habr.com/ru/post/919676/


All Articles