Skip to content

Instantly share code, notes, and snippets.

@bertrandmartel
Last active October 10, 2023 17:14
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save bertrandmartel/d964444f4a85c2598053 to your computer and use it in GitHub Desktop.
Save bertrandmartel/d964444f4a85c2598053 to your computer and use it in GitHub Desktop.
Build Curl for Android NDK

Build libcurl for android NDK

Libcurl requires openssl and zlib to be fully operationnal

  • Step 1 : cross compile zlib
  • Step 2 : cross compile openssl
  • Step 3 : cross compile curl with zlib/openssl external link

Prerequisites :

Build zlib for arch arm-linux-androideabi

  • get sources
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -xvzf zlib-1.2.8.tar.gz
cd zlib-1.2.8
  • build settings
INSTALLATION_PATH=/path/to/install_directory

export CC=arm-linux-androideabi-gcc
export CPP=arm-linux-androideabi-g++
export AR=arm-linux-androideabi-ar
export prefix=$INSTALLATION_PATH
  • build & install
./configure
make
make install

Build openSSL for arch arm-linux-androideabi

  • get sources
wget https://www.openssl.org/source/openssl-1.0.1p.tar.gz
tar -xvzf openssl-1.0.1p.tar.gz
  • build settings
#Delete all reference to arm-linux-androideabi arch in your path before pursuing, the following will do this for you

INSTALLATION_PATH=/path/to/install_directory

wget https://wiki.openssl.org/images/7/70/Setenv-android.sh
chmod 777 Setenv-android.sh
export ANDROID_NDK_ROOT=/path/to/ndk
change _ANDROID_API="android-18" to _ANDROID_API="android-21" in Setecn
. ./Setenv-android.sh
cd openssl-1.0.1p
perl -pi -e 's/install: all install_docs install_sw/install: install_docs install_sw/g' Makefile.org
./config shared no-ssl2 no-ssl3 no-comp no-hw no-engine --openssldir=$INSTALLATION_PATH
  • build
make depend
make all
make install

Build curl for arch arm-linux-androideabi

  • build settings
#Before all, add standalone toolchain to your path if you dont (remove latter toolchain entry from openssl build and add the one you got from make-standalone.sh script)

INSTALLATION_PATH=/path/to/install_directory

export CROSS_COMPILE="arm-linux-androideabi"
export CPPFLAGS="-I/home/abathur/MyApplication/app/src/main/prebuild/include" #path to zlib and openssl header folder
export LDFLAGS="-L/home/abathur/MyApplication/app/src/main/prebuild" #path to zlib and openssl library folder
export AR=${CROSS_COMPILE}-ar
export AS=${CROSS_COMPILE}-as
export LD=${CROSS_COMPILE}-ld
export RANLIB=${CROSS_COMPILE}-ranlib
export CC=${CROSS_COMPILE}-gcc
export NM=${CROSS_COMPILE}-nm
export LIBS="-lssl -lcrypto"

./configure --host=${CROSS_COMPILE} --with-ssl --with-zlib --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
  • build & install
make

DESTDIR=$INSTALLATION_PATH

make install
@xerta555
Copy link

Hello,
I'm trying to cross-compiling curl for all android archs by following your guide, but i get error at this following point:
. ./Setenv-android.sh

There is my error:
. ./Setenv-android.sh : command not found : command not found : command not found : command not found : command not found : command not found : command not found : command not found bash: ./Setenv-android.sh: line 106: syntax error near the unexpected symbol "$'do\r'". ash: ./Setenv-android.sh: line 106: do`

(translated)

Of course, i have follow your step:
change _ANDROID_API="android-18" to _ANDROID_API="android-21" in Setecn

I'm targeting Android Pie (API 28) and

  • arm32
  • arm64
  • x86
  • x64
  • mips86
  • mips64

CPU archs.

I don't understand from what this error come from.
Any help would be very appreciated.

@mathiasi
Copy link

@xerta555 Seems to be some problem with the line endings.
Running
dos2unix Setenv-android.sh
should do the trick.

@deadpoolcode1
Copy link

where do you get curl sources from ?

@xerta555
Copy link

where do you get curl sources from ?

From official website..

@AbhilashSunkad
Copy link

Trying to build libcurl for my android environment

Compiling and linking on MAC OS
I have properly linked the following libraries.
/platform/android/openssl/libssl-android-x86_64/lib/libssl.a
/platform/android/openssl/libssl-android-x86_64/lib/libcrypto.a
/platform/android/curl/libcurl-android-x86_64/lib/libcurl.a

Below are the logs .

/Users/Desktop/Projects/Android_Metrics//platform/android/curl/libcurl-android-x86_64/lib/libcurl.a(libcurl_la-md5.o):md5.c:function Curl_md5it: error: undefined reference to 'MD5_Init'
/Users/Desktop/Projects/Android_Metrics/platform/android/curl/libcurl-android-x86_64/lib/libcurl.a(libcurl_la-md5.o):md5.c:function Curl_md5it: error: undefined reference to 'MD5_Update'
/Users//Desktop/Projects/Android_Metrics/platform/android/curl/libcurl-android-x86_64/lib/libcurl.a(libcurl_la-md5.o):md5.c:function Curl_md5it: error: undefined reference to 'MD5_Final'
/Users/Desktop/Projects/Android_Metrics/platform/android/curl/libcurl-android-x86_64/lib/libcurl.a(libcurl_la-md5.o):md5.c:Curl_DIGEST_MD5: error: undefined reference to 'MD5_Init'
/User/Desktop/Projects/Android_Metrics/platform/android/curl/libcurl-android-x86_64/lib/libcurl.a(libcurl_la-md5.o):md5.c:Curl_DIGEST_MD5: error: undefined reference to 'MD5_Update'
/Users/Desktop/Projects/Android_Metrics/platform/android/curl/libcurl-android-x86_64/lib/libcurl.a(libcurl_la-md5.o):md5.c:Curl_DIGEST_MD5: error: undefined reference to 'MD5_Final'
/Users/Desktop/Projects/Android_Metrics/platform/android/curl/libcurl-android-x86_64/lib/libcurl.a(libcurl_la-md5.o):md5.c:Curl_HMAC_MD5: error: undefined reference to 'MD5_Init'
/Users/Desktop/Projects/Android_Metrics/platform/android/curl/libcurl-android-x86_64/lib/libcurl.a(libcurl_la-md5.o):md5.c:Curl_HMAC_MD5: error: undefined reference to 'MD5_Update'

@teenari
Copy link

teenari commented Aug 24, 2020

this is ugly

@danoli3
Copy link

danoli3 commented May 19, 2021

Thank you *

@milinddeore
Copy link

Thanks @bertrandmartel
I would recommend these scripts, they are quite straight forward to compile. I tried with NDK v14b and its works quite well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment